inotify+rsync实现文件同步

#!/bin/bash
src=/test/
dest=test
rsync_secret_file=/etc/rsync_pwdfile
dest_ip=192.168.37.133
user=test
/usr/local/bin/inotifywait -mrq --format '%Xe %w %f' -e modify,delete,create,attrib,move,open,close,access /test | while read file
do
test_event=$(echo $file | awk '{print $1}')
test_path==$(echo $file | awk '{print $2}')
echo -------------$(date)---------------
if [[ $test_event =~ 'CREATE' ]] || [[ $test_event =~ 'MODIFY' ]] || [[ $test_event =~ 'CLOSE_WRITE' ]] || [[ $test_event =~ 'MOVED_TO' ]]; then
echo 'CREATE or MODIFY or CLOSE_WRITE or MOVED_TO'
rsync -avzcR --password-file=${rsync_secret_file} ${test_path} ${user}@${dest_ip}::${dest}
fi
if [[ $INO_EVENT =~ 'DELETE' ]] || [[ $INO_EVENT =~ 'MOVED_FROM' ]];then
echo 'DELETE or MOVED_FROM'
rsync -avzR --delete --password-file=${rsync_secret_file} ${test_path} ${user}@${dest_ip}::${dest}
fi
if [[ $INO_EVENT =~ 'ATTRIB' ]];then
echo 'ATTRIB'
if [ -d ${test_path}];then
rsync -avzcR --password-file=${rsync_secret_file} ${test_path} ${user}@${dest_ip}::${dest}
fi
fi
done

原文地址:https://www.cnblogs.com/lishug/p/13215377.html