How do I trigger rsync on file modification?

31,019

Solution 1

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.

Solution 2

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.

Solution 3

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
Share:
31,019

Related videos on Youtube

BlackScorp
Author by

BlackScorp

Updated on September 18, 2022

Comments

  • BlackScorp
    BlackScorp over 1 year

    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

  • BlackScorp
    BlackScorp over 10 years
    well the problem it seems, that inotify is not able to check shared nfs folders. to make sure, what i want to,tryin to describe i have virtual box installed with vagrant, there i have a shared folder "projects" it is mounted on boot, all my webserver files are located there so i can modify them on my host system(win7) as i told virtual box shared folders are really slow, so solution could be to sync the mounted nfs folder to another place e.g /var/www but the common ways are not working, i tryed fileschanged, icrontab, gues gamin or fam would help, but i dont know how to start or use them
  • Oli
    Oli over 10 years
    @BlackScorp realistically you're stuck with just cronning rsync and letting it decide what needs syncing. As I lead with in the answer, that's not an awful bad option.
  • Oli
    Oli over 10 years
    The other option is to push the files from the host rather than trying to detect changes remotely.
  • BlackScorp
    BlackScorp over 10 years
    well this solution i used before, it has 2 problems: 1) i had to reconect everytime to my virtual box to edit files, which means, my virtual must running, quick editing is not working anymore 2) i dont know why, but because of the project scanning process of my IDE(Netbeans) samba shows 99% CPU Usage and everything within the virtual box is just slow while developing i will try the rsyn + cronjob solution or the php build in webserver
  • MarSoft
    MarSoft almost 5 years
    The inotifywait solution has one slight but important drawback: it does not detect changes which happened when rsync is running. Consider the following situation: file A is changed; rsync is triggered; when rsync is almost done synchronizing file A, file B is changed. Now, rsync finished and inotifywait is running, but file B is not synchronized and won't be synchronized until some next change. Consider using lsyncd as suggested by @Arigion.
  • MarSoft
    MarSoft almost 5 years
    This won't detect changes happened while rsync is running. Consider using lsyncd as suggested by @Arigion.
  • Somebody
    Somebody over 4 years
    this will re-apply all events on all files after each change... and when it's applying it won't see any change in that period...
  • Somebody
    Somebody over 4 years
    this will re-apply all events on all files after each change... and when it's applying it won't see any change in that period...