Automatically backup Address Book once a day

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

After a couple of time consuming snafus with Address Book (one involving the "lovely" 10.5.3. update that allowed Mac to sync Address Book with Gmail contacts..grrrr...) I decided to get serious about backing up my address book so I wrote the following applescript for Hazel to run:
Code: Select all
on hazelProcessFile(infile)
   do shell script "date +%Y-%m-%d"
   set todaysDate to result
   set fileName to "AddressBookArchive-" & todaysDate --& ".abbu" AB will auto-add the ending, if you have default Hide Endigs enabled in Finder!
   --do shell script "echo $USER"
   --set userName to result
   
   tell application "Address Book" to activate
   tell application "System Events"
      tell process "Address Book"
         click menu item "Address Book Archive…" of menu 1 of menu item "Export" of menu 1 of menu bar item "File" of menu bar 1
         delay 1
         keystroke fileName
         keystroke "d" using command down -- Select Desktop
         keystroke return
         delay 5 -- Allow AB to save the backup
      end tell
   end tell
   
   --do shell script "mv -f /Users/" & userName & "/Desktop/" & fileName & " /Users/_myname_/Backup/."
   
   tell application "Address Book"
      quit
   end tell
end hazelProcessFile


I should note that one downfall of this script is that it requires Address Book to actually open, which can be a little surprising the first couple of times it runs.

This script makes an address book archive with the following name format: AddressBookArchive-2008-06-21, where the last bit is the date. It seems to save to the Desktop and, despite my efforts to fix this (see the commented out "do shell script" line), I can't fix it.

Using Hazel, I set up the following rules:
On Applications: if date last matched is not in the last 1 day and Name is Address Book, run AppleScript named MakeAddressBookArchive.scpt
On Desktop: if name starts with AddressBookArchive- and kind is Address Book Archive File, move file to folder AddressBookBackup

For my ~200 contacts, these files are about ~4MB, so they can easily add up. I am considering adding a rule to delete old files from the AddressBookBackup file. My concern is that I won't notice an address is missing until a while later.

Ideally, I would prefer to export everything as a multiple VCard file since this is an open format. Getting the applescript program to "select all addresses" is a little tricky. I can't get the UI scripting to work properly. Booo...

EDIT: this script was adapted from the following link http://www.macosxhints.com/dlfiles/auto_ab_backup_scpt.txt. I removed some the the UI checking since I have it enabled for other reasons. You DO need to enable access for assistive devices.
jayhawkbabe
 
Posts: 17
Joined: Sun Jun 15, 2008 10:39 am

Re: Automatically backup Address Book once a day Wed Jun 25, 2008 11:55 am • by sjk
Maybe I'm overlooking something, but wouldn't it be easier just having Hazel copy the ~/Library/Application Support/AddressBook folder to a backup location?
sjk
 
Posts: 332
Joined: Thu Aug 02, 2007 5:43 pm
Location: Eugene

You sure can do that, and I almost mentioned it. (There was actually a discussion about it on the web page that I stole this script from.) I am not a fan of copying the data directory (even if tarring and zipping it beforehand) because I have had the index in this directory get corrupted somehow, which renders the directory useless for backups without you even knowing it. In my thinking, exporting the entire address book is safer in this regard and will at least give you an error if a problem does occur.

If only there were a way to make the export happen in the background... I might have to look into that.
jayhawkbabe
 
Posts: 17
Joined: Sun Jun 15, 2008 10:39 am

Re: Automatically backup Address Book once a day Wed Jun 25, 2008 5:19 pm • by sjk
Thanks for the explanation.

Do you happen to know the topic and/or date of the MacOSXHints article where the original script you linked to came from?
sjk
 
Posts: 332
Joined: Thu Aug 02, 2007 5:43 pm
Location: Eugene

It's here: http://www.macosxhints.com/article.php?story=20051122133402732&query=auto_ab_backup_scpt

I just searched for the file name "auto_ab_backup_scpt". I meant to post this link as well, but got in a hurry. Cheers.
jayhawkbabe
 
Posts: 17
Joined: Sun Jun 15, 2008 10:39 am

Re: Automatically backup Address Book once a day Wed Jun 25, 2008 5:30 pm • by sjk
Good idea searching for the script name; I could have thought to try that before asking. :)
sjk
 
Posts: 332
Joined: Thu Aug 02, 2007 5:43 pm
Location: Eugene

No worries. It only occurred to me when I was responding to your email. If it had occurred earlier, I would have already posted it. Good tip to know though--I have a ton of these script URLs clipped and sometimes wonder where they came from!!!
jayhawkbabe
 
Posts: 17
Joined: Sun Jun 15, 2008 10:39 am

For anyone who is interested, I have come up with a much better version of this script. This one doesn't rely on UI programming and, thus, can run in the background. The "breakthrough" occurred when I saw a script on a blog for creating the backup files from iCal (*.icbu) format and noted that they were nothing but the directory with an extension added and a plist file inside. By doing a little investigating of my own, I figured out that this was the same case with the Address Book backup files (*.abbu). (So, as someone noted earlier, you could just copy the address folder elsewhere and have that be your backup script.) Nonetheless, it was easy to take the original iCal script (see the link inside the code if you're interested in the iCal script) and convert it to Address Book.

With credits to John from BusyMac for the original iCal backup script.

I have tested briefly and it seems to work fine. Testing recommended and use at your own risk, of course.

Code: Select all
on hazelProcessFile(infile)
   -- Address Book Backup.applescript v1.0
   -- Original iCal version created by John Chaffee, BusyMac LLC, on Nov. 4, 2008
   -- http://blog.busymac.com/blog/2008/11/backup-ical-automatically-with-applescript-alarms.html
   -- Modified by Michelle Gill for Address Book on Dec. 3, 2008
   -- Creates an AddessBook backup file (*.abbu)
   --Script can be launched from Hazel for automatic backup
   
   --use first version of timeStamp for shorter file names if you only backup once a day
   --use longer one to preserve times
   set timeStamp to do shell script "/bin/date \"+%Y-%m-%d\""
   --set timeStamp to do shell script "/bin/date \"+%Y-%m-%d-%H-%M-%S\""
   
   tell application "Finder" to set myHome to (POSIX path of (path to home folder))
   
   -- Change this path if you want to store the backups somewhere else
   set destDir to myHome & "/Desktop"
   
   set fileName to "AddressBookArchive-" & timeStamp
   do shell script "/bin/mkdir -p " & quoted form of destDir & "/" & quoted form of fileName
   set destFile to destDir & "/" & fileName
   
   -- Create backup files: copy Address Book files from sourceDir to destFile
   set sourceDir to myHome & "/Library/Application Support"
   do shell script "/bin/cp -R " & quoted form of sourceDir & "/AddressBook/* " & quoted form of destFile
   set plistVers to "2.0"
   
   -- Need to calculate number of Persons and Groups in AddressBook for backup plist file
   set numPersons to do shell script "/bin/ls " & quoted form of sourceDir & "/AddressBook/Metadata/* | /usr/bin/grep Person | /usr/bin/wc -l | /usr/bin/sed 's/ //g'"
   set numGroups to do shell script "/bin/ls " & quoted form of sourceDir & "/AddressBook/Metadata/* | /usr/bin/grep Group | /usr/bin/wc -l | /usr/bin/sed 's/ //g'"
   
   -- Create plist file   
   set plistFile to destFile & "/BackupDetails.plist"
   set plistTime to do shell script "/bin/date -u \"+%Y-%m-%dT%H:%M:%SZ\""
   set plistContent to "<?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>ABBackupDate</key>
   <date>" & plistTime & "</date>
   <key>ABBackupGroupCount</key>
   <integer>" & numGroups & "</integer>
   <key>ABBackupPeopleCount</key>
   <integer>" & numPersons & "</integer>
</dict>
</plist>"
   do shell script "echo " & quoted form of plistContent & " > " & quoted form of plistFile
   
   -- Change destFile folder to .abbu suffix
   do shell script "mv -f " & quoted form of destFile & " " & quoted form of destFile & ".abbu"
   
   -- Uncomment to delete old backups
   -- Set the deleteAfter variable to the number of days to keep. Anything older will be deleted
   --set deleteAfter to 10
   --set myDir to POSIX file destDir
   --tell application "Finder"
   --   set myOldFiles to files in folder myDir where name contains ".abbu" and modification date is less than ((get current date) - deleteAfter * days)
   --   set myOldFileCount to count of myOldFiles
   --   repeat with myOldFile in myOldFiles
   --      delete myOldFile
   --   end repeat
   --end tell
end hazelProcessFile
Last edited by jayhawkbabe on Fri Dec 05, 2008 4:56 pm, edited 1 time in total.
jayhawkbabe
 
Posts: 17
Joined: Sun Jun 15, 2008 10:39 am

And if you copy and paste the above text, this should all be one line:
Code: Select all
   --   set myOldFiles to files in folder myDir where name contains ".abbu" and modification date is less than ((get current date) - deleteAfter * days)
jayhawkbabe
 
Posts: 17
Joined: Sun Jun 15, 2008 10:39 am


Return to Tips & Tricks - DO NOT POST QUESTIONS

cron