Synchronize two folders on a Mac with Automator and Rsync
Power of Automator and Unix combined
I really miss some good Automator tutorials online. It's such a great and I guess underrated and underused tool. Here I'll show you how to combine it with rsync to easily synchronise two folders.
Here is the finished workflow from the tutorial so you can use it right away and see how it's done for yourself: RsyncTutorial.workflow.zip 152kb
And here it is for the patient ones, step by step...
You'll start by opening automator.app, you can find it in your Application folder it has a nice icon which reminds me of eve from Wall-E. Once you start it, it will ask you what kind of workflow you're going to make, we'll choose Workflow:
First action we're going to add is "Ask for Finder Items" (it's inside Files & Folders category, but best way to find actions is to use search), that action simply opens finder window where you can choose file(s) or folder(s).
And here we can write some custom text in "Prompt" field, you can specify which folder will it open as a starting point (Start at), and we'll specify that we want only folders and we do not want multiple selection option.
Next we'll add "Set Value of Variable" action which will get the folder that you chose and write it's name into a variable that we'll use later with Rsync.
Next, we'll repeat it, we need another pair of Ask for Finder Items and Set Value of Variable actions. First we add Ask for Finder Items:
But the important thing here, is to ctrl + click (right click) on the Ask for Finder Items text and choose "Ignore Input" option, if you do not do this the whole thing will not work. Then we'll add Set Value of Variable:
And we'll click on Variable drop down and choose New variable and name it for example "target_folder".
Next step is optional, we'll add confirmation window (Ask for Confirmation action) that will ask us if we chose the right folders and print out the names of the folders we're going to sync:
Here you start writing something like "You'll be syncing from" than you drag the variable "source_folder" from variable list below, continue by writing " to " and than drag "target_folder" variable from the list, and finish your confirmation text, for example " is that right?".
Here you're also have to say to automator that you want to ignore input from previous action.
Next we'll add two Get Value of Variable actions that will pick up the values of source and target variables:
Again, ignor the input from the Ask for Confirmation action.
After that, add Run Shell Script action which will tell rsync what to do:
You have to change "Pass input" to "as arguments" and than write rsync -va --delete "$1/" "$2/" $1 represents first variable that you're sending in, and $2 second one (first one being source_folder and second one target_folder). You can play with rsync options here and maybe exclude some files from syncing.
And here is another optional action, we'll add New TextEdit Document at the end. That will open new TextEdit window with output from Rsync, so you can see what Rsync actually did.
And that's it! Now we're going to save it, the best way I think is to save the workflow into your Scripts folder (you go to your home folder and there you'll find Scripts folder). I named mine workflow "RsyncTutorial". Then if you have Script Menu turned on in menu bar (If you don't you can enable it by running AppleScript Utility, located at /Applications/AppleScript/, and checking the "Show Script Menu in menu bar" checkbox) you'll see something like this when you click on the Script Menu:
And if you click on it your workflow will start by asking you for folder to sync from.
Recent posts
- Thursday, 21. january 2010. Find files larger than X and delete them in Mac OS/Unix terminal
- Tuesday, 15. december 2009. Bananica.com - Finally!
- Monday, 14. december 2009. MySQL Find and Replace with helper tool that generates SQL for you
- Monday, 14. december 2009. Synchronize two folders on a Mac with Automator and Rsync
- Monday, 14. december 2009. Synchronize two folders on a Mac and other Unix Systems with Rsync
- Sunday, 13. december 2009. Add all new files and folders to subversion with one line in terminal
Recent comments
-
Wednesday, 24. february 2010. karol commented on Add all new files and folders to subversion with one line in terminal Great! However, this doesn't work if your file names contain spaces.
I managed to compile several snippets from the net and came up with this:
svn status | grep "^?" | awk '{$1="";$0=substr($0,2)}1' | sed -e "s,[^.],\'&," -e "s,\$,\'," | xargs svn add
Phew! Works in Snow Leopard. - Tuesday, 23. february 2010. eMancu commented on Synchronize two folders on a Mac and other Unix Systems with Rsync Thanks a lot! I was looking for --delete example :)
-
Wednesday, 13. january 2010. annmarie commented on Ambium Ja, meni je moja spletna stran noro dobra. Na svetovnem nivoju, ni kaj.