Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use fswatch and rsync to automatically sync directories?

I'd like to automatically run a one way sync between two local directories using rsync. Meaning when a change is detected in a file of /dir1 or its subdirs, the following command should run:

rsync -rtuv /dir1 /dir2

How can I go about achieving this with fswatch?

Is it possible to supply arguments for rsync to only copy the actual files that were changed, as given by the fswatch events?

like image 736
OpherV Avatar asked Jan 03 '16 10:01

OpherV


1 Answers

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

Second line runs run_rsync once unconditionally and then - on each change in current directory (or specify exact path instead of .)

Rsync options:

  • -a - stands for "archive" and syncs recursively and preserves symbolic links, special and device files, modification times, group, owner, and permissions.
  • -z - compression
  • -P - combines the flags --progress and --partial. The first of these gives you a progress bar for the transfers and the second allows you to resume interrupted transfers
  • --exclude - excludes files by pattern

You will need fswatch:

brew install fswatch
like image 142
Daniel Garmoshka Avatar answered Oct 02 '22 09:10

Daniel Garmoshka