环境:
192.168.1.35 CentosX64 6.5
192.168.1.37 CentosX64 6.5
实现功能:
现在我们需要监控IP为35机器上的某一目录,然后同步到IP为37的机器上
软件安装:
我们需要在35上安装rsync服务,在37上安装rsync客户端和inotify-tools
rsync服务安装参考http://blog.csdn.net/chenghuikai/article/details/60767613
inotify-tools安装:直接yum安装即可
编写inotify脚本:
#!/bin/bash
host01=192.168.1.160 #inotify-slave的ip地址
src=/backup/ #本地监控的目录
dst=backup #inotify-slave的rsync服务的模块名
user=rsync_backup #inotify-slave的rsync服务的虚拟用户
rsync_passfile=/etc/rsync.password #本地调用rsync服务的密码文件
inotify_home=/usr/local/inotify-3.14 #inotify的安装目录
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
# rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0
然后后台运行脚本:
sh inotify.sh &
现在可以测试了