Hopefully most our readers here have a good backup routine in place. After learning the lesson the hard way, I now I use an obsessive combination of a cloud-based system (I use SpiderOak, which we introduced here and here), time machine, the mirroring of important files between two computers with the help of SpiderOak, regular backups to a hard drive and more irregular backups to a medium-term storage medium. This, in turn, is deposited at a safe location in a country not likely to be destroyed in a nuclear apocalypse at the same time as the country I live in.
One common task needed for this variety of backup procedures is the syncing of a folder, especially when I back up to my external hard drive. I have tried lots of different folder syncing apps over the years, many of which can now be found on the App Store, but I’ve noticed that, since the coming of OS X, some leading sync apps (such as Carbon Copy Cloner) are running a simple open source command line utility to perform their task behind the scenes. Today I want to introduce one of these that I have recently started using: rsync. Rsync has been around since the late 1990s as a way to synchronize folders from the command line (Read Lincoln’s introduction here) of Unix and Mac OS X. If you use a Mac, rsync should be already installed.* When used for uni-directional sync (I won’t cover bi-directional sync here), it copies folders and files from a source location to a destination, and optionally can remove files that no longer exist at the source from the destination.
Please use this at your own risk. Try it first on a test file or folder first, for example, so you see what it does.
To keep a collection of my primary sources synced with a folder on my backup drive I open up my Terminal application in the Applications->Utilities folder and enter:
rsync -a -v --delete /Source/BackMeUp/ /Volumes/BackupDrive/BackMeUp
Tip: Drag a folder from the Finder and drop it onto the Terminal to find a folder path easily but add the trailing “/” in the source.
Why is this better than simply dragging the folder in the Finder to the destination location on my backup drive? Well, using a command like rsync only copies the things I need over (new or modified files), and only deletes the files I no longer need. It can change a 20 minute copy-and-replace operation into a 2 minute one if you have many gigabytes of files that have only a few files that need updating.
Let me explain the command in a bit more detail. You can read this yourself in the manual for the command if you type “man rsync” at the command line, but briefly, “-a” is “archive mode” which is explained in this great MacLife overview of rsync found here. It preserves timestamps and permissions on your files. The “-v” option increases the verbosity of the command. I like to know more about what is going on when the command is run. The “–delete” command, which looks frightening, is used to delete files in the destination folder that no longer exist in the source. If you change the name of a file in the source folder and run the command without –delete, for example, you will get both a file with the old name and the new name in the destination folder, and any files you have deleted will remain as ghosts in the backup destination folder.
Now, obviously, I am in no mood to type this command every time I want to perform my backup. Hackers among you who spend a fair bit of time on the command line may want, at this point, to add an alias for this backup routine to the .bash_profile file that lives in your home folder or schedule a regular backup using launchd or cron. However, for those of you who don’t spend much time hanging out in your Terminal, you can easily save your backup routine by creating an Automator application:
1) Open the “Automator” in your Applications folder
2) Create a new “Application”
3) In the search bar on the left find “Run Shell Script” and drag-and-drop this into the blank pane on the right.
4) In the text box for the code (it may say “cat” as an example, delete that text) enter the rsync command you used for your backup (Again! Please use with care and test it in the command line with a test folder so you see what the command does)
5) I like to see the output of the command, so I drag-and-drop another “New TextEdit Document” command under the “Run Shell Script” command so that the output of rsync will get dumped into an empty text file, that opens upon completion of the sync.
Save this application and then you simply run it every time you want to perform your backup from that particular source folder to a destination (which can be an external drive).
Have you ever used a command line sync utility like rsync or ditto? Are there other free or commercial folder sync applications you like?
* I believe Mountain Lion comes with the outdated version 2.6.9. I have seen notes here and there that the newer 3.0.x versions are better at capturing some file attributes (see here, for example) and resource forks on old pre-OS X files. I haven’t experienced any problems without making this update, but if anyone understands this better, please share.
by Tim Morgan