MacDevCenter    
 Published on MacDevCenter (http://www.macdevcenter.com/)
 See this if you're having trouble printing code examples


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:

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.

Mac OS X: The Missing Manual

Related Reading

Mac OS X: The Missing Manual
By David Pogue

If you want to record shell command errors, perhaps for debugging the script, change the command to:


do shell script 
"rsync -aE --delete ~ /Volumes/FW200/Backups 2>>~/rsyncErr.txt || echo -n"

This will append the error message to the file rsyncErr.txt in your home folder. This change is only of use if you need to see the shell error message.

As noted in the Tiger Terminal article, several characters such as spaces, quotes, apostrophes are command-line metacharacters. That is, if you type them in the command line they are interpreted as commands and not part of the name. If your backup target folder contains any of these characters, you will need to escape or quote the characters appropriately. Fortunately, AppleScript includes a quoted form command that escapes a text string appropriately for the command line. You would use it like so:


do shell script 
"rsync -aE --delete ~ " & quoted form of "/Volumes/FireWire 200/My Backup ƒ"

Note the space between the tilde and quote in the above command. You need the space, in unquoted form, to separate the source and target file names. Remember, you only need to do this if the target folder name has a command-line metacharacter.

Try running the script you just made; it should back up your entire home folder to the target folder of your external drive.

Running Commands Automatically

We need a way of running the backup command automatically, without a person doing it. The easiest way of doing this in Tiger is to create a repeating alarm in iCal.

Launch iCal and create a new event. Set the event's "from" field to the time you want your backup to take place, probably early morning or some such time when you are not using your Mac. You will make the event repeat and trigger at the start, so the dates and "to" fields are not important.

Set the "repeat" field to "every day" and the "alarm" fields to "Run script," your script file, and "0 minutes before." Note that you will have to select "Other..." from the alarm file popup menu to select the script file.

tab 2

Now iCal will run your backup script automatically at your event "from" time (for example, 2 a.m.). You must leave your external drive mounted and iCal running to trigger the backup script.

You can add other commands you wish to be run daily to your backup script. For example, you could add more rsync commands to back up other mounted volumes.

Richard Hough is a web developer for a Vancouver, Canada educational software company.


Return to the Mac DevCenter

Copyright © 2009 O'Reilly Media, Inc.