rsync+inotify的使用注意事项

-m是要持续监视变化。 
-r使用递归形式监视目录。 
-q减少冗余信息,只打印出需要的信息。 
-e指定要监视的事件列表。 
--timefmt是指定时间的输出格式。 
--format指定文件变化的详细信息。

所以可以通过脚本挂在后台实时监控指定目录文件用来触发rsync做文件推送:
#!/bin/bash
#rsync_inotify.sh
port=873
src_dir="/data/www/"
rsyncd_user="username"
rsyncd_host="192.168.2.1"
DEST_name="backup"
password_file="/etc/.rsync.passwd"
 
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %Xe %w%f' -e modify,delete,create,attrib ${src_dir} | while read line
do
    file=$(echo $line | awk '{print $4}')
    dir=$(dirname $file)
    if [ -f $file ];then
        rsync -vzrLtopg --progress --delete --port=${port} ${file} --password-file=${password_file} ${rsyncd_user}@${rsyncd_host}::${DEST_name}
    else
        cd $dir && rsync -vzrLtopg --progress --delete --port=${port} ./ --password-file=${password_file} ${rsyncd_user}@${rsyncd_host}::${DEST_name}
    fi
done

这样再把脚本后台启动后加入开机启动文件/etc/rc.local文件中,使得下次在服务器重启后还会生效:
/usr/local/scripts/rsync_inotify.sh &

Rsync 的详细介绍请点这里
Rsync 的下载地址请点这里

猜你喜欢

转载自www.linuxidc.com/Linux/2017-09/146674.htm