Sync folders when drive is plugged in.

[UPDATE] Didn’t realize how dangerous this is if you actually need to get a backup file(s) off the drive rather than restore from catastrophic failure. Now prompts to confirm sync before proceeding. Should also prompt to eject when complete.

The previous AppleScript post is an earlier version of nearly the same thing. I posted it here to be able to write a new script for where I work. Its intention is to keep an on-site duplicate of an off-site backup. This can of course be used for simple backups and/or folder syncing operations if given the appropriate trigger(s).

To have this script run automagically attach it as a Folder Action to the Volumes folder (/Volumes).

Also, as the previous post, this script calls an rsync binary in a custom install location. Please be aware of that.

Because I can: rsync manual

-- The name of the volume to watch for.
property trigger : "Backup"

-- The paths of the folders to sync. Drag-n-drop supported (choose "Alias").
property theFolders : {"/Do not/forget to/add quotes around", "/And/commas between/items"}

-- rsync options. I use '-rtE' (recursive, preserve times, copy Extended attributes/resource forks)
property rsync_options : "-rtE --delete" -- For mirroring the contents of source dir.
-- property rsync_options : "-rtE --ignore-existing" -- For keeping deleted files on backup. WARNING: also dupes files moved within the source dir.
-- See the rsync manual (man rsync) for more info.

property skip : {} -- Leave empty. Meant to hold items from theFolders which gave rsync an error so we can start over but ignore items in this list.

on adding folder items to this_folder after receiving added_items
  try
    repeat with i in added_items
      if name of (info for i) is trigger then
        if the result of (display dialog trigger & " drive connected. Would you like to sync available source folders now?" giving up after 600) is not false then
          do_it(skip)
          do shell script "logger 'Sync backup folders: Complete'"
          ejectit(trigger)
        end if
        exit repeat
      end if
    end repeat
  on error theErr
    do shell script "logger 'Sync backup folders: " & theErr & "'"
  end try
end adding folder items to

on run
  do_it(skip)
  do shell script "logger 'Sync backup folders: Complete'"
  ejectit(trigger)
end run

on ejectit(trigger)
  if the result of (display dialog "Backup sync complete. Eject drive '" & trigger & "'?") is not false then
    tell application "Finder" to eject trigger
  end if
end ejectit

on do_it(skip)
  tell application "System Events" to set trigger_path to POSIX path of every disk whose name is trigger -- When a drive is mounted and its previous mount point was not removed the new mount point will be given a unique number to follow the name (which isn't shown in the Finder), e.g. "/Volumes/Backup 1". This asks the System for the path rather than us blindly using text.
  if trigger_path is {} then
    display dialog "The drive '" & trigger & "' does not appear to be mounted. :("
    return
  end if
  set rsync_options to rsync_options & " "
  repeat with i in theFolders
    tell application "Finder"
      if not (exists (i as POSIX file)) then
        set end of skip to i as text
      end if
    end tell
    log skip
    if i is not in skip then
      try
        do shell script "/usr/local/bin/rsync " & rsync_options & quoted form of i & " '" & trigger_path & "/'"
        set end of skip to i as text
      on error the_err
        if i is last item in theFolders then
          display dialog "rsync encountered a problem: " & the_err
        else
          if button returned of (display dialog "rsync encountered a problem: " & the_err & return buttons {"Cancel", "Continue"}) is "Continue" then
            set end of skip to i as text
            do_it(skip)
          end if
        end if
      end try
    end if
  end repeat
  set skip to {}
end do_it

This script includes error handling that isn’t well tested. Getting it to work with it at all was a bit of a trick (particularly if not (exists (i as POSIX file)) since if not (exists POSIX file i) does not work as expected). So that whole “skip” idea may not work as intended if you are asked to continue. The last do_it(skip) call may need to be my do_it(skip) instead.

Leave a Reply

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