rsync + inotify (相对来说比较实时)
系统环境
cat /etc/issue
CentOS release 6.6 (Final)
uname -sr
Linux 2.6.32-504.el6.x86_64
服务器规划
rsync服务器: 192.168.10.241
rsync + inotify-tools服务器 :192.168.10.231
配置rsync服务器 具体配置可以参照博客rsync+lsyncd中rsync的相关配置
1.配置rsync 客户端和inotify-tools实现文件实时同步
编辑rsync客户端,提供添加密码文件,并将权限修改为600
echo "123456" > /etc/backserver.pas
chmod 600 /etc/backserver.pas
ll /etc/backserver.pas
-rw------- 1 root root 7 Jul 27 15:34 /etc/backserver.pas
yum install inotify-tools -y
编写同步脚本
1.本地同步
vim Lsyncd_Local.sh
------------------------------------------------------------------------------------------------------------------------------
#!/bin/bash
Lsync_Local(){
LOG_PATH=/home/web
SrcPath=/opt/
/usr/bin/inotifywait -mrq --timefmt ‘%F %T‘ --format ‘%e %w%f %T‘ -e create,delete,modify,move,attrib $SrcPath |while read line
do
rsync -vzrtopg --progress --delete $SrcPath /tmp/back/
echo -n "$line" >> ${LOG_PATH}/inotify_Local_$(date "+%F").log
done
}
Lsync_Local
------------------------------------------------------------------------------------------------------------------------------
2.远程同步
# vim Lsync_Remote.sh
------------------------------------------------------------------------------------------------------------------------------
#!/bin/bash
Lsync_Remote(){
LOG_PATH=/home/web
SrcPath=/opt/
/usr/bin/inotifywait -mrq --timefmt ‘%F %T‘ --format ‘%e %w%f %T‘ -e create,delete,modify,move,attrib $SrcPath |while read line
do
rsync -vzrtopg --progress --delete $SrcPath root@192.168.10.241::home --password-file=/etc/backserver.pas
echo -n "$line" >> ${LOG_PATH}/inotify_Remote_$(date "+%F").log
done
}
Lsync_Remote
------------------------------------------------------------------------------------------------------------------------------
2.记录同步时间日志
tailf inotify_Local_2015-07-28.log
MODIFY /opt/test.sh 2015-07-28 16:14:33
CREATE /opt/test.bak 2015-07-28 16:15:33
MODIFY /opt/test.bak 2015-07-28 16:15:33
MODIFY /opt/test.bak 2015-07-28 16:17:34
MODIFY /opt/test.sh 2015-07-28 16:18:04
DELETE /opt/test.bak 2015-07-28 16:18:09
MODIFY /opt/test.sh 2015-07-28 16:24:35
MODIFY /opt/test.sh 2015-07-28 16:25:29
本文出自 “Linux之旅” 博客,请务必保留此出处http://openlinuxfly.blog.51cto.com/7120723/1679287
原文地址:http://openlinuxfly.blog.51cto.com/7120723/1679287