src1=/test  #监控的路径
des1=uploaders/   #需要同步到那台服务器上的rsync模块名称
rsync_passwd_file=/etc/server.pass  #密码交互文件
ip=192.168.8.178  #需要同步的IP
user=yihong  #需要同步方的用户
inotify=/usr/bin/inotifywait
cd ${src1}
$inotify -mrq --timefmt ‘%y-%m-%d %H:%M‘ --format ‘%T %w%f %e‘ --event modify,create,move,delete,attrib  ./ |while read file
do
INO_EVENT=$(echo $file | awk ‘{print $4}‘)
INO_FILE=$(echo $file | awk ‘{print $3}‘)
echo "-------------------------$(date)---------------------------" >> /var/log/rsync.log 2>&1
echo $file >>/var/log/rsync.log 2>&1
if [[ $INO_EVENT=‘CREATE‘ ]]||[[ $INO_EVENT=‘MODIFY‘ ]]||[[ $INO_EVENT=‘CLOSE_WRITE‘ ]]||[[ $INO_EVENT=‘MOVED_TO‘ ]];then
echo ‘CREATE or MODIFY or CLOSE_WRITE or MOVED_TO‘ >> /var/log/rsync.log 2>&1
rsync -avzcR $(dirname ${INO_FILE}) ${user}@${ip}::${des1} --password-file=${rsync_passwd_file} >>/var/log/rsync.log 2>&1
fi
if [[ $INO_EVENT=‘DELETE‘ ]]||[[ $INO_EVENT=‘MOVED_FROM‘ ]];then
echo ‘DELETE or MOVED_FROM‘ >>/var/log/rsync.log 2>&1
rsync -avzR --delete $(dirname ${INO_FILE}) ${user}@${ip}::${des1} --password-file=${rsync_passwd_file}>>/var/log/rsync.log 2>&1
fi
if [[ $INO_EVENT=‘ATTRIB‘ ]];then
echo ‘ATTRIB‘ >>/var/log/rsync.log 2>&1
if [ ! -d "$INO_FILE" ];then
rsync -avzcR  $(dirname ${INO_FILE}) ${user}@${ip}::${des1} --password-file=${rsync_passwd_file}>>/var/log/rsync.log 2>&1
fi
fi
done&
原文:https://blog.51cto.com/13746824/2438847