I've been using rsync to sync a bunch of files between my two servers. I set a cron job that would run rsync every minute. However, I'd prefer if I could have it run always, so that files are synced the moment they are changed. What would be the best way to do this?

Thanks.

link|improve this question

feedback

2 Answers

up vote 7 down vote accepted

If you have inotify in your kernel, check out inotify-tools.

There's an example on that page:

#!/bin/sh
# A slightly complex but actually useful example
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %f' \
 -e close_write /home/billy | while read date time file; do
    rsync /home/billy/${file} rsync://billy@example.com/backup/${file} && \
    echo "At ${time} on ${date}, file ${file} was backed up via rsync"
done
link|improve this answer
I'm interested in this solution @Doug. Would you mind explaining what the different parts of this script do? – roseck Aug 5 '11 at 20:11
feedback

use the inotify system of linux, in combination with incron.

link|improve this answer
Doug Harris has a nice example usage. Care to further document yours as well? – Nerdling Sep 24 '09 at 21:53
doug copyNpasted the sample direct from that page :) to avoid doing the same: inotify.aiken.cz/?section=incron&page=doc&lang=en (rtfm) ... it works just like cron, just in a different directory (/etc/incron.d/) – akira Sep 25 '09 at 5:06
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.