Keep your laptop and PC in Unison

275

Author: Federico Kereki

I usually carry my laptop everywhere and do work on it, but I do also need to work at my desktop PC, so I need to synchronize the files on the two machines. I have used several sync tools in the past, ranging from simple commands like scp and rsync to utilities like Krusader, which includes a way to synchronize directories on different machines. Recently I started using Unison.

Unison can synchronize files between Unix, Linux, and Windows machines; for example, you could synchronize a Windows laptop with a Unix server with the same ease as you would synchronize two Linux machines. You can use it with any two machines connected over the Internet, and it can use an encrypted SSH connection for extra security.

Unison is freely available under the GNU GPL. Its development seems to be at a halt right now, with no one specifically on charge of maintenance or bug fixes. However, the site explains that the original developers are still using the program, and continue to maintain and support it, and (on occasion) release new bug fixes or small changes. The current stable version at the site is 2.13.16, but there’s a beta version 2.27.29 (quite a jump!) and the current development, unstable, version is 2.28.15.

Since it has been around for some time, most distributions’ repositories include it. I use openSUSE (both 10.1 and 10.2), and installing Unison was a breeze. A curious point: on openSUSE 10.1 I got the 2.13.16 version, but on 10.2, I got a newer 2.17.1 one. That posed a problem, because you must have the same version of the program in both machines, so I went to rpmseek.com and got the 2.17.1 version for my openSUSE 10.1 box.

Trying it out

After getting Unison and installing it on all the machines you want to keep in sync, open a terminal window and test it. The command unison -doc tutorial should produce a tutorial on its usage. Similar commands include unison -doc topics to get a list of specific topics, and unison -doc all to get a complete manual.

Unison can run as a command-line program (useful for scripts or running in a console window) or with a somewhat spartan graphical interface. Invoking the program without any parameters will open the graphical interface, but you can force the command mode with the -ui text parameter.

As the documentation suggests, the easiest (and safest) way to learn to use Unison is by synchronizing two directories on the same machine. I started by creating a pair of identical directories named original and copy. For a first test, I just did unison -ui text original copy and got a longish message explaining that this was my first run, saying what Unison would do, and finally informing me that no changes were needed. Typing unison original copy without the text option provided a screen showing an “Everything is up to date” message.

Next, I made some changes in both directories — adding, removing, and changing some files — and tried the synchronization again. This time, I got a listing of the differences between the directories. For new files the interface will show “new file” and copy it to the other directory. Deleted files will be deleted in the second directory. For files in both directories that have different contents, the user must press “<” or “>” to tell Unison which version to take as standard, or “/” to leave the files alone and do nothing with them. By the way, answering “?” to a prompt produces a list of possible answers; see the example below.

By the way, the update detection algorithm doesn’t actually compare the current contents of files to their previous contents, but just looks at each file’s inode number and last modification time; if neither has changed, it concludes that the file hasn’t been changed. This is pretty safe; though it may sometimes detect “false updates,” it won’t miss real ones. Of course, you could fool it by using touch to change the file’s modification timestamp. The information about files is kept in a hidden .unison directory.

Contacting server...
Contacting server...
Looking for changes
Reconciling changes

original       copy
changed  ====> changed    listing  [] >
deleted  ---->            mounts  [f] f
         <---- new file   new_one  [f] f

Proceed with propagating updates? [] ?
Commands:
  y or g                Yes: proceed with updates as selected above
  n                     No: go through selections again
  q                     exit unison without propagating any changes
Proceed with propagating updates? [] y
Propagating updates

UNISON started propagating changes at 13:46:02 on 25 Aug 2007
[BGN] Updating file listing
  from /home/fkereki/Documents/articles/prueba/original
  to /home/fkereki/Documents/articles/prueba/copy
[END] Updating file listing
[BGN] Copying new_one
  from /home/fkereki/Documents/articles/prueba/copy
  to /home/fkereki/Documents/articles/prueba/original
[END] Copying new_one
[BGN] Deleting mounts
  from /home/fkereki/Documents/articles/prueba/copy
[END] Deleting mounts
UNISON finished propagating changes at 13:46:02 on 25 Aug 2007

Saving synchronizer state
Synchronization complete  (3 items transferred, 0 skipped, 0 failures)

Working with two machines

There are several ways to use Unison to synchronize two different machines. The simplest approach is to connect via SSH. To test whether you can make an SSH connection and run Unison, try ssh 192.168.0.198 unison -version, substituting the IP address of the remote machine on which you have Unison installed. After typing your password for the other machine, you should see Unison’s version. If you need to log in as a different user, use ssh someotheruser@192.168.0.198 unison -version.

If the connection is working, then you can do the synchronizing by typing a command like unison original ssh://192.168.0.198//home/fkereki/original (note the double “//”). If you need a different user on the remote machine, then use unison original ssh://someotheruser@192.168.0.198//home/fkereki/original.

The socket connection method is also simple, but more insecure, so you shouldn’t use it over a public network. It requires setting Unison to run in server mode on the remote machine. Also, in this mode Unison can serve only one client at a time, so if two different users happened to want to synchronize their files at the same time, one would have to wait until the other was done. Finally, note that Unison will run with the permissions of the user who started it; if several users need to synchronize against a server, and they have separate permissions, they will have to use the SSH method.

To use the socket method, you must run unison -socket 12345 on the “server” machine so Unison will wait for connections. You can add an “&” at the end of the command to let Unison run in the background. By the way, the parameter after -socket, 12345, is an arbitrary port number, and you could almost safely choose any number between, say, 10000 and 40000. (Some numbers are restricted: check this reference to make sure you pick a safe one.) Once Unison is running on the remote machine, you can synchronize files as above by running unison original socket://192.168.0.198:12345/home/fkereki/original, and you should get the same results as with the SSH method.

More advanced usage

Unison has many more options. If you usually require the same ones, you can specify them in a profile (which is simply a file with a specific format stored in the .unison directory) and then just run unison yourprofile in order to apply all preferences at once. Read the documentation on profiles available online.

If you need to synchronize several machines, you must be careful, since Unison is designed to keep a pair of machines in sync. The way to do this is to use a “star topology”: one machine should be the star center or hub, and all the other machines should always synchronize with this one. If you never try to synchronize any other pairs of machines, there should be no problems.

In conclusion

Unison allows for easy synchronizing, though the “same version” requirement could prove a stumbling block if you needed to synchronize files with several different servers. The software seems solid enough, and there are two good points for it: the relatively high version number for the stable version, and the fact that the developers keep using it and producing updates. I find it an easy tool to use, and I’m keeping it in my everyday toolkit.

Categories:

  • Networking
  • Tools & Utilities