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 \ AddressBook/
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" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.paulcalnan.backupaddressbook</string> <key>ProgramArguments</key> <array> <string>/Users/paul/AddressBookBackups/backup-address-book.sh</string> </array> <key>StartCalendarInterval</key> <dict> <key>Hour</key> <integer>0</integer> <key>Minute</key> <integer>0</integer> </dict> </dict> </plist>
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.
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:
- Quit the Contacts app (formerly known as Address Book)
- Copy the latest backup file to
- Rename the
~/Library/Application Support/AddressBookdirectory to
- Unpack the latest backup file (using
tar xjf FILENAME)
- Restart Contacts and see if this fixed your problem