macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Automated Backups with Existing Tools

by Peter Hickman
02/10/2004

The big difference between the world of Unix and that other place is that in the other place they have applications and we have tools.

Applications require learning, not just parameters and keystrokes but a whole way of working. If the programmer who created the word processor thinks you should spell-check your documents before you save them, then check them you shall.

Tools, on the other hand, also require learning, but instead they ask "What do you want to do?" The choice is yours. And with a good toolbox full of these, anything is possible.

Backup -- The Job Nobody Wants to Do

To backup my iMac, I bought a small FireWire hard disk and used Carbon Copy Cloner to make my backups. Although this software is simple and effective, there were two flaws. First, the backups only happened when I remembered to initiate them, and second, I had only one backup, whenever I last made it. I wanted something that addressed these issues, so I sat down and listed my requirements:

Related Reading

Mac OS X Panther for Unix Geeks
Apple Developer Connection Recommended Title
By Brian Jepson, Ernest E. Rothman

  • Automatic backup, preferably when I was not using the Mac.
  • Several generations of backup available.
  • As cheap as possible.

I did look at some commercial software. But in addition to the cost of the software itself, I would need to get a bigger drive and learn how to use the application (see paragraph 1). The application and I did not get along, it soon found itself on eBay, and I was back where I started, along with the investment of a 120 Gb disk. It was time to dig into my existing toolbox and see what we could cook up.

Apple already provides a tool to make backups -- ASR (Apple Software Restore), which allows one volume to be cloned onto another. In addition to this, we also have the multifunctional hdiutil, which can create, mount, and unmount disk images. With these and a handful of standard Unix tools, we are well on our way to a happy ending.

We need to create a disk image of the required size on the external drive, mount it, clone the source drive into the image, and unmount it.

Writing the Code

Let's write the code (the line numbers here are just for reference). First we need to name a few things. SOURCE is the volume that we are backing up -- in this case root. FILEDEST is the volume where the backup will be stored. All disk images require a VOLUMENAME, otherwise they get called untitled, which is really not a lot of use to anyone, so we set it to backup_YYYY-MM-DD and use it as the basis of the IMAGENAME.


 1:#!/bin/sh
     
 2:############################################
 3:# Set some variables
 4:############################################
     
 5:### The name of the volume we wish to backup
 6:SOURCE='/'
    
 7:### The volume onto which we are putting the backup
 8:FILEDEST='/Volumes/Overflow'
     
 9:### Creating the backup disk image file and volume name
10:VOLUMENAME=backup_`date +%Y-%m-%d`
11:IMAGENAME=$FILEDEST/$VOLUMENAME.dmg

Next we need to know how big to create the image. Line 15 tells us the disk usage in Mb of the SOURCE drive, line 16 gets the actual Mb used and line 17 adds 5% to the size just to make sure that there is actually enough room.


12:############################################
13:# Find out the size we require (disk usage in Mb + 5%)
14:############################################
     
15:SIZE=`df -m $SOURCE | grep '^/'`
16:SIZE=`echo $SIZE | cut -d" " -f3`
17:SIZE=`dc -e "$SIZE $SIZE 5 / + n"`

Next we create the image file on line 21 and mount it on line 25. As we mount (attach) the new volume we are grabbing the device name, /dev/disk1s4 for example, that we will require when we unmount the image at the end of the script.

 
18:############################################
19:# Create the image
20:############################################
     
21:hdiutil create -quiet -megabytes $SIZE -fs HFS+ \
                 -volname $VOLUMENAME $IMAGENAME|
     
22:############################################
23:# Mount the image and capture the device name
24:############################################
     
25:DEVICE=`hdiutil attach $IMAGENAME | grep $VOLUMENAME | cut -d" " -f1`

Now we can backup the SOURCE into the image file. The full path for asr is given so that the command can be found when we run it under the more limited cron path arguments.

 
26:############################################
27:# Do the actual backup
28:############################################
 
29:/usr/sbin/asr -source $SOURCE -target /Volumes/$VOLUMENAME -noprompt

Finally we need to unmount the image using the DEVICE we captured on line 25.


30:############################################
31:# Unmount the image
32:############################################
   
33:hdiutil detach -quiet $DEVICE

Making Things Even Easier

So we have a tool that will backup a designated volume into an image file. To run it, which will have to be done as root, we have to enter sudo backupimage, and almost everything we wanted is ours.

Now we want to automate it so that we no longer have to remember to do this chore, and it can execute when we are tucked safely in bed. In the world of Unix we have cron, which runs tasks for us at regular intervals. So we need to set up a cron,entry as root to run our script once a week (or the frequency you prefer).


[peterhickman]$ sudo crontab -e -u root
0 0 * * 0 /usr/local/bin/backupimage

Should things go wrong, cron will mail you if it can, but if sendmail is not configured or running on your machine (and by default it is not running) then look at /var/mail/root, which will tell you what the problem was.

So there we have it. All we ever needed to do our backups, other than the external disk, was in our toolbox all the time. External hard disks are cheap and there is no reason to buy additional software.

Other Issues

It's important to remember that your external hard disk, although large, is not infinite. Every now and then check that you're not going to run out of space. That too could be automated, but would detract from the simplicity of our current solution -- another problem for another day. Also just because the script runs without error does not mean that it has worked; open your backup image file and rummage around. Are the files there? Are they correct?

You can automate your chores but you cannot automate your responsibility.

Final Thoughts

Thanks to solid built-in tools like these, Macs are putting the power back into the hands of users who like to fiddle under the hood. Almost everything we need is there. We just forget sometimes.

Peter Hickman is currently working as a programmer for Semantico, which specializes in online reference works and Access Control Systems. When not programming or reading about programming he can be found sleeping.


Return to MacDevCenter.com.