Syncing Desktop, Laptop working directories quickly with Unison

I now have a laptop to accompany my main development desktop. I rarely use it, but when I do, I like to be able to grab it and go, keeping my exact working state from my desktop synced with it.

Originally, I had planned to use SkyDrive/Dropbox/Google Drive to accomplish this. I thought I could put all my workspace files into the synced folder, and with no effort, my files would be safely synced across my devices.

That was the expectation. In reality, there was a huge problem, as I use Git for all my major projects. As a result, the object stores can easily grow to contain hundreds or thousands of files, which takes hours to sync on any commercial cloud sync service. Even worse, the services tend handle conflict resolution very poorly, renaming and duplicating files that do not match across machines.

In other words, using Dropbox, GDrive, SkyDrive, etc., is a great way to corrupt your repositories, and a poor, slow way to sync your working directories.

Enter Unison

I had played with Unison before. It's like rsync, but subjectively better. It's also freaking fast.

Syncing files from machine A to machine B is really simple, and the getting started can tell you that in a second. But my setup required something a little different. I'm going to be using my laptop and desktop on a dorm network, which means I cannot directly connect my two machines over the network. Bluetooth or other local connection options seemed like a poor choice. Instead, I added a third player: my hosted VPS.

This is actually really beneficial. For one, I can keep my files backed up safely on a private server. For two, I can push and pull from the server at any time, regardless of the state of the other machine.

The setup

I created a simple batch file containing the following:

unison c:\users\jaxbot\www\ ssh://
\ -sshargs "-C" -auto -sshcmd ssh2plink.bat -ignore "Name config.js"

Let's break it down. There are some additional curveballs here, since my machines are Windows based, which makes this a little more complicated. If you're lucky enough to be able to use *nix full time, give yourself a pat on the back.

Windows stuff: For one, the latest Unison executable needs to be in your PATH. Two, I'm using Putty with Pageant and Plink to handle SSH certificate authentication, which allows a passwordless sync.

If you want to use Plink, you have to grab ssh2plink.bat from here and also place it in your path, with Plink. If you want passwordless authentication on Windows, look up some tutorials on Pageant. Otherwise, password authentication works fine.

The additional flags are just for customization; -C turns on SSH compression, -auto enables Unison to make judgments on its own, and -ignore stops it from syncing my project config files, which differ from machine to machine.

The result

I click on my batch file. Unison fires up, connects to my server with Plink, does an evaluation, and informs me of any conflicts, allowing me to choose which file to keep. The entire sync of my 2,777 files across 753 folders takes... about 4-7 seconds. Dear cloud sync providers: implement this.

I can now set a schedule to automatically sync my files, add some triggers, etc. That was easy.

tl;dr: Unison is a very, very fast, stable, solid syncing program that works cross-platform. Use it directly between machines, or use a central hub to duplicate your working state perfectly.

BTC: 16TQ2WtfNEUSZ5e2NfC2VtNuMD8e4J29eA