在 《用 rsync sersync两个工具实现文件备份及同步 一》 中,我们说了rsync的安装配置,使用rsync可以做同步备份等任务。如果想要实现 客户端监视文件变化实时同步到服务器段的话,我们可以使用 sersync 工具。
注: 操作环境接《用 rsync sersync两个工具实现文件备份及同步 一》
实验目的:
达到将192。168.18.42 /home/tongbu 文件夹中的内容进行监控,同步到192.168.18.46 /home/tongbu 中
一、首先在服务器端(192.168.18.46)添加模块
vi /etc/rsyncd.conf
[tongbu]
path = /home/tongbu
read only = no
#write only = yes
host allow = *
auth users = haifeng
secrets file = /etc/rsyncd.scrt
二、在客户端安装 sersync 工具
1.下载安装包
wget http://sersync.googlecode.com/files/sersync2.5_64bit_binary_stable_final.tar.gz
2.解压安装包 进入目录
[root@lz ~]# tar zxf sersync2.5_64bit_binary_stable_final.tar.gz
[root@lz ~]# cd GNU-Linux-x86/
[root@lz GNU-Linux-x86]# ls
confxml.xml sersync2
3.编辑 confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="192.168.18.42" port="8008"></host> 本地主机地址
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/home/tongbu"> #要同步的本地路径
<remote ip="192.168.18.46" name="tongbu"/> #服务端ip 模块名
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-avz"/> #rsync的参数
<auth start="true" users="haifeng" passwordfile="/etc/rsyncd.scrt"/>
##为true的时候,开启密码验证
<userDefinedPort start="true" port="873"/><!-- port=873 --> #端口
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="true" schedule="1"><!--600mins-->
#为true 即多长时间监控一次 ,默认为600分钟,我们设置1分钟,方便实验
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/p_w_picpaths"/>
</localpath>
</plugin>
</head>
##设置完成后保存推出,注意密码文件 /etc/rsyncd.scrt 权限 设置为 600
4.启动sersync
[root@lz GNU-Linux-x86]# ./sersync2 -r -d -o /root/GNU-Linux-x86/confxml.xml
5.验证
在客户端/home/tongbu中写入文件,或更改文件,服务器端可以自动同步。
成功。 :)