Posts Tagged ‘script’

Backup Script: A Love Story

Sunday, August 15th, 2010

I have written a few backup scripts by now. Every time I do I find a new interesting challenge somewhere in the task. As such I’d like to talk a little about my most recent backup script and offer my script to the community at large.

First of all, I tend to use rsync for backups. It’s powerful and it works well. You can use rsync to backup any file system and so it is also very flexible in a network or on a virtual machine. I’m not going to dive into the man page for rsync, but you will want to take a look there (man rsync) because there is a wealth of information about the various switches available for that command. I have selected the switches that fit my purposes and that is what is displayed below.

Next, where possible I prefer to leave my backup drives unmounted until they are actually needed for the backup process. I consider my current system for dealing with this imperfect, but again that is what you will see in my current script.

Finally, the server I have written this script for is running the desktop version of Ubuntu. This is not likely important in any way except that (as you will see) if I had been running the server version I would not likely have had the same problems when I did my test run.

Ok, so here is my current script.

#
#
#
# run in cron Mondays at 4 AM
# [0 4 * * 1 /home/[scriptpath] >/dev/null 2>1]

# redirect from script --> sends all STDERR to log file

exec 2> /home/[username]/Desktop/$(date +%Y%m%d) ## use dated folder on desktop

## backup [DriveAa]

if ! mountpoint -q /media/[BUDriveA]
then
   mount -t [filesystem] -U [UUID goes here; no brackets or quotes] /media/[BUDriveA]
fi

# Note rsync -a copies permissions but will not copy owner:group if not run as root

rsync -ailS --delete --progress /media/[DriveA]/[Folder] /media/[BUDriveA]

umount /media/[BUDriveA]

## backup [DriveB]

if ! mountpoint -q /media/[BUDriveB]
then
   mount -t [filesystem] -U [UUID goes here; no brackets or quotes] /media/[BUDriveB]
fi

# Note rsync -a copies permissions but will not copy owner:group if not run as root

rsync -ailS --delete --progress /media/[DriveB]/[Y] /media/[BUDriveB]
rsync -ailS --delete --progress /media/[DriveB]/[Z] /media/[BUDriveB]

umount /media/[BUDriveB]

#

As you can see I like to keep my scripts well documented. I encourage you to do the same. Memory is fallible, after all.

(Note: Everything in square brackets [] will be replaced in your script.)

[filesystem]

This is where you specify the file system type you are using and is called out by the -t argument. For my script it was ext3.

[UUID]

This is a unique identifier for a drive and it is called out by the -U argument. I prefer using UUID’s because other drive/partition designations can change (for instance, sda1 can become sdb1 if you add a new drive). (For information on adding drives to your system see this ink.)

My backup script manages the backups for three directories (recursive) over two drives. I first test to see if a backup drive is mounted and if it is not I mount it. That’s the job of the if statements. (This is what I consider imperfect and will seek to improve as time moves forward.) It works well enough, and if you don’t want your backup drives to be mounted all the time this is a decent way of dealing with the matter.

You will also note that I unmount each drive as I finish with it (umount).

You will also see that in the DriveB example I backup (synchronize) two directories (Y & Z). This also helps to make it clear that you do not need to specify the name of the directory at the backup location.

Lastly, you see my note about -a copying permissions and requiring root to copy also the owner and user information. As such I put this script into a cron job as root.

What?

It’s easy enough to do. Just open cron as root:

#
#
sudo crontab -e

You will be prompted for your password. You can learn more here. You can see my cron entry for this script in a comment in my script above. The man page for cron (man cron) will help you understand how 0 4 * * 1 means every Monday at four in the morning.

Let’s talk about some of the mistakes I made.

The first big snag was not having the if statements correct. I left out the UUID’s and so the script did not mount the drive for the first sync operation. The if statement tested to see if DriveA was mounted and found that it was not. Then it ran the mount command which failed because no valid drive (UUID) was specified.

Because the drive was not mounted at the mount point rsync began synchronizing data to the mount folder and not the mounted drive. This caused the script to fail (once the OS drive containing / was full—about 105 GB later) and borked the / partition.

I was not able to restart Gnome (Gnome, the desktop environment, requires some free space on your / partition to function and mine was 100% full). I ssh’d into the machine from a Windows box nearby (using Cygwin) and maybe two hours later I sorted out what I had done. I was able to remove (using the rm command) the offending folder after making absolutely certain DriveA was in fact not mounted. After that I was able to reboot and get back into Gnome. (However, I did have to run fsck on the drive probably due also to the drive having filled itself. If you boot your system and get a shell stating you cannot login try running fsck and answering y to all the fixit questions.)

Oops. This is why we make test runs, right?

So I fixed the if statements (specifically the mount and umount commands) and that took care of that.

Then the script ran fine through the backup for DriveA but finished DriveB in a few seconds. Not possible. I looked back at the script and realized that I had specified the backup location for both the source and the destination. Damn it. Fixed that and DriveB was synchronizing properly.

