Sunday, August 26

Automated on-demand backups with OS X

Update #2: Although the solution below is free and easy and relatively reliable, I eventually broke down and just purchased SuperDuper.

Update #1: In my haste, I forgot some important tidbits of information, such as where you put all these files...

I just [finally] got myself an external USB hard drive (Western Digital 160GB Passport) and I wanted to set up my Macbook Pro so that whenever I connected the drive to the laptop, the laptop would automatically back itself up, then give me a report of the backup and unmount the drive so I could immediately unplug it and put it away. That's how lazy I want to be about backing up.

So I scoured the 'net and finally figured out how to "git 'er done" just the way I like, thanks primarily to this old post. I'm posting this primarily for my own benefit because I know I'll have to do this again in the near future and I won't remember how I did it. So here's how it goes...

First of all you need to write yourself a fancy backup script (most comments are from the original author, not me) and place it in "~/Library/Scripts". I named my script "" to conform to the example in the original article:


# Borrowed (and BASH'd) from...

# Convenience variables to specify what I want to
# backup and where I want to back it up to

VOLUME="/Volumes/WD Passport"

# This sleep timer has been added to allow enough
# time for the system to mount the external drive
# On my PowerBook 30 sec. is more than enough time

sleep 20

# This check is added to test for cases when we are
# removing a drive from /Volumes or if the drive failed
# to mount in the first place

if [ ! -e "$VOLUME" ]; then
echo $VOLUME not mounted;
exit 0;

# Create the folder to back up the data to (defined above)

if [ ! -e "$DEST" ]; then
echo $DEST does not exist;
exit 0;

# Copy the files over. Here we are using rsync.


rsync --verbose --archive --modify-window=1 --exclude-from="$EXCLUDES" "$HOME" "$DEST" > "$RESULTS"
# --delete --delete-excluded

# Optionally, once the rsync is done, unmount the drive.

hdiutil detach "$VOLUME" >> "$RESULTS"

# Display the results


exit 0

Then you need yourself a "plist" file to tell OS X's "launch control" [launchctl] service to (a) watch for the drive to be mounted [plugged in], and (b) run the aforementioned script when it is. This file goes in "~/Library/LaunchAgents" and in my case is named "net.rudiment.backup":

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \

Now as the original article mentions, all you have to do is log out and log back in for this trigger to activate, but you can do it manually (and I did) like so:

Teflon-Ted:~ ted$ launchctl list
Teflon-Ted:~ ted$ launchctl load ~/Library/LaunchAgents
Teflon-Ted:~ ted$ launchctl list

That's it! You're golden.

And also for the sake of posterity, here's the contents of the "exclude" file referenced in the script:

iPod Photo Cache/

No comments: