Geek to Live: Mirror files across systems with rsync

by Gina Trapani



Why rsync?

"Ugh," you say. "Why the complicated command line? I've got my pretty clickety click SyncToy/FolderShare/SyncBackSE/FTP proggie right here which doesn't make me paw through screens of man pages to get it to work."


EverythingLinux's excellent rsync tutorial discusses some of these points in detail:


Interested? Good. Let's give it a whirl.

How to rsync

Rsync's installation, configuration and the switches you need will differ depending on the systems you're mirroring between. Today's simple example will use rsync from a Windows PC to mirror files that exist on a Mac.

Here's how:

1. If it isn't already, install the Unix emulator Cygwin on your PC, and be sure to check off the rsync and ssh packages when you do. Here's more information on Cygwin and adding packages to an existing Cygwin installation.

2. Connect your Mac to your PC - either over Firewire (as described here) or via your home network router. In your Mac's System Preferences' Sharing pane, enable Remote Login as shown:

Geek to Live: Mirror files across systems with rsync

Notice the IP address listed at the bottom of the pane in the message "To log in to this computer remotely, type 'ssh gina@'". Take note of your Mac's IP address (the 192 bit.)


rsync -avz --delete --exclude '.DS_Store' gina@ d:/data/mac

Let's break this command down. Here you're calling rsync using the -a, v, z options (archive, verbose and use compression, respectively). It should delete any files that exist on the destination that don't exist on the Mac (—delete), and it should exclude all files called '.DS_Store' (—exclude, because we don't need to backup the Mac's annoying little system files). The file source is /Users/gina/Documents on the Mac (addressed at, as specified in the Mac's System Preferences pane) and the destination is the d:/data/mac folder on the PC.

You must replace your source user@server:/file/path and destination path with your computers' details. Once you do and hit Enter, rsync will spring into action, and start transferring the files on the Mac and mirroring them in the destination directory on your PC. The -v switch (verbose) means you'll see all the action as it's happening.

Rsync switches


-r, --recursive             recurse into directories
    --existing              only update files that already exist on receiver
    --delete                delete files that don't exist on the sending side
-z, --compress              compress file data during the transfer
    --exclude=PATTERN       exclude files matching PATTERN

If you're just getting started with rsync, the -n ("dry run") switch with -v (verbose) is a great way to see what files would get copied without actually performing the copy. Use that switch to test out your rsync recipe before you run it.

Of course, once you get your rsync command just right, you can place it in a batch file or shell script and automate it using Windows Task Scheduler (or cron on the Mac or a Linux machine). The only problem here is that you'll get a password prompt every time to log into the machine you're ssh'ing into; use this handy tip on how to set up passwordless ssh logins to automate rsync without the prompt.


But before then, some more informative rsync-related links:

