Auto-backup of Photos Library.photoslibrary when portable drive is attached

This script relies on Folder Actions to fire it once it is attached to /Volumes and the appropriately named hard drive is plugged in. I use it to update my off-site photo backup drive.

Settings in the script are commented with instructions, which are hopefully clear enough. It does mention iPhoto, because it was written when iPhoto was a thing. A good thing, a great thing even. Now… *meh*

The options for rsync are fairly standard (recursive, preserve times, extended attributes, and skip files already on the backup). The path to rsync is from OS X shipping with a version of rsync that was unbearably slow and me installing a newer version. I’m not sure when Apple ditched the older rsync version, but it sure wasn’t soon enough. I’m leaving it here for example purposes, but this script will fail if you don’t have an rsync binary there.

-- The name of the physical disk.
property top_drive : "Portable HDD"

-- The name of the disk image file.
-- If it is beyond root of top_drive, the (non-POSIX) path will need to be added. ie "backups:iPhoto backup.dmg"
property iPhoto_dmg : "Photos backup.sparseimage"

-- The name of the disk image drive.
property iPhoto_drive : "Photos backup"

-- POSIX path to iPhoto Library (Default is ~/Pictures/iPhoto Library).
property iPhoto_lib : quoted form of (POSIX path of ((path to pictures folder) as text) & "Photos Library.photoslibrary")

-- rsync options
property rsync_options : "-rtE --ignore-existing"

-- Path to rsync binary.
property rsync_path : "/usr/local/bin/" -- Leave blank ("") if not using a user installed version of rsync.

on logErr(the_msg, the_num)
  do shell script "logger '" & "iPhoto backup: " & the_msg & " (" & the_num & ")'"
end logErr

on adding folder items to this_folder after receiving added_items
  set new_item to item 1 of added_items as text
  if new_item is (top_drive & ":") then
    tell application "Finder"
        open top_drive & ":" & iPhoto_dmg as alias
        delay 10
      on error errMsg number errNum
        my logErr(errMsg, errNum)
      end try
    end tell
    set n to 0
      set rsync_options to rsync_options & " "
      repeat until n = 10
        tell application "Finder" to set all_drives to name of every disk
        if iPhoto_drive is in all_drives then
          set start_time to current date
          set rsync_it to (do shell script rsync_path & "rsync " & rsync_options & iPhoto_lib & " " & quoted form of ("/Volumes/" & iPhoto_drive))
          set total_time to (current date) - start_time
          my logErr("Done", "" & total_time & " sec")
          exit repeat
          delay 6
          set n to n + 1
        end if
      end repeat
      tell application "Finder" to eject disk iPhoto_drive
      display dialog "iPhoto backup complete." & return & return & "You may now eject the disk "" & top_drive & ""." with icon 2
    on error errMsg number errNum
      my logErr(errMsg, errNum)
      display dialog "Oops! An error occurred. :(" & return & return & errNum & " : " & errMsg with icon 0
    end try
  end if
end adding folder items to

Leave a Reply

Your email address will not be published. Required fields are marked *