On February 12, 2015 In Blog, Tech, Tutorials

RSync (MacOSX) & Automator & Terminal

Following the creation of my HandBrake CLI automator tutorial, here comes another useful Automator application/workflow/action for backing up photos and/or other uses using RSync.

I did not write the code by myself but made use of many online resources to get me through and thought I’d share this workflow with everyone who might run into similar problems.

Useful scenarios

  • Depending on your backup workflow: Backup current working Lightroom photos/catalog onto another folder, making sure all changes are synced/copied over. (Those .XMP files… grrr)
  • Backup & Update iTunes library (Good luck finding all your songs that you’ve added recently)
  • General backup/sync of any folder (with/without –delete option)

At the end of the post I will attach the automator .app , you can download and start using it.

Running on Mac 10.10.2

Rsync via the Terminal

Terminal with the rsync command

Terminal with the rsync command

rsync -avPi --delete "/source/" "/target/"

Straight forward, you probably have successfully ran the command & started syncing but is looking for a way to not “type” the folders into the Terminal (you can drag and drop from Finder into Terminal FYI)

Note:

--delete

Adding this option will delete files in your “Target” folder that are not in the “Source” folder. In other words, it makes the same exact copy of your “Source” folder onto “Target”. If you want to keep adding or update newer files, remove the “–delete” option to become

rsync -avPi "/source/" "/target/"

Rsync with automator – no output (Shell script)

Time to plug it into Automator!

I followed a tutorial online and you can visit the link below, my automator app is pretty much the same but with some Growl notifications.

Credit to:

Synchronise two folders on a Mac with Automator and Rsync

Problem:

The above method works, but it does not show any output, just the usual Automator logo spinning in the menu bar. Not really helpful if you are syncing huge data and want to see some kind of output status, just like the plain Terminal command above.

Rsync with Automator via AppleScript (Terminal)

Since AppleScript can “run” applications, I thought I will just run the Terminal with the rsync command.

Most of the code is the same as the Shell script version, just replace the Shell script action with an AppleScript action

Here is the working screenshot of the AppleScript code with comment markings

screenshot of the AppleScript code with comment markings

screenshot of the AppleScript code with comment markings

(1) (2) – The most important code to get Applescript to run Terminal app and run the Rsync command on it

Credits:

accessing multiple Automator variables in a Run Applescript action

Multiple Variables for Automator’s “Run AppleScript” action

Problem:

The path that returned from the Automator and into the Terminal was given in the form of Volume:dir:dir , and Terminal doesn’t like this at all!

(3) Remember about the POSIX path playing around previously with the Automator… Converts the Volume:dir:dir to “\Volume\dir\dir”

Working Rsync Automator AppleScript action

Working rsync applescript

Working rsync applescript

Problem solved~ The Rsync app now sits in my dock and I can call it anytime to sync preferred folders. If you want Rsync to add and/or update newer files without deleting existing files in your “Target” folder, remove the ” –delete” option from your code as described above. Or create two versions 🙂

You can download the app here in both versions with –delete

Download at your own risk! I will not be responsible for any data loss

Happy RSyncing! (comments would be nice if you find it useful 😉 )

RSync Automator AppleScript action with Options (Update)

Rsync Automator Applescript with Dialog

Rsync Automator Applescript with Dialog

Updated the rsync automator workflow. Instead of having two automator apps, you can now choose to either do a exact copy (–delete) or update only (without –delete), or Abort via a dialog.

The updated AppleScript

on run {input, parameters}
set source_folder to first item of input
set target_folder to second item of input
set source_folder to POSIX path of source_folder
set target_folder to POSIX path of target_folder
set question to display dialog "Option: Exact Copy(--delete) or Copy Updates" buttons {"Abort", "Exact", "Update"}
with title "Rsync Automator Options" default button 2
set answer to button returned of question
if answer is equal to "Abort" then
end if
if answer is equal to "Exact" then tell application "Terminal" activate set currentTab to do script "rsync" & " -avPi --delete " & quoted form of source_folder & " " & quoted form of target_folder
end tell
end if
if answer is equal to "Update" then tell application "Terminal" activate set currentTab to do script "rsync" & " -avPi " & quoted form of source_folder & " " & quoted form of target_folder
end tell
end if
end run

Download at your own risk! I will not be responsible for any data loss

AppleScript version with Options dialog

Scroll Up