Need to check a directory tree for changes every 10 seconds and run a script if it is changed
I have a directory that has many folders with code inside it, and I need to run a script to rsync changes over to a couple other servers. I have that script written and working fine, but I cannot find something that will check the directory tree for changes. I tried stat, but that only check one folder down, not recursively (as far as I could figure out). Anyone know of a command or program I could install to do this? I use Ubuntu 10.04. Thanks in advance.
inotifywait
will do recursive checks.
It's in the Ubuntu repositories.
The perfect tool for this job is using the inotify kernel service. You can use them on a shell script with the package inotify-tools (debian/ubuntu). You can read more and see some examples on the project's page. There's even an example on the page that seems to do something close to what you want:
#!/bin/sh
# get the current path
CURPATH=`pwd`
inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' \
-e close_write /tmp/test | while read date time dir file; do
FILECHANGE=${dir}${file}
# convert absolute path to relative
FILECHANGEREL=`echo "$FILECHANGE" | sed 's_'$CURPATH'/__'`
rsync --progress --relative -vrae 'ssh -p 22' $FILECHANGEREL [email protected]:/backup/root/dir && \
echo "At ${time} on ${date}, file $FILECHANGE was backed up via rsync"
done
rsync will check the directory tree for changes and copy only the changed files if you give it a directory as the source.
Have a look at inotify. I solved a similar problem some time ago with dnotify or sgi_fam (don't recall which) but inotify seems to have superceeded them both.
Someone already invented this wheel. It's called lsyncd and it is apparently a mashup of rsync and inotify. I haven't used it myself, because the limitations of inotify are such that it doesn't scale to really large (and deep) directories. I'm waiting until they reimplement it directly using fsnotify.
There's also PIrsyncD which seems to do the same thing.