How to use fswatch and rsync to automatically synchronize directories?

I would like to automatically start one-way synchronization between two local directories using rsync . Meaning, when a change is detected in the /dir1 file or its subdirectories, the following command should be executed:

rsync -rtuv /dir1 /dir2

How can I achieve this with fswatch ?

Is it possible to provide arguments for rsync only to copy the actual files that have been modified as indicated by the fswatch events?

+5
source share
1 answer
 alias run_rsync='rsync -azP --exclude ".*/" --exclude ".*" --exclude "tmp/" ~/Documents/repos/my_repository username@host :~' run_rsync; fswatch -o . | while read f; do run_rsync; done 

The second line starts run_rsync once unconditionally, and then each time the current directory is changed (or specify the exact path instead . )

Rsync options:

  • -a - means "archive" and synchronizes recursively and saves symbolic links, special and device files, modification time, group, owner and permissions.
  • -z - compression
  • -P - combines the flags --progress and --partial. The first one gives you a progress bar for transfers, and the second allows you to resume interrupted transfers.
  • - exclude - excludes files by pattern

You will need fswatch :

 brew install fswatch 
+10
source

Source: https://habr.com/ru/post/1239715/


All Articles