Aaron Shen
Aaron Shen

Reputation: 8374

How to trigger Unison sync files and directories when there's any change on OSX?

I have a need to sync two directories on OS X. 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?

Upvotes: 5

Views: 7837

Answers (4)

Mike Pierce
Mike Pierce

Reputation: 1534

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

Upvotes: 6

Yuci
Yuci

Reputation: 30109

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

Upvotes: 2

rentzsch
rentzsch

Reputation: 3588

Install Unison:

brew install unison

Install unox:

git clone [email protected]: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

Upvotes: 2

Aaron Shen
Aaron Shen

Reputation: 8374

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.

Upvotes: 1

Related Questions