I hope this helps you out.

(Thanks to Ian over at Ubuntu for his suggestions while I was troubleshooting.)

Happy scripting.

JamesIsIn
  • Share/Bookmark

BASH sudo through a Pipe

Tuesday, June 1st, 2010

I fixed my dad’s Windows box and after getting it all set up I figured I would create a backup image based on the completed build/installations/updates.  A nice best-of restore point, eh?

So I booted the machine into an Ubuntu 10.04 Live CD, created the necessary partition using gparted, and mounted that partition onto which I would create the backup.

I tried using dd to create the backup image, but found that my backup space was too small for the full OS partition.  To build the backup I ran this:

#
#
# dd command from partition to file
#
sudo dd if=/dev/hdb of=/path/to/image

Not such a problem because I could of course pipe my dd output into gzip and compress my backup image.  Typically if you want to compress the  image you would merely pipe the dd output into gzip like thus:

#
#
# normal format for piping dd into gzip
#
dd if=/dev/hdb | gzip > /path/to/image.gz

The problem is that if I used sudo on this full command I hit a permission denied error.  This is because sudo only applies to the first command (dd) and not to any subsequent commands (in this case gzip)—sudo does not flow through the pipe.  However, I pulled out my trusty BASH Cookbook and found a way to run the whole thing under sudo:

#
#
#
# proper way to manage using dd, gzip, and sudo
#
sudo bash -c 'dd if=/dev/hdb | gzip > /path/to/image.gz'

This runs a bash terminal as root (non-interactive) for the duration of the quoted command and then exits back into the interactive terminal I had been using.  Worked great.

(My backup partition was hdb; adjust your code accordingly.)

Thanks, O’Reilly.

JamesIsIn
  • Share/Bookmark

Update to My Renaming Script

Wednesday, March 3rd, 2010

A while back I wrote a post on renaming FLAC files to include their associated disc numbers. For example, if you have a file from disc 1 named “01 – Track Name.flac” my script would change it to “01.01 – Track Name.flac”.

This updated version of the script allows the user to enter the path to the folder needing files renamed and the associated disc number.

I think this is a slightly simpler method than my previous method for using this script. You can read about the original script here.

Here is the current script:

#
#
# Prepend album number before track numbers
# 01 - Track Name.flac becomes 01.01 - Track Name.flac

echo
echo "This script will change FLAC file names based on a containing folder and a disc number which you supply."
echo "Example:  \"01 - Track Name.flac\" becomes \"01.01 - Track Name.flac\""

read -p "I will require the path to the containing folder: "
if [ -d "$REPLY" ]; then
printf "I have confirmed this is a directory.\n\n"
directory="$REPLY"
else
printf "I cannot find this directory.\n\n"
exit 1
fi

read -p "Which disc number, please (01 through 99): "
# add test for 01-99
printf "%b\n\n" "I will prepend $REPLY to all FLAC files in: $directory\n\n"
newname=$REPLY

read -p "Press <ENTER> to continue (ctrl-c will abort)."
echo

cd "$directory"

rename "s/^/$newname./" *.[Ff][Ll][Aa][Cc]
#

This version also handles FLAC extensions in any case (flac=FLAC=FlAc &c).

If I make any updates, I’ll likely just change this post. As such, I should say that this current version is current as of 8 March 2010.

Have fun with that.

JamesIsIn
  • Share/Bookmark

Little Apes to FLAC files

Monday, January 18th, 2010

You may come across individual APE files which you would like to convert to FLAC files.  This can be especially useful if you have an album FLAC and an accompanying CUE file and you are having trouble getting the APE to burn correctly.

There is a fairly simple tool for dealing with this conversion.  As fortune would have it this tool will also manage conversions to and from ALAC, SHN, TTN, and WAVE files.  How can they pack so much fun into such a small package?  Now it’ll be that much easier to clean your collection up and keep everything tidy: flacflacflac.

Ok, so you’ll need—and if you’ve been following along here you already have—the MAC (Monkey Audio Codec).

Next you’ll need to get something called apeinfo.   There exist both 32 and 64 bit versions so be sure you get the proper version for whichever Ubuntu you are running.  You will find them here.  You will want to change the name of the file you download to merely apeinfo (so remove the _32 or _64) or it won’t work when called up by the next tool.

The next tool being known as convtoflac.  You may find that here.

Download both of your files and they all gets stuck into /usr/local/bin.

I downloaded each of these to my desktop and then used sudo to copy them into /usr/local/bin.  Make certain you have given them execute permissions (set the execute bit).  By storing them in /usr/local/bin I am sure they are in my command path (basically those places your system looks when you type a command) and they are ready to use.  Here is the command you need to move the apeinfo file from the Desktop to /usr/local/bin:

sudo mv /home/[usename]/Desktop/apeinfo /usr/local/bin/apeinfo

