版本3.0:Sersync实现主机间的数据同步
一:Sersync简介
Sersync利用inotify与rsync对服务器进行实时同步,其中inotify用于监控文件系统事件,rsync是目前广泛使用的同步算法,其优点是只对文件不同的部分进行操作,所以其优势大大超过使用挂接文件系统的方式进行镜像同步。由金山的周洋开发完成,是目前使用较多的文件同步工具之一。该工具和其他的工具相比有如下优点:
· sersync是使用c++编写,由于只同步发生更改的文件,因此比其他同步工具更节约时间、带宽;
· 安装方便、配置简单;
· 使用多线程进行同步,能够保证多个服务器实时保持同步状态;
· 自带出错处理机制,通过失败队列对出错的文件重新出错,如果仍旧失败,则每10个小时对同步失败的文件重新同步;
· 自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次;
· 自带socket与http协议扩展,你可以方便的进行二次开发;
二:Sersync的安装配置(只需在客户端安装Sersync,)
1:客户端和服务器端分别安装rsync,服务器端(1.147)编辑配置文件,参照 版本1.0
2:在客户端安装配置Sersyns
wget http://sersync.googlecode.com/files/sersync2.5_64bit_binary_stable_final.tar.gz
tar xzvf sersync2.5_64bit_binary_stable_final.tar.gz
mv GNU-Linux-x86 /usr/local/sersync
3: 配置文件分析
vim /usr/local/sersync/confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host> #针对插件的保留字段,保留默认即可。
<debug start="true"/> #在sersync正在运行的控制台,打印inotify,rsync同步命令。
<fileSystem xfs="false"/> #对于xfs文件系统用户,需要将这个选项开启才正常工作.
<filter start="false"> #过滤系统的临时文件,被过滤的文件不会被监控提高
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify> #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="/data/test"> #监控本地的目录
<remote ip="192.168.1.147" name="backup"/> #远程接受同步的IP和rsync模块名
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="www" passwordfile="/etc/rsyncd.secret"/>
#开启用户认证,定义用户名密码
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<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="600"><!--600mins--> #每隔600s会做一次完全同步
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/> #调用name参数指定的插件。
</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>
</head>
4:Sersync的应用
A:启动服务
/usr/local/sersync/sersync2 -d -r -n 8 -o /usr/local/sersync/confxml.xml
-d 后台启动 -r同步前将已存在的文件全部同步过去 -n开启的线程总数默认10 -o指定配置文件
B:监控Sersync运行状态的脚本,如果服务停止了就重启服务
vim /root/tool/sersync.sh
#!/bin/bash
SERSYNC="/usr/local/sersync/sersync2"
CONF_FILE="/usr/local/sersync/confxml.xml"
STATUS=$(ps aux |grep 'sersync2'|grep -v'grep'|wc -l)
if [ $STATUS -eq 0 ];
then
$SERSYNC -d -r -o $CONF_FILE
else
exit 0;
fi
三:测试
在客户端监控的目录/data/test创建文件,然后查看服务器端backup模块对应的目录是否同步更新
