Month: February 2011

Unison: File sync from Ubuntu to Windows 7

Hey, been awhile. Have been ignoring the blog (even my traditional New Year's Eve Post) and many of my other Internet habits in favor of various projects I've been hard at work on. I just pulled off a WordPress update; you're reading this so it looks like it went smoothly.

Anyhow. One of the aforementioned projects (and the thing you came here to read, if you found this page by Googling an error message -- and if you did, you may want to skip my meandering explanation and go straight for the numbered steps at the bottom of this post): I recently decided to set up a file sync system across the computers in my house. It's useful for syncing things like savegames, RSS feeds, and the public-domain ebooks I've been grabbing from Project Gutenberg and MobileRead and comics from Digital Comic Museum across multiple devices.

I'd done some command-line RSS before, and also set up backup systems with Toucan, but figured I'd try something different on this one. I gave Ubuntu One a shot and it seemed promising until I realized it isn't open-source and I can't set up my own server. Canonical is swiftly becoming the Apple of the Linux world -- good at taking open-source software and making it pretty and usable, but not so great at giving back to the open-source community.

Ultimately I settled on Unison, which proved to be a bit of a headache -- frankly if anybody has a better solution I'd be happy to hear it, but here's how I got it to work.

First of all, the Unison GUI requires GTK. Hardly a problem on the Linux side, but under Windows, extracting the binaries from gtk.org and setting the PATH variable didn't work, no matter what I did. Maybe it's a Windows 7 thing, or maybe it's a Unison thing, but either way, Unison threw up "This application has failed to start because libgtk-win32-2.0-0.dll was not found. Re-installing the application may fix this problem." every time I ran it. Sticking it directly in the GTK\bin directory worked but is an ugly solution; multiple sites suggested installing Pidgin, which comes with GTK, but produces the same problem as Unison doesn't find it in the path.

(Actually, let me back up a bit: I couldn't get Unison to work with 64-bit GTK at all. The only Unison binaries I could find were 32-bit; I opted to install a 32-bit version of GTK rather than stick Cygwin on my HTPC and compile Unison from source.)

Ultimately, I found a binary Windows installer for GTK (conveniently the first Google match for gtk windows binary installer); whatever my PATH problem was, this installer fixed it. The Unison GUI was up and running, from its own folder.

Next problem, though: SSH. Unison did not play nice with PuTTy.

Googling the problem, I found a page called Unison-ssh, which includes a wrapper named ssh.exe for download. If you've read this far you've probably already installed PuTTy, but in case you haven't, you'll only need it if you want to use public key authentication -- this ssh.exe will automatically install a copy of PuTTy's command-line SSH utility, plink.exe, if it can't find it. (Well, hypothetically. It tries to stick it in WINDIR and if you're not running it with admin privileges it'll fail.)

Now, I should add that this ssh.exe doesn't work properly under Windows 7; it'll prompt you for a username but only let you type one character and then automatically Enter it. Same problem with the password prompt. The comments thread in the page is filled with people who have the same problem. Maybe a clean compile would fix it, I don't know; again, I didn't want to go to the trouble of setting up compilers on my HTPC.

There's a solution a ways down the comments thread. Unison stores its data in the .unison directory, even under Windows. (That'd be \Users\name\.unison under Win7.) They're simple text files with the .prf extension. And you can add an "sshargs" line to give command-line arguments. If you're comfortable sticking your password in plain text, you can add the line "sshargs = -pw [pass]" and you're done. But if you're not, you can set it up with RSA keys. A later comment links a post on Palin's Technical Blog that runs down how to generate a keypair with puttygen -- the problem is, I couldn't get my Linux server to accept it; I kept getting a "Server refused our key" error.

I found the solution on Andre Molnar's blog: you need to generate the keypair on the Linux server, using ssh-keygen, add the public key to your authorized_keys file, then move the private key over to the Windows machine and use puttygen to import it and then save as a PuTTy .ppk file. From there, add "sshargs = -i [path to private key]" to the appropriate .prf file.

Almost done, but the Unison GUI still has path issues, even if you stick ssh.exe in the same directory as PuTTy and add that to your PATH. I got around it by sticking a shortcut on the desktop with the PuTTy directory as the working directory.

In summary:

  1. Install openssh-server on your Linux server and PuTTy on your Windows client.
  2. Install Unison and its dependencies on your Linux server. (It's offered in the Ubuntu repos; command-line is unison, GUI is unison-gtk.)
  3. Install Unison on the Windows client.
  4. If you want to use Unison's GUI, install GTK on Windows.
  5. Download the ssh.exe wrapper for PuTTy. Stick ssh.exe in the same directory as PuTTy and put that directory in your PATH.
  6. Generate an RSA keypair on your Linux server using ssh-keygen. By default it will put the keys in ~/.ssh/id_rsa and id_rsa.pub.
  7. Copy the contents of the public key (id_rsa.pub) to ~/.ssh/authorized_keys. Remember to set perms on ~/.ssh to 700 and authorized_keys to 600.
  8. Move the private key (id_rsa) to the Windows machine. That's move, not copy; delete it from the Linux side as you don't want to store the same private key in more than one place.
  9. Run puttygen.exe. Import your existing private key, then save the result as a new .ppk file. Delete the original key file. Again, only the owner should have read perms on this file.
  10. At a minimum, your \Users\name\.unison\foo.prf file should contain the following:

    root = [Windows path]
    root = ssh://[user]@[host]//[Linux path]
    sshargs = -i [path to private key]

  11. To get the Unison GUI to run ssh.exe properly, create a shortcut and set its working directory to the PuTTy directory.
  12. You can schedule regular syncs using Windows Task Scheduler; run the command-line Unison executable, with args "-batch [name of pref file]". Don't include path or extension, just the filename ("foo" in my example above).

So there you go: a cross-platform syncing solution. Good for backups, for keeping files consistent between your desktop and your laptop, or for anything else that requires keeping the same files on multiple machines.


Playing: Just finished playing a fan translation of Act Raiser. Maybe a bit more on that soon.

Reading: Blood of the Elves. As I await The Witcher 2.