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

4

5

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?

Sinuosity answered 4/12, 2015 at 23:31 Comment(0)
L
6

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

Latham answered 19/12, 2015 at 20:31 Comment(0)
B
2

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
Bauxite answered 2/2, 2017 at 21:24 Comment(0)
I
2

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
Irish answered 26/3, 2019 at 16:49 Comment(0)
S
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.

Sinuosity answered 5/12, 2015 at 4:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.