You can copy and paste that line and make the necessary substitutions for your system, download location, and specific file.

Once you have these installed correctly you are ready to use the tools to make conversions.  I have written a small simple script for making all of this work together.  If you make my script executable and place it  in your /usr/local/bin you can merely call the whole thing up by typing the name of the command (whatever you decide to name your version of my script) in your terminal.  I named my script Ape2Flac.sh.


#  Ape2Flac.sh

#  also requires apeinfo and convtoflac
#  http://www.soundunreason.com/InkWell/?p=1335

for i in *.ape
do convtoflac.sh "$i"
done

This method will preserve any existing tags.

To fire it up, navigate into the folder in which you have the APE files and run my script (by typing its name into the command line).  It’s that easy.

See, nothing to fear from the command line.

Happy hunting.

  • Share/Bookmark

Out of My Shell and into My Collection

Saturday, January 10th, 2009

As you may have heard, I’m building a music server for my home (aka my local network).  I’m rippin’ dem discs down to .flac files and stowing dem on a phat drive.  Caught between geek and gangster, I’m having a good time.

I am using Sound Juicer to squeeze my CD’s into flacs.  You can read about my encoding woes and solutions here.

Sound Juicer: Applications —> Sound & Video —> Audio CD Extractor

One thing about using Sound Juicer is that it likes to put all the files for an album or boxset into a single folder with the name of that album or boxset (under a folder with the name of the person or band or whatever).  That works out fine if you rip single albums (like Pink Floyd’s Meddle, for instance).  It creates files with the track numbers (optional, but I like it) and track names:

01 One Of These Days.flac

As you can see, that’s pretty straight forward.  Where I run into trouble is when I rip a boxset, such as The Immortal Soul of Al Green.  This is a four disc set.  Sound Juicer wants to create a folder called “The Immortal Soul of Al Green” and put all of the tracks into that folder.  That means I have four track ones and four track twos…

Ack!  What a mess that is.

So I did some hunting around the web and got some help from this blogger here, and I wrote a script that would do me a favor.

I don’t really want to create folders called “The Immortal Soul of Al Green CD1″ and “The Immortal Soul of Al Green CD2″ all the way down the line for each double album and boxset I encounter.  Instead I will be satisfied by prepending each track number with the corresponding disc number for that particular album.  Single albums I will not change.

So, for the album “The Immortal Soul of Al Green” I will run a script on those files for disc three such that the files will change as follows:

01 Take Me To The River.flac —> 03.01 Take Me To The River.flac

This tells me that this song is the first track on the third disc of this set.  Using this method keeps all of the songs from this boxset in one folder (called “The Immortal Soul of Al Green”) under the folder in my collection called Al Green.  Further, when I look in this folder all of the songs are in order.  Very convenient and easy to comprehend.

At first I was doing this by renaming each file as they were ripped.  Lame.  Oh, so very lame.  Curdled lameness.

(I have updated this renaming script.  You can find that new version here.)

Here is the script I settled upon thanks to a little research and some kind coders far and wide:

for filename in *.flac
do
mv "$filename" ../Changed/03."$filename"
echo $filename changed | tee -a /home/[username]/Desktop/Changed/logfile
done

As you may guess, this is the script for changing the names of the songs from CD 3.  Remember to use your actual username.  To make the scripts necessary for other CD’s simply change the 03 to whatever disc number you are interested in (01 or 02 or whatever).  Put this into an empty file and save it.  I saved mine as ChangeScript.sh (the sh file extension merely indictates it’s a shell script).  Name yours whatever you want.  (The line beginning echo is totally superfluous to the actions desired and so can be omitted, but it allows the script to tell you what it’s doing and so it can be very handy.)  After you have saved this file you will want to alter the permissions of the file and allow execution:

  1. Right-click the .sh file you created and saved
  2. Choose Properties from the context menu which appears
  3. On the Properties dialog select the Permissions tab
  4. Check the “Allow executing file as program” box

Here is the setup I was using in which this script was functioning.  I had a folder on my Desktop for each disc number (01-08) and after a disc would rip I would drop all the files for that disc into the appropriate folder.  I had a copy of the script in each folder (each using the corresponding disc number 01-08).  Open a command prompt (Applications —> Accessories —> Terminal) and get to the folder where you plan to do this work—I used my Desktop (enter: cd ~Desktop).

In a command prompt after dropping all the files for a disc into the correct folder I would perform the following actions to get into that folder and run the script (you won’t need the ../ the first time):

  • cd ../01
  • ChangeScript.sh

This would put all of the files into another folder on my Desktop which I called Changed.  (It also creates a logfile in Changed in case that interests you.)  Once I had changed all the discs for a set I would move the files from Changed into my library.  Then I would move on to the next set.

Totally confusing?  You’ll get there.  Hang on tight.

Hope this has been helpful.

  • Share/Bookmark