Search code examples
synchronizationunison

Unison / Rsync; mirror directories with sftp upload user


I know there are hundreds of questions about this topic. But I never found a clear example about it.

I created two users one is the developer and the other the sftp upload user. The sftp upload user should be able to upload files and folders. Those files should then be synced automatically to the developer folder. The developer now has the ability to process and edit the files. That means: When a file gets edited/deleted/created, the sync process should realize this change and mirror it to the developer from sftp and vice-versa.

I've tried rsync with inotifywait which works fine in one direction but not both ways. Then I stumbled across unison where I saw that it was possible to sync two ways. Now I just found out that with unison you can't set the new user/group permission like you could with rsync, which makes the sftp user obsolete. Can someone show me the example code for unison which will sync two ways on file change/create/delete from /home/folder1 to /home/folder2.

UPDATE: I've written a inotifywait bash script which works almost the way I want:

#!/bin/bash
EVENTS="CREATE,CLOSE_WRITE,DELETE,MODIFY,MOVED_FROM,MOVED_TO"
DIR1="/home/dev/project/"
USR1="dev"
DIR2="/home/sftp/uploads/"
USR2="sftp"

while inotifywait -e "$EVENTS" -r "$DIR1"; do
    sudo rsync -avu --delete --chown="$USR2":"$USR2" "$DIR1" "$DIR2"
done &
while inotifywait -e "$EVENTS" -r "$DIR2"; do
    sudo rsync -avu --delete --chown="$USR1":"$USR1" "$DIR2" "$DIR1"
done &
wait

There is one big flaw: On some occasions the rsync commands run simultaneously which can corrupt the files.

So my updated question:

How can I make the first loop's command only run when the second loop's command is finished and vice versa?


Solution

  • I'm not sure exactly what you mean by, "with unison you can't set the new user/group permission." Unison can't change the setuid and setgid bits, but Unison does change the file owner and file group. See the owner and group options in the Unison manual for more details on this.

    In general you'll want to have the developer invoke Unison (because I assume you want the developer to own and have control over the transferred files). Then you should call Unison with the repeat=watch option so that the instance of Unison will be watching the sftp files for any changes and transfer them immediately.