Archived Linux Mac & OS X Uncategorized

Using rsync to synchronise folders

I keep most of my primary files on my iMac. Needless to say, this would be a pretty dumb move if I kept them there, and there only. However, I’m a paranoid freak when it comes to data, having lost 7 years worth of work in 1 go a few years back – I’m not letting that happen again. I use an Apple Time Capsule and Apple Time Machine for my primary backups and it’s saved me once or twice already.


Anyway, because I also have a Macbook Pro, that means I’m sometimes working where my iMac isn’t. I needed a solution to synchronise a specific set of folders on my iMac and my Western Digital portable USB drive. There are a ton of ways to do this but because I’m a geek I chose to use the command line (Terminal, in OS X) and write a small script. I’m using rsync for this – here’s the script, should you need something like it yourself.

The Script

Synchronise with rsync:


# sync the files
rsync ~/Data/Solutions /Volumes/Backup --recursive --verbose --delete --progress --human-readable --exclude="tmp*"

# unmount the external drive
sudo diskutil unmount /Volumes/Backup

What does it do?

Simple! The script script above does the following.

  • Synchronises all files in the sub-folder Data/Solutions in my home folder onto my USB drive (called ‘Backup’). All files matching the file pattern tmp* are skipped.
  • Ejects the USB drive when finished.
  • All sub-folders are included.
  • Files that exist in the destination but not in the source are deleted.
  • Progress of each file is displayed as it is copied.
  • All output is in human-readable format … because I’m human.

The eject part prompts for my password before ejecting because the diskutil unmount command is a privileged operation and requires elevated rights before it will be allowed,

Simple, but useful. 🙂