HOWTO: Backup Your Mac With rsync

About This Document

Author: Matthew Phillips <backup@mattp.name>

Click here for Belorussian translation (thankyou to Marina)
Click here for German translation (thankyou to Marina Dmitrieva)

This document describes how to setup a system for automatically backing up a Mac (OS X 10.2 or later) to an external drive using the freely available rsync utility. This applies to any kind of external drive including FireWire, USB and network drives. It does not apply to CD-R or DVD-R media. It also does not apply if you want to implement an incremental backup system.

External Drive Setup

If you've already set up the external drive you'll be backing up to, you can skip to the next section.

To create a partition on the disk drive, connect the drive and wait for Mac OS X to offer to run Disk Utility to initialise it.

The details of how to use Disk Utility are beyond this HOWTO and are well covered by the Mac's online help: just run help and search for "Partitioning a hard disk".

Below is a screenshot of a partitioned drive in Disk Utility. In this example I've chosen to create two partitions, named OSX and Data, one for backing up the core system and user accounts, and one for large data files such as music and movies. These appear as drives on the desktop and as folders in the /Volumes folder. If you're not sure what you want, just create a single OSX partition.

Disk Utility screenshot

When you've created the partitions, you'll see them appear as empty drives in the Finder and on the Desktop.

Ensuring Ownership Permissions Are Enabled

For some reason Mac OS turns off permissions management for new partitions by default, which will result in "chown" errors from rsync if not rectified.

To enable permissions, select the partition in on the Desktop and show its information page by hitting Command+I. Expand the "Ownership & Permissions" section and make sure that "Ignore ownership on this volume" is not checked as shown below.

Partition info screenshot

Software

Note to users of Mac OS X 10.4 and later: Apple extended the version of rsync shipped with Mac OS X 10.4 to directly support resource forks, so if you don't want to use the GUI provided by rsyncx, you can skip the software installation and go to the Advanced section.

You only need one piece of software to backup your Mac that's not already part of Mac OS: rsyncx. Although Mac OS X comes packaged with bog-standard Unix rsync, rsyncx is needed in order to preserve Mac-specific resource forks. While Apple is no longer recommending applications use resource forks, some classic Mac apps still rely on them. rsyncx also comes with a graphical front end that can make it much easier to get started.

You can download rsyncx from VersionTracker (version 2.1 was current when this document was last updated). More information about rsyncx and its developer, Kevin Boyd, is available here.

To install rsyncx, simply extract the rsyncx archive and find the installer package inside it which will be called something like RsyncX_v2.1.pkg. Double-clicking this will install the rsyncx GUI and the command line program /usr/local/bin/rsync (it will not delete the built-in rsync).

Simple Setup

If you just want to create a backup of your entire Mac hard disk (and optionally make it bootable), then this section is for you. If you want to back up only some of the drive or use multiple partitions, see the Advanced section.

The simplest way to use rsyncx is to use the graphical front end: just select RsyncX from the Applications folder. Below is a screenshot of how to set up the application to back up the entire Mac hard drive to the external drives' OSX partition and make it bootable.

RsyncX screenshot

Tips

Booting From Backup

Obviously the best way to test whether the backup will work in an emergency is to boot from it. To do this, restart the Mac and hold the Option key down while it boots. You will be presented with a screen that allows you to choose the boot drive. If you can successfully boot and login from the external drive, then you can be fairly confident of being able to recover from a disaster by simply booting from backup and using rsync in reverse to restore the system.

Advanced Setup

If you want to do more funky things like backup different parts of the drive to different partitions, exclude certain files from backup or any other sort of customised stuff, then you may want to write some scripts to drive rsync from the command line. This section contains some tips on doing this. It assumes you know Unix-type stuff like vi.

Mac OS X 10.4 users

As of 10.4 Apple's shipping rsync supports resource forks by specifying the -E flag on the command line. The example script allows switching to the OS X built-in rsync by swapping commented definition of RSYNC at the top of the script.

Below is the text of a script called backup.sh that can be used to backup all of the main hard disk, with a specified set of exceptions listed in a file called backup_excludes.txt.

  #!/bin/sh

  # To use Apple's rsync switch commented lines below
  # To use rsyncx:
  RSYNC=/usr/local/bin/rsync --eahfs --showtogo 
  # To use built-in rsync (OS X 10.4 and later):
  # RSYNC=/usr/bin/rsync -E
  
  # sudo runs the backup as root
  # --eahfs enables HFS+ mode
  # -a turns on archive mode (recursive copy + retain attributes)
  # -x don't cross device boundaries (ignore mounted volumes)
  # -S handle sparse files efficiently
  # --showtogo shows the number of files left to process
  # --delete deletes any files that have been deleted locally
  # $* expands to any extra command line options you may give

  sudo $RSYNC -a -x -S --delete \
    --exclude-from backup_excludes.txt $* / /Volumes/OSX/

  # make the backup bootable - comment this out if needed

  sudo bless -folder /Volumes/OSX/System/Library/CoreServices

You can copy and paste this text into a new new and then execute chmod u+x backup.sh to make it into an executable command.

The file backup_excludes.txt contains a list of files to exclude. An example is shown below — this list can be used as the default set of directories that should not be backed up which you can add to as needed.

/tmp/*
/Network/*
/cores/*
*/.Trash
/afs/*
/automount/*
/private/tmp/*
/private/var/run/*
/private/var/spool/postfix/*
/private/var/vm/*
/Previous Systems.localized
.Spotlight-*/

Tips