Tuesday, July 13, 2010

Set Up Unison on Ubuntu to Sync 2 Laptops

Unison Sync Between Two Machines Part 1

The problem:

You have two laptops (or desktops, whatever) that you switch between or have multiple users share.  You want your data to sync between the two machines in both directions (you can save a file on either machine and it will sync to the other.)

Answer Number 1:

You can use Ubuntu one if you have less than 2 GB of data to keep up with (or are willing to pay to sync more data.)  Ubuntu One is nice since you can also access the files you are syncing via a web interface.  My biggest problem with Ubuntu One is the sync delay, it seems to sync whenever it feels like it (or sometimes not.)

Answer Number 2:

Unison (http://www.cis.upenn.edu/~bcpierce/unison/) lets you sync directories much easier than setting up rsync, especially in this use case.  (On Ubuntu, installation is as simple as "aptitude install unison".)  There is a graphical UI - you can start it once to create the $HOME/.unison/ directory and $HOME/.unison/default.prf file if you wish, or create those by hand.   (The default.prf file is empty to start with.)  I don't bother with the UI, but it's pretty self explanatory to use if you desire.  Let's say we want to keep our $HOME/Documents/, $HOME/Music/, $HOME/Pictures/, and $HOME/Desktop/ directories synchronized.  Install unison on both machines, then add the following to the $HOME/.unison/default.prf file:

# Unison preferences file
root = /home/alex
root = ssh://

terse = true
perms = -1
owner = true
group = true
times = true
batch = true

path = Desktop
path = Documents
path = Music
path = Pictures

(I would recommend creating some test directories to test all of this with before you use it on any important data.)  Once you've got the default.prf file set up on both machines, all you need to do is run the unison command to sync.  You will want to be sure to run it manually once before running it from CRON or in any other automated way since you'll have to confirm some things.  Automating unison is then as simple as adding a CRON entry such as:

# m h  dom mon dow   command
0 3 * * * /usr/bin/unison 2>&1 >> /home/$USER/.unison/unison_cron.log

Coming up in future parts of this article:
  • Setting up a root CRON for shared or other non-user directories
  • Setting up unison to sync at login and logout on Ubuntu

No comments:

Post a Comment