This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: Networking

Keep your laptop and PC in Unison

By Federico Kereki on September 04, 2007 (4:00:00 PM)

Share    Print    Comments   

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.

Federico Kereki is an Uruguayan systems engineer with more than 20 years' experience developing systems, doing consulting work, and teaching at universities.

Share    Print    Comments   

Comments

on Keep your laptop and PC in Unison

Note: Comments are owned by the poster. We are not responsible for their content.

Keep your laptop and PC in Unison

Posted by: Anonymous [ip: 159.238.13.55] on September 04, 2007 05:23 PM
Personally, I prefer to use SVN. I just have a central point of truth (home server) and then I can make changes on both and reasonably figure out how to reconcile the differences.

#

Re: Keep your laptop and PC in Unison

Posted by: Anonymous [ip: 76.237.187.147] on September 04, 2007 09:02 PM
Different tools for different tasks. Unison is significantly quicker for large number of files, larger files or both.

#

Re(1): Keep your laptop and PC in Unison

Posted by: Anonymous [ip: 63.230.172.88] on September 15, 2007 11:51 AM
Besides, if you know that you need a 'central point of truth' (possibly for more than one person) then a revision system is the obvious way to go. Unison, is geared twards 'point of latest updates and changes between two points'. It scales to a single user better than svn, as there is really no repository to set up and it works at directory granularity. It is also simple (and I mean SIMPLE) to use right out of the gate. At least, in my experience, it took about 5 minutes for me to start using it successfully. If you are worried about revision control, then use an RCS. If you need 'those files to be the same as these files' then unison is a good option.

#

Revision Control versus Unison?

Posted by: Anonymous [ip: 68.126.190.109] on September 04, 2007 10:03 PM
Which works more efficiently with text files? Binaries?

#

Keep your laptop and PC in Unison

Posted by: Anonymous [ip: 68.146.205.60] on September 05, 2007 04:14 PM
Your write-up only mentions the console version. There is a graphical frontend (unison-gtk) for those scared of the CLI.

Cheers,
Simon

#

Keep your laptop and PC in Unison

Posted by: Anonymous [ip: 210.165.26.182] on September 06, 2007 07:51 PM
Correction: although Unison uses inode-number checking as a shortcut (only on Unix), it does compare the contents of possibly-updated files. See "Fast Update Detection" section of the manual. An option "-fastcheck no" can be used to force comparison of contents for every files. This means that "false updates detection" cannot occur, as stated in the "What is an Update?" section. (If you want touching files to be detected as an update, use -times option.)

#

Keep your laptop and PC in Unison

Posted by: Anonymous [ip: 91.3.80.38] on September 06, 2007 09:36 PM
great stuff. i am a fan of rsync an you are now in my bookmarks
http://wiki.mobbing-gegner.de/Linux/BackUp/rsync

#

no need for a star topology - really!

Posted by: Anonymous [ip: 83.26.239.98] on October 06, 2007 04:51 AM
You don't need a "star topology" - simply because Unison automatically always resolves changes in favor of a newest change (even if a file disappeared on the one side, Unison defaults to deleting it on the other side - of course you can change that), and whenever there is a "contradiction" of some sort, ie when the file has been changed on both sides, it defaults to doing nothing / asking a user. So you can just go all around with this tool, syncing random pairs with each other, simply choosing defaults and still - sooner or later every change will propagate to all the others.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya