With the open-source file synchronization tool Unison, you can easily keep files synchronized in multiple places locally and on remote machines.
The cardinal rule of IT is to keep backups of your files. But how can you easily manage multiple locations of working copies of files? Here's where file synchronization comes into play at your desktops and servers.
Unison
Unison is an open-source synchronization tool that runs on both Windows and Linux environments. What makes it unique is its ability to synchronize across platforms, meaning from a Windows machine to a Linux server. Remote synchronization can be handled through a tunneled SSH session or across a direct TCP socket link. Unison claims to be sparing with network bandwidth, giving you the ability to run across PPP connections.
You can download Unison for both Windows and Linux. The manual concentrates on running the commands from a Linux machine, but the Windows command line version is almost identical. The Windows version also gives you the ability to run Unison in a graphical mode, but it requires you to load the GTK+ toolkit. GTK is an open-source toolkit for creating GUIs on both Windows and Linux. I'll concentrate on using the command version for both operating systems, rather than the GUI.
Installing Unison on Linux
As most of you know, my Linux distributions of choice are Red Hat Enterprise Linux, CentOS, and Fedora. By using any of these distributions, you can easily install Unison from Yum using RPM packages. On Fedora, Unison is part of the typical update package branch, but on CentOS and RHEL, you can install Unison using the RPMforge third-party repository. If you don't already have their repository set up, follow along with the RPMforge guide for installing the necessary components. Once that's complete, you can simply run the Yum utility to add the package:
# yum install unison
If you need to install Unison from a source file, you'll need a couple of items to build it on your systems, the first being a GCC compiler, and the second being the OCaml compiler. OCaml installation instructions are located with the installation file after you unpackage the download. If you need to install a GCC compiler, seek your operating system's guidance for installing the tool from their package management tool.
Next, download the Unison source code. Unzip the contents, and you can find the installation instructions for building Unison in the file marked INSTALL.
Installing Unison on Windows
To install Unison on Windows, download a binary installation file from maintainer Alan Schmitt's Web site. As of this writing, version 2.27.57 is the most current stable release available. I mentioned earlier using Unison with the GUI tool. If you prefer using the GUI, follow the directions on the Web site to install a version of GTK+ suitable for Unison. Otherwise, all you need to do is unpack the contents of the directory and place it somewhere easily accessible. I created a C:unison directory to extract the contents to.
From here, you'll find the command line text version and the GUI version of the command. If you want to make it easier on yourself, rename the text version's executable file to simply unison.exe. You can run it from the command line as follows:
C:> unisonunison.exe -help
Using Unison Locally
Using Unison locally is perfect if you want to keep copies of files and directories between two locations on the same machine. One reason for doing so would be if you use roaming profiles on your network but also have local accounts. For example, I maintain laptops for workers who are on the network while they're in the office but then sign into a local account while they're out in the field. Unison can synchronize files on the network profile to the user's local directory so that he or she can always have up-to-date information while outside the office.
To synchronize files locally, create a test environment with two directories. Name one temp1 and the other temp2. Create a file in temp1 and then synchronize it to temp2 using Unison. I'm assuming running these commands from Linux.
# mkdir temp1 temp2
# echo "testing" > temp1/testing.txt
Now, run the Unison tool to synchronize the file you just created to the temp2 directory. The first time you run Unison, it will search to see if this synchronization has taken place before. If it hasn't, it will create a hidden directory (.unison) within your user profile, from which it will work when figuring out synchronizations. On a Linux system, it will be in /home/userprofile/.unison directory, and on Windows, it will be located at the C:/Documents and Settings/userprofile/.unison folder. Once you read the text it displays, hit the Enter key to continue. Unison will then show you what it is going to do in regard to your files.
# unison temp1 temp2
Press return to continue.[<spc>] Reconciling changes
temp1 temp2
file ----> testing.txt [f]
At this point, if you hit Enter, the file you created in temp1 will be synchronized to the temp2 directory.
Notice the [f] beside the file in the above command. This means that it will go ahead and follow Unison's recommendation on how the synchronization will take place. You can change these options and commands if you want Unison to behave differently. For example, you may want to display the differences, show all the details, or even merge the differences. To see a list of the available commands, hit SHIFT+? to display the option list.
Lastly, you'll be asked whether you want the changes to take place or not. Simply type a "y" for yes or an "n" for no.
Proceed with propagating updates? [] y
If you want a list of Unison's command line options and help, run Unison from the command line with the -help switch.
Merging Differences
Unison itself doesn't handle changes to the same file in two different locations. Ultimately, it can get confusing when you are changing the same file in two locations, so it probably shouldn't be done to begin with just for sanity's sake. Unison does have the ability to call upon other utilities and programs, however, to do the actual merge for you should this problem arise. Within the Unison documentation, there is a section dedicated to handling merging conflicting files.
On Linux, you can write your own scripts or use the diff3 tool, as the documentation points out. From a Windows machine, the documentation explains using Emacs installed under Windows. Also, you might want to check the open-source project WinMerge, which you can call from Unison.
You can add the following to your Unison preference file within Windows. The file is called default and is located in the hidden directory. Open it up with a text editor and add something similar to this:
merge = Regex .* -> "C:PROGRA~1WinMergeWinMergeU.exe" "CURRENT1" "CURRENT2"
Remote Usage: Linux to Linux or Windows to Linux
There are two options for using Unison remotely. You can use the remote shell method, which invokes commands across SSH. This method is secure and easier to use than the second method, which is to use a TCP socket link. This means running a client on your local machine and a server on the remote machine. Between Linux machines, I would definitely recommend using the SSH method. You will need to install Unison on the remote machine before continuing. After you verify that you can connect to the remote machine, you can sync a directory over to the remote machine.
# unison -testServer test1/ ssh://desktop/test1
Contacting server...
Connected [//laptop//home/hetrickme/tmp/temp1 -> //desktop//root/temp1]
If the connection was successful, you can now synchronize your files over to the remote machine.
# unison test1/ ssh://desktop/test1
The normal menu system will now be displayed as it was before. This method should work the same for Linux-to-Linux systems, as well as for synchronization from Windows to Linux.
Remote Usage: Linux to Windows or Windows to Windows
If you want to synchronize from a Linux machine to a Windows machine or from a Windows machine to a Windows machine, you can use the socket method. Remember that the socket method is insecure, however, so you should only run this across a secure or trusted network.
On the Windows machine, put Unison into socket mode, listening on a port.
C:unisonunison.exe -socket 412
server started
On the Linux or Windows machine, you can go ahead and synchronize the files across the listening TCP sockets.
# unison test1/ socket://remote_windows:412/test1
This will synchronize the directory to the C: est1 folder from Linux to the Windows server.
Perfect for Home/Office
As you can see, Unison makes it easy and quick to synchronize files from your desktop and laptops in an office environment. Many people do not have VPN solutions or remote access to their office from home, so synchronization of files is the next best thing.
LATEST COMMENTS
MC Press Online