29

I can sync folders with rsync -avz /directory /target, now I wish to do it if I changed a file in /directory so rsync should be called automatically.

I am using Virtual Box and the shared folder of Virtual Box is really slow, especially if you have a webpage which is using the shared folder as document root. With rsync i would be able to work with my local files on shared folder and sync it automatically with document root.

I hope someone has an idea how to do so,crontab would be not good, because it is executed each x minutes, so if i don't do anything, it will still call rsync but not if I modified my file.

Best regards

moon.musick
  • 1,958

4 Answers4

26

crontab would be not good, cause it is executed each x seconds/minutes, so if i dont do anything, it will still call rsync but not if i modified my file

rsync will only sync the files that have been changed. If nothing has changed, it will exit. That's really a minimal overhead.

If you're unhappy with that you could use inotifywait:

while inotifywait -r /directory/*; do
    rsync -avz /directory /target
done

That will be more instant but it will do things every time you save.

Oli
  • 299,380
13

You could use Lsyncd (Live Syncing Daemon):

Lsyncd watches a local directory trees event monitor interface (inotify or fsevents). It aggregates and combines events for a few seconds and then spawns one (or more) process(es) to synchronize the changes. By default this is rsync. Lsyncd is thus a light-weight live mirror solution that is comparatively easy to install not requiring new filesystems or block devices and does not hamper local filesystem performance.

Here is for example a tutorial for Ubuntu 16.04.

Arigion
  • 488
6

You can use inotifywait and rsync. inotifywait with the event modify,create,delete enabled. This way you will synchronize with your server only when the file changes, otherwise it will sync whenever a file is read (editors read several times your file to check if there are any changes). Thus said:

while inotifywait -r -e modify,create,delete /directory; do
    rsync -avz /directory /target
done
silgon
  • 320
0

Expanding slightly on a comment to the accepted answer I've had success using fswatch to trigger an scp of changed files to the guest. On Linux this is a wrapper around inotify but it's also cross-platform (I'm on a Mac with an Arm-based QEMU guest). I've tacked on an ssh remote build as well. The push approach works well with shares that behave in less than ideal ways.