Unison tutorial: syncing files easily with a remote server

What if you could enjoy the computing power of a remote server in the comfort of your laptop? In this article, I’ll show you how to use Unison to enable bidirectional sync between your local computer and a remote server. Every time you edit a file on either computer, it’ll be synced to the other.

Untitled.png

To get this running, we need to install Unison with its watcher module (unison-fsmonitor). The watcher will ensure that the sync runs every time you edit a file. Also note that both computers need to have the same version of Unison.

Installing on Ubuntu

The best way to install Unison with its watcher module to install from source:

sudo apt install ocaml
wget https://github.com/bcpierce00/unison/archive/2.48.4.zip
unzip 2.48.4.zip
cd unison-2.48.4
make
sudo cp -t /usr/local/bin ./src/unison ./src/unison-fsmonitor

Installing on Mac OSX

Follow instructions to install 2.48 on OSX. Then run the following to install unison-fsmonitor:

brew install autozimu/homebrew-formulas/unison-fsmonitor

Creating a config file

While you can run Unison with arguments, I’d suggest using a config file. If you’re on Linux, create a file in ~/.unison/server1.prf and on OS X in ~/Library/Application\ Support/Unison/server1.prf. The config file only needs to be on your local computer. Then add the following content:

root = /folder/on/local/computer
root = ssh://[email protected]//folder/on/remote/server

path = notebooks
path = models
# Unison preferences file
ignore = Name .*
ignore = Name {,.}*{.*}
ignore = Name __pycache__
ignorenot = Name *.py
ignorenot = Name *.ipynb
ignorenot = Name *.sh
ignorenot = Name *.json

# Log actions to the terminal
log = true

auto = true
batch = true
repeat = watch
prefer = newer
fastcheck = true
sortnewfirst = true
confirmbigdeletes = true

This config files syncs .py, .ipynb, .sh and .json files, as well as the folder hierarchy.

You can now run: unison server1 from your local computer and enjoy the sync!

Resources

Platform-specific Unison tips

Full Unison documentation

...
Eliot Andres

CTO & Co-founder @ PhotoRoom