{"id":721,"date":"2018-06-21T22:16:42","date_gmt":"2018-06-22T05:16:42","guid":{"rendered":"http:\/\/10.0.1.201\/?p=721"},"modified":"2018-06-21T22:16:42","modified_gmt":"2018-06-22T05:16:42","slug":"sync-folders-when-drive-is-plugged-in","status":"publish","type":"post","link":"https:\/\/strawhousepig.net\/wordpress\/2018\/06\/21\/sync-folders-when-drive-is-plugged-in\/","title":{"rendered":"Sync folders when drive is plugged in."},"content":{"rendered":"<p><ins datetime=\"2018-09-06T18:47:19+00:00\">[UPDATE] Didn&#8217;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.<\/ins><\/p>\n<p>The <a href=\"http:\/\/strawhousepig.net\/auto-backup-of-photos-library-photoslibrary-when-portable-drive-is-attached\/\">previous AppleScript post<\/a> 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).<\/p>\n<p>To have this script run automagically attach it as a Folder Action to the Volumes folder (\/Volumes).<\/p>\n<p>Also, as the previous post, <strong>this script calls an rsync binary in a custom install location<\/strong>. Please be aware of that.<\/p>\n<p>Because I can: <a href=\"x-man-page:\/\/rsync\">rsync manual<\/a> <\/p>\n<p><code>-- The name of the volume to watch for.<br \/>\nproperty trigger : \"Backup\"<\/p>\n<p>-- The paths of the folders to sync. Drag-n-drop supported (choose \"Alias\").<br \/>\nproperty theFolders : {\"\/Do not\/forget to\/add quotes around\", \"\/And\/commas between\/items\"}<\/p>\n<p>-- rsync options. I use '-rtE' (recursive, preserve times, copy Extended attributes\/resource forks)<br \/>\nproperty rsync_options : \"-rtE --delete\" -- For mirroring the contents of source dir.<br \/>\n-- property rsync_options : \"-rtE --ignore-existing\" -- For keeping deleted files on backup. WARNING: also dupes files moved within the source dir.<br \/>\n-- See the rsync manual (man rsync) for more info.<\/p>\n<p>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.<\/p>\n<p>on adding folder items to this_folder after receiving added_items<br \/>\n\ttry<br \/>\n\t\trepeat with i in added_items<br \/>\n\t\t\tif name of (info for i) is trigger then<br \/>\n\t\t\t\tif 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<br \/>\n\t\t\t\t\tdo_it(skip)<br \/>\n\t\t\t\t\tdo shell script \"logger 'Sync backup folders: Complete'\"<br \/>\n\t\t\t\t\tejectit(trigger)<br \/>\n\t\t\t\tend if<br \/>\n\t\t\t\texit repeat<br \/>\n\t\t\tend if<br \/>\n\t\tend repeat<br \/>\n\ton error theErr<br \/>\n\t\tdo shell script \"logger 'Sync backup folders: \" & theErr & \"'\"<br \/>\n\tend try<br \/>\nend adding folder items to<\/p>\n<p>on run<br \/>\n\tdo_it(skip)<br \/>\n\tdo shell script \"logger 'Sync backup folders: Complete'\"<br \/>\n\tejectit(trigger)<br \/>\nend run<\/p>\n<p>on ejectit(trigger)<br \/>\n\tif the result of (display dialog \"Backup sync complete. Eject drive '\" & trigger & \"'?\") is not false then<br \/>\n\t\ttell application \"Finder\" to eject trigger<br \/>\n\tend if<br \/>\nend ejectit<\/p>\n<p>on do_it(skip)<br \/>\n\ttell 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.<br \/>\n\tif trigger_path is {} then<br \/>\n\t\tdisplay dialog \"The drive '\" & trigger & \"' does not appear to be mounted. :(\"<br \/>\n\t\treturn<br \/>\n\tend if<br \/>\n\tset rsync_options to rsync_options & \" \"<br \/>\n\trepeat with i in theFolders<br \/>\n\t\ttell application \"Finder\"<br \/>\n\t\t\tif not (exists (i as POSIX file)) then<br \/>\n\t\t\t\tset end of skip to i as text<br \/>\n\t\t\tend if<br \/>\n\t\tend tell<br \/>\n\t\tlog skip<br \/>\n\t\tif i is not in skip then<br \/>\n\t\t\ttry<br \/>\n\t\t\t\tdo shell script \"\/usr\/local\/bin\/rsync \" & rsync_options & quoted form of i & \" '\" & trigger_path & \"\/'\"<br \/>\n\t\t\t\tset end of skip to i as text<br \/>\n\t\t\ton error the_err<br \/>\n\t\t\t\tif i is last item in theFolders then<br \/>\n\t\t\t\t\tdisplay dialog \"rsync encountered a problem: \" & the_err<br \/>\n\t\t\t\telse<br \/>\n\t\t\t\t\tif button returned of (display dialog \"rsync encountered a problem: \" & the_err & return buttons {\"Cancel\", \"Continue\"}) is \"Continue\" then<br \/>\n\t\t\t\t\t\tset end of skip to i as text<br \/>\n\t\t\t\t\t\tdo_it(skip)<br \/>\n\t\t\t\t\tend if<br \/>\n\t\t\t\tend if<br \/>\n\t\t\tend try<br \/>\n\t\tend if<br \/>\n\tend repeat<br \/>\n\tset skip to {}<br \/>\nend do_it<\/code><\/p>\n<p>This script includes error handling that isn&#8217;t well tested. Getting it to work with it at all was a bit of a trick (particularly <code>if not (exists (i as POSIX file))<\/code> since <code>if not (exists POSIX file i)<\/code> does not work as expected). So that whole &#8220;skip&#8221; idea may not work as intended if you are asked to continue. The last <code>do_it(skip)<\/code> call may need to be <code>my do_it(skip)<\/code> instead.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[UPDATE] Didn&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,23,24,28],"class_list":["post-721","post","type-post","status-publish","format-standard","hentry","category-code","tag-applescript-2","tag-macintosh","tag-macos","tag-os-x"],"_links":{"self":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/comments?post=721"}],"version-history":[{"count":0,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/721\/revisions"}],"wp:attachment":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/media?parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/categories?post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/tags?post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}