macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Automated Backups on Tiger Using rsync

by Richard Hough
07/22/2005

Mac users know that they should back up their data, but few do it. Why? Here are a few reasons:

  • Commercial backup programs are too expensive, don't provide enough storage space, or require constant renewal fees.
  • Many backup programs require the user to run them, and people can't be bothered to remember to do this.
  • Backing up by copying files can take a long time and overwhelm your backup storage space.

In this article I'm going to show you how to create a free, customizable backup solution using only software that comes with Mac OS X 10.4, which will automatically back up changes to your data without user intervention.

To use this solution, you should back up to an external firewire hard drive or to a network volume. Do not attempt to back up to a CD or DVD drive or to an internal hard drive. Removable media, such as DVDs, do not have enough storage space to back up modern hard drives. Backing up to an internal drive is unsafe because anything that destroys your internal data would probably destroy the backup as well. An external firewire drive with a capacity greater than your internal drive is an effective backup storage device.

The rsync Command

The article An Introduction to Tiger Terminal, Part 2 introduced the rsync command. We can use this command to compare the files on your internal drive to those on the backup, and back up only the files that have changed. The command:

rsync -aE --delete ~ /Volumes/FW200/Backups

compares the files on the source folder (in this case the user's home folder, ~) to those in the target folder (Backups) on the external backup drive (/Volumes/FW200/). The earlier article shows how to find the command-line path to external drives, then use the path to your own external drive instead of "/Volumes/FW200/Backups". You could also specify a different source folder (for example, "~/Documents") if you only want to back up that folder.

Type man rsync in the Terminal application to see a list of the rsync options and parameters. In the example above, The -aE options will copy all files that have changed in all folders in the source folder or that do not exist in your target folder, and will ignore files that are the same in both folders, while preserving Mac OS metadata. The --delete option will delete files in the target folder that are not in the source folder. In short, this command will make as few changes to the target folder as necessary to make it a duplicate of the source folder.

Note that this command will create a folder with the same name as your home folder in the backup folder. You should do this to allow different users to back up to the same external drive.

We will use this command as the basis for our automated backups.

Running Commands from the Finder

To run this command automatically, instead of having a user type it in the Terminal, we need to store it in and run it from a file. One way of doing this is to create an AppleScript. Open Apple's Script Editor application and type the following command:


do shell script 
"rsync -aE --delete ~ /Volumes/FW200/Backups || echo -n"

Remember to use your external drive's folder instead of "FW200/Backups." Save the script with file format of "script" in a convenient folder, such as the Scripts folder in your home folder's Library folder.

tab 1

You may wonder what the characters "|| echo -n" are for. Every Terminal command returns a status code. rsync returns zero if files were copied without error, or nonzero if there was an error. The AppleScript do shell script command will display an error alert if the shell command returned a nonzero status. We will be running the command unattended, so won't be able to dismiss this alert. Also, I have found the "error" to almost always be a missing Mac OS metadata file for a file that has no metadata. We can ignore such a situation.

The characters "||" in a shell script run the following command if the preceding command returns a nonzero status code. In this case, the following command is "echo -n," which does nothing and returns a status code of zero. The result is that no alert is shown if the rsync command returns an error status.

Pages: 1, 2

Next Pagearrow