5

I have a need to sync two directories on OSX. I find this post recommending using unison.

I tried unison, it seems it can sync two directories very well. But it doesn't do synchronization automatically( always keep two directories synced when there's any change ).

So I guess to accomplish my purpose, unison should be used with a trigger which will watch changes and notify unison to do its work.

But how to do that? Any recommendation and tutorial?

Community
  • 1
  • 1
Aaron Shen
  • 8,124
  • 10
  • 44
  • 86

4 Answers4

6

Unison has this capability built in. Just add the line repeat = watch to your Unison profile.

Mike Pierce
  • 1,390
  • 1
  • 12
  • 35
2

Install Unison:

brew install unison

Install unox:

git clone git@github.com:hnsl/unox.git
cd unox
ln -s $PWD/unox.py /usr/local/bin/unison-fsmonitor

Do the initial sync:

unison -auto -batch dirA dirB

Run the continuous bidirectional sync:

unison -auto -batch -repeat watch -times dirA dirB
rentzsch
  • 3,548
  • 4
  • 27
  • 39
1

I find the npm module fsmonitor for watching content changes in a directory.

first I write a small script to sync the folders I want with unison like:

unison -auto -batch dirA dirB
unison -auto -batch dirB dirA

install the module in global: npm install -g fsmonitor.
run command in shell: fsmonitor -d <the dir> <sync script>.
Then any change happens in the directory, the sync script will be executed, and the two directories will be in sync.

Aaron Shen
  • 8,124
  • 10
  • 44
  • 86
1

Using repeat = watch in your unison profile in the .unison directory will do the trick. (I tried on Ubuntu 18.04)

Step 1, edit unison profile, $HOME/.unison/example.prf

root = /tmp/data1
root = /tmp/data2

path = shared

repeat = watch

Step 2, start unison

$ unison example

Step 3, create a file in either root, and it will be synced automatically by unison to the other.

$ touch /tmp/data1/shared/1.txt
$ ls /tmp/data2/shared/
-rw-r--r-- 1 user1 user1    0 Mar 26 16:25 1.txt

Appendix

  1. You can install unison in Ubuntu by running sudo apt-get install unison
  2. To use repeat = watch, you'll need unison-fsmonitor
$ curl -L -o unison-fsmonitor https://github.com/TentativeConvert/Syndicator/raw/master/unison-binaries/unison-fsmonitor
$ which unison
/usr/bin/unison
$ sudo cp unison-fsmonitor /usr/bin/
$ sudo chmod +x /usr/bin/unison-fsmonitor
Yuci
  • 27,235
  • 10
  • 114
  • 113