I’ve started using a backup strategy based on that originally described by Jamie Zawinski and subsequently covered in Jeff Atwood’s What’s your backup strategy? article. It works by incrementally backing up your data to a bootable clone of your computer’s internal drive, in order to replace the internal drive when it fails.
This script is now developed in my dotfiles repo. Please report problems or improvements in the issue tracker.
This post is mainly to document – for myself as much as anything – the process I went through in order to implement an incremental backup strategy in OS X 10.6. Use at your own risk. Feel free to suggest improvements if you know of any.
Formatting and partitioning the drive
With your backup drive in its enclosure, connect the drive to your Mac and open the Disk Utility application.
- Click on the disk’s name. This should bring up a “Partition” tab in the right panel.
- Click on the “Partition” tab.
- Under “Volume scheme” select the number of partitions you need. Probably “1 partition” if it is to match your internal disk.
- Under “Name” enter the volume name you want to use, e.g., “Backup”.
- Under “Format” select “Mac OS X Extended (Journaled)”, which is necessary if the disk is to be bootable.
- Click “Options” and check that “GUID Partition Table” is selected.
- Click “Apply”.
This will format and partition the disk. The partition(s) should now show up in the Finder and on the Desktop.
Enable ownership permissions
The new partition needs permissions to be enabled to avoid
chown errors when using
rsync. To do this, select the partition and view its information page (using “Get Info” or
Command+I). Expand the “Ownership & Permissions” section and uncheck “Ignore ownership on this volume”
The backup script relies on using rsync – a fast and versatile file copying tool that is included in OS X – to manage the copying and moving of data between volumes. It offer a wide variety of options and only copies the differences between the source files and the existing files in the destination, making it ideal for incremental backups. You can find out more about rsync in the rsync documentation.
The following is the contents of a script I’ve named
rsync_backup.sh. I’m using it to backup all of the data on my internal disk, with a specified set of exceptions contained within a file called
#!/bin/sh PROG=$0 RSYNC="/usr/bin/rsync" SRC="/" DST="/Volumes/Backup/" EXCLUDE="$HOME/rsync_excludes.txt" # -v increase verbosity # -a turns on archive mode (recursive copy + retain attributes) # -x don't cross device boundaries (ignore mounted volumes) # -S handles spare files efficiently # -H preserves hard-links # --extended-attributes preserves ACLs and Resource Forks # --delete deletes any files that have been deleted locally # --delete-excluded deletes any files that are part of the list of excluded files # --exclude-from reference a list of files to exclude if [ ! -r "$SRC" ]; then /usr/bin/logger -t $PROG "Source $SRC not readable - Cannot start the sync process" exit; fi if [ ! -w "$DST" ]; then /usr/bin/logger -t $PROG "Destination $DST not writeable - Cannot start the sync process" exit; fi /usr/bin/logger -t $PROG "Start rsync" sudo $RSYNC -vax -S -H --extended-attributes --delete --exclude-from=$EXCLUDE "$SRC" "$DST" /usr/bin/logger -t $PROG "End rsync" # make the backup bootable sudo bless -folder "$DST"/System/Library/CoreServices exit 0
This is the contents of the
.Spotlight-*/ .Trashes /tmp/* /Network/* /cores/* /afs/* /automount/* /private/tmp/* /private/var/run/* /private/var/spool/postfix/* /private/var/vm/* /Previous Systems.localized /Volumes/* */.Trash
Adapted from the excludes file at Automated OSX backups with launchd and rsync
Every time the script runs, messages will be written to the system log.
Check that the source (
SRC) and destination (
DST) paths in the script are correct and match the volume name that you chose when partitioning the disk. Wrapping the
$DST variables in double quotes ensures that the script will work even if your volume names contain spaces (e.g. “Macintosh Backup”).
The command option
--exclude-from tells the script where to find the file containing the exclude patterns. Make sure you either have
rsync_excludes.txt in the home directory or that you update this part of the command to reference the full path of the excludes file.
Running the backup script
You can run the script from the command line, or make it executable from the Finder or the Desktop:
- Type the following into the command line to ensure that you have permission to execute the script:
chmod +x /path/to/rsync_backup.sh
- Remove the
.shextension from the script.
- Create an alias of the script and move it to the Desktop.
- Double click the icon to run the backup script.
It’s important to run the script regularly in order to keep the backup in sync with your internal disk. If you have a desktop computer, or you never turn off your laptop, you can automate the running of the script by setting up a cron job.
Checking the disk is bootable
Once you’ve run the backup script, you should test that the backup disk is bootable. To do this, restart your computer and hold down the Alt/Option key. Your backup disk should be presented, with the volume name you chose, as a bootable disk.
When I first booted my backup, the terminal displayed the following line:
dyld: shared cached file was build against a different libSystem.dylib, ignoring cache
According to this article, the fix for this is to update the cache by entering the following into the terminal:
sudo update_dyld_shared_cache -force
That should be everything you need to start implementing an incremental backup strategy when using OS X.
Comment on this post
Please wrap code fragments in
<code> tags, wrap blocks of code in
<pre><code>, and use JsFiddle to post working examples.