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?