wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar -zxvf inotify-tools-3.14.tar.gz
./configure
make
make install
inotify
的系统相关参数
/proc/sys/fs/inotify/max_queued_events
/proc/sys/fs/inotify/max_user_instances
/proc/sys/fs/inotify/max_user_watches
#!/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
#!/bin/sh
EVENT=$(inotifywait --format '%e' ~/file1)
[ $? != 0 ] && exit
[ "$EVENT" = "MODIFY" ] && echo 'file modified!'
[ "$EVENT" = "DELETE_SELF" ] && echo 'file deleted!'
使用:
#! /bin/bash
src=/opt/webmail
des=/tmp
ip=172.18.199.82
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T%w%f%e' -e modify,delete,create,attrib ${src} |\
while read file
do
rsync -avz --delete --progress ${src} root@$ip:$des &&
echo "$src was rsynced"
echo "------------------------------------------------"
done
注:要取得监控文件发生的事件,在
--format
处指定
%e
参数,同时,使用
--event
参数来指定要监控的事件即可,如
--format '%T %w%f %e' --event modify,delete,create,attrib
#!/bin/sh
#set -x
#var
src="/usr/local/nginx/html/lib /usr/local/nginx/html/www /usr/local/nginx/html/var/www.work.com.conf.php"
des_ip="172.18.1.35 172.18.1.36 172.18.1.37 172.18.1.38"
#function
inotify_fun ()
{
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' \
-e modify,delete,create,move $1|while read time file
do
for ip in $des_ip
do
echo "`date +%Y%m%d-%T`: rsync -avzq --delete --progress $1 $ip:`dirname $1`"
rsync -avzq --delete --progress $1 $ip:`dirname $1`
echo
done
done
}
#main
for a in $src
do
inotify_fun $a &
done