Sunday, January 10, 2010

Unify your files with Unison

If you are using multiple computers, you must have some way of keeping the files you want on each machine.  A lot of people use Dropbox or LiveMesh, and Canonical recently threw its hat into the ring with UbuntuOne, but all of these solutions provide a pretty small amount of storage (2 gigs in the case of Dropbox and UbuntuOne), and as with any online storage solution they also require that you trust them not to lose your data and to respect your privacy.

Because I prefer not to trust anyone with my data unless I have to, and partly because I'm a cheapskate, I prefer the DIY approach.  This is where Unison comes in.

Unison Explained

Unison is a file synchronization utility that keeps the contents of two directories synchronized.  In true Unix fasion, it doesn't reinvent the synchronization wheel, instead it uses rsync to do the comparison and synchronization, effectively making a tool that could only handle one-way synchronizations capable of handling two-way synchronizations.  In addition to being able to sync local directories, it can also operate over SSH, so you don't have to spend a bunch of time worrying about providing unison a protocol or dedicated port, or setting up a dedicated authentication mechanism.

Like all great Linux utilities, Unison has a graphic user interface but can also be used on the command line (this is important for automating synchronizations in cron; more on that later).  Either way, you are going to manage Unison tasks with Profiles.  Profiles are basically a set of two folders that you are syncing (one local, the other local or remote), and zero or more options, and although they can get rather elaborate, don't let it scare you.  The Unison GUI makes the task of creating a new profile simple.

Creating Unison Profiles
  
After you've installed Unison (on Ubuntu use "sudo apt-get install unison unison-gtk"), fire it up from your menu or by typing "unison-gtk" into the command line.  You will be greeted with a screen where you can type in the name of your first "root", or local directory.  When you are finished with that, hit OK and then you will be promted to enter the second directory.  This screen will give you the option to connect to a remote machine over SSH or a raw socket (not recommended).  If you decide to sync with another machine over SSH, be sure to type in the absolute path to the folder on that machine (i.e. "/home/jizldrangs/documents").  Fill in the host name, username to connect as, and port name as necessary, then hit OK and you're done!  Unison will take this information and create a new profile called "default".  You can create new profiles the next time you launch the Unison GUI

The GUI will help you set up a profile and will allow you to set some of the basic options, but if you want to use any of the more advanced features (see the Unison man page for a list), you will need to edit the profile by hand.  Your profiles are stored in separate files in the .unison directory in your home directory (e.g. /home/jizldrangs/.unison; it is a hidden directory).  In that folder you will see a file for each of your profiles, all ending with ".prf".  Simply open the profile you want and start adding options.  You can do things like exclude certain file types, exclude certain directories, exclude files over a certain size, have it follow links, etc. 

I already have an old Pentium 3 laptop recommissioned as our file and print server, and it runs all the time, so it was the perfect place to store the master copies of all my files, and to receive updates from whichever of my computers I make changes on and distribute them to the rest of the machines, all within the security and ease of SSH.

To make sure that I have the latest version of my files, I've tasked Cron, my personal Linux butler, with the task of running the sync at the bottom of every hour.  Here is the relevant line from my crontab file:

# m h  dom mon dow   command
30 * * * * unison default -ui text -batch

The "ui text" option tells unison to use command-line mode and not to launch the gui, and the "batch" option tells it to accept default update options (basically, replace older files with newer files), so it doesn't prompt me for input on what to do with the files.  Now my file syncing is totally automated.

Unison Gotchas

If you are syncing files to an mp3 player or usb flash drive, add the option "perms = 0" to avoid getting the error "failed to set permissions" when you try to sync.

Happy syncing!  

No comments:

Post a Comment