I'd like to automate my family backups, specifically I'd like to run a shell script (with rsync) when an external drive is connected.

Since it's a family thing, it should be also sort of GUI driven, maybe an icon like Dropbox. Probably a shell script with fswatch isn't really nice to play with, they would probably complain and certainly won't understand if an error occurs.

I was thinking about Automator or AppleScript (I have the GUI for the backup script in AppleScript already, but no automation), since "Folder Actions" isn't available for /Volumes, but any other solution is more than welcome.

Any suggestions?

  • Actually, a folder action on the /Volumes folder is exactly how I'd do it, and I achieved this successfully for the purposes of this answer here. It contains instructions on how to set up a folder action for /Volumes. If you want me to provide a specific answer for your question below, I can try and do this for you, or you can just follow my guide from the other answer and adapt it to your specific use case. – CJK Nov 14 '18 at 23:04
  • The other way to monitor the folder would be through a launchd plist, which can execute an AppleScript to perform the backup process if the drive that triggers the AppleScript is the right one (similar to the folder action in this respect). – CJK Nov 14 '18 at 23:14

Ive always done mine based on user login. You would go to Users & Groups click the current user go to Login Items hit the plus icon at the bottom and you can add it there. I built a similar application to detect my NAS and mount if not mounted:

enter image description here

Sample code, not tested:

on run
    tell application "System Events" to set theDisks to name of every disk
    set theNAS to "Shared" ## Change here to name
    set isMounted to false
    if theNAS is in theDisks then set isMounted to true
    if isMounted = false then mount volume "afp://" & theNAS
end run

Some links to mounting:

You could do through a LaunchAgent here are some resources:

There are other approaches like a launchd but you will sacrifice RAM so it can check. I think at login would be your best approach.

| improve this answer | |
  • 1
    A trimmed down equivalent AppleScript would be tell app "System Events" to if theNAS is in the name of every disk then mount volume "afp://" & theNAS, plus a preceding line to declare the variable theNAS. The introduction of an isMounted variable seems very verbose, and usually only serves to aid readability of a script, but seems to do the opposite here. I'd also recommend against posting untested code, although in this instance, your scripting brain nailed it successfully. – CJK Nov 14 '18 at 23:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .