Google
 

7/4/08

HOWTO: Backup Your Mac With rsync

About This Document

  • Created: 26 January 2005
  • Updated: 18 December 2005: added note for Mac OS X 10.4 and general clean up.
  • Updated: 18 February 2006: added note about using -x switch to reduce need for specified excludes. From suggestion by Mike Bremford.

Author: Matthew Phillips

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

  • The "Remove Unmatched" checkbox tells rsyncx to delete any files on the external drive that have been deleted since the last backup. Coupled with Archive mode (which preserves all file modification times, ownerships, etc), this will produce an exact copy of the drive.

    Note: this option will also mean that any files you may have accidentally deleted will also be deleted from the backup too, making it possible to lose a file permanently if you don't notice its deletion in time.

  • Although it's not obvious, you can resize the window by dragging the right corner which displays more than one "Source" path. You can use this to select several specific folders rather than the whole drive.
  • If you're using FileVault to encrypt your home folder, you may want to consider backing up the system from another account. This is because if you run the backup while you are logged in, the backup process will copy your unencrypted home folder's contents.

    The simplest way to do this is to create a special "Backup" user and, when you do a backup, log off from your account and log back in as Backup to run the backup process. This will result in your home folder also being encrypted in the backup (it appears as a single, large, encrypted .sparseimage file). The one downside of doing it this way is that you'll also need to grant the backup user admin rights as described in the advanced section.

    In fact running the backup while logged in as another user can be a good idea even if you're not using FileVault, since it reduces the likelihood that processes will modify files while they're being copied.

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

  • You can run the backup script with the -n option to test it. In this mode rsync will run through the motions but not make any actual changes. Adding -v will cause rsync to print out all the files it will be changing.
  • If you're running the backup as the "backup" user as recommended in a previous tip, you will need to grant the backup user the ability to run the rsync and bless commands as root. To do this you need to run "sudo visudo" as an admin user and add the lines highlighted in green to make it look like the example below.
    # sudoers file.
    #
    # This file MUST be edited with the 'visudo' command as root.
    #
    # See the sudoers man page for the details on how to write a sudoers file.
    #

    # Host alias specification

    # User alias specification

    # Cmnd alias specification
    Cmnd_Alias BACKUP = /usr/bin/rsync, /usr/local/bin/rsync, /usr/sbin/bless

    # Defaults specification

    # User privilege specification
    root ALL=(ALL) ALL
    %admin ALL=(ALL) ALL
    backup ALL=BACKUP
  • The fact that the command line version of rsyncx has the same name as the built-in rsync carries the danger that you may not be using the right one. The standard rsync that comes with Mac OS X is in /usr/bin/rsync and, since /usr/local/bin is usually used before /usr/bin, the extended rsync should be what you get. If you want to be sure, run "rsync 2>&1 | head -n 3" and if you see "HFS+ filesystem support for OSX (C)2004 Kevin A. Boyd" as the last line, then you're using rsyncx.