Paul Calnan
Published August 12, 2012

Occasionally, I hear horror stories about iCloud screwing up a synced address book. Although I have another backup system in place, I decided I wanted to make an automated script that could back up just my address book.

The script to run the actual backup is fairly simple. It creates a tar.bz2 file with the contents of the ~/Library/Application Support/AddressBook/ directory. The script puts the tar.bz2 archive in ~/AddressBookBackups, but you can change it to wherever you'd like your backups to go.

#!/usr/bin/env bash

tar -C $HOME/Library/Application\ Support/ -cjf \
    $HOME/AddressBookBackups/AddressBookBackup-$(date +%Y-%m-%d).tar.bz2 \

Now, for the automation part, I wrote a Launch Agent file that schedules the script to be run nightly at midnight.

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

You can change the Label (and the filename) from com.paulcalnan... to whatever you prefer. You should change the path to the program from /Users/paul/AddressBookBackups to wherever you have the script stored.

I have an OS X server running 24/7, so I'm able to schedule this there and it runs the backup for me without having to think about it. If don't have a computer that's always on, launchctl takes care of running the scheduled script even if the scheduled time is missed. From the launchctl.plist man page:

Unlike cron which skips job invocations when the computer is asleep, launchd will start the job the next time the computer wakes up. If multiple intervals transpire before the computer is woken, those events will be coalesced into one event upon wake from sleep.

Place the plist file in ~/Library/LaunchAgents then run the following to get launchctl to schedule the job:

launchctl load ~/Library/LaunchAgents/com.paulcalnan.BackupAddressBook.plist

Note that this setup will keep making new backup files every day. I'll leave it as an exercise to the reader to write a script that cleans up old backup files.

In the event that you want to restore from backup, you can do the following: