Using Unison to Sync Two Folders
I’ve been using archlinux on both my home workstation and my laptop. Usually when I need to do serious work I’ll simply ssh to my workstation so syncing is usually not an issue. Except for one simple thing. I use mpd to play songs on both computers, and I pull lyrics on both machines. As a result I often found the lyrics of my favorite song missing on one machine. Automatic lyrics retrieval doesn’t work so well with my large collection of Jpop songs, even with my custom python script. Therefore I was looking for a way to sync the two lyrics directories on my two computers today.
And I found this little tool called Unison. It should be easy to install from any major linux distribution. The way it works is to set up a profile, which contains the location of the two directories that you want to sync. Then the command
will automatically sync the two directories. To make this command work, you'll need unison installed on both computers, and a profile file on the machine that you want to run the command, located in
~/.unison/profile-name.prf. A typical profile looks like (this is the profile I use to sync my lyrics)
# Unison Profile
label = Description of the Profile
root = /home/username/.lyrics
root = ssh://email@example.com/.lyrics
sshargs = -C -p port-number
After creating this file under
~/.unison/, the only thing you need to do is to run
unison profile-name and the two folders will have the same content. When there's a file conflict, unison will pause and ask for your input, whether to overwrite local file or the remote file, or merge the two files, similar to an svn commit. A good idea is to put this command into your crontab, and run it everyday or so, to keep the folders synced.