天天看点

sersync:基于 rsync + inotify 实现数据实时同步

sersync:基于 rsync + inotify 实现数据实时同步

<a target="_blank"></a>

提到数据同步就必然会谈到<code>rsync</code>,一般简单的服务器数据传输会使用<code>ftp/sftp</code>等方式,但是这样的方式效率不高,不支持差异化增量同步也不支持实时传输。针对数据实时同步需求大多数人会选择<code>rsync+inotify-tools</code>的解决方案,但是这样的方案也存在一些缺陷(文章中会具体指出),<code>sersync</code>是国人基于前两者开发的工具,不仅保留了优点同时还强化了实时监控,文件过滤,简化配置等功能,帮助用户提高运行效率,节省时间和网络资源。

可靠高效的数据实时同步方式
synchronize files and folders between servers -using inotiy and rsync with c++ 服务器实时同步文件,服务器镜像解决方案

sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。目前使用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google开源项目openduckbill(依赖于inotify- tools),这两个都是基于脚本语言编写的。相比较上面两个项目,本项目优点是:

sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤(详细见附录,这个过滤脚本程序没有实现),所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。

相比较上面两个项目,sersync配置起来很简单,其中bin目录下已经有基本上静态编译的2进制文件,配合bin目录下的xml配置文件直接使用即可。

另外本项目相比较其他脚本开源项目,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。

本项目有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对同步失败的文件重新同步。

本项目自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次。无需再额外配置crontab功能。

本项目socket与http插件扩展,满足您二次开发的需要。

针对上图的设计架构,这里做几点说明,来帮助大家阅读和理解该图

1 ) 线程组线程是等待线程队列的守护线程,当事件队列中有事件产生的时候,线程组守护线程就会逐个唤醒同步线程。当队列中 inotify 事件较多的时候,同步线程就会被全部唤醒一起工作。这样设计的目的是为了能够同时处理多个 inotify 事件,从而提升服务器的并发同步能力。同步线程的最佳数量=核数 x 2 + 2。

2 ) 那么之所以称之为线程组线程,是因为每个线程在工作的时候,会根据服务器上新写入文件的数量去建立子线程,子线程可以保证所有的文件与各个服务器同时同步。当要同步的文件较大的时候,这样的设计可以保证每个远程服务器都可以同时获得需要同步的文件。

3 ) 服务线程的作用有三个:

处理同步失败的文件,将这些文件再次同步,对于再次同步失败的文件会生成 rsync_fail_log.sh 脚本,记录失败的事件。

每隔10个小时执行 rsync_fail_log.sh 脚本一次,同时清空脚本。

crontab功能,可以每隔一定时间,将所有路径整体同步一次。

4 ) 过滤队列的建立是为了过滤短时间内产生的重复的inotify信息,例如在删除文件夹的时候,inotify就会同时产生删除文件夹里的文件与删除文件夹的事件,通过过滤队列,当删除文件夹事件产生的时候,会将之前加入队列的删除文件的事件全部过滤掉,这样只产生一条删除文件夹的事件,从而减轻了同步的负担。同时对于修改文件的操作的时候,会产生临时文件的重复操作。

注意主从配置的区别,记得调整selinux和防火墙

服务器a(主服务器)

服务器b(从服务器/备份服务器)

rsync默认tcp端口为873

在服务器b上安装rsync:

<code>cd /app/local</code>

<code>wget http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz</code>

<code>tar zxf rsync-3.1.1.tar.gz</code>

<code>cd rsync-3.1.1</code>

<code>./configure</code>

<code>make &amp;&amp; make install</code>

设置rsync的配置文件:

<code>vi /etc/rsyncd.conf</code>

<code>#服务器b上的rsyncd.conf文件内容</code>

<code>uid=root</code>

<code>gid=root</code>

<code>#最大连接数</code>

<code>max connections=36000</code>

<code>#默认为true,修改为no,增加对目录文件软连接的备份</code>

<code>use chroot=no</code>

<code>#定义日志存放位置</code>

<code>log file=/var/log/rsyncd.log</code>

<code>#忽略无关错误</code>

<code>ignore errors = yes</code>

<code>#设置rsync服务端文件为读写权限</code>

<code>read only = no</code>

<code>#认证的用户名与系统帐户无关在认证文件做配置,如果没有这行则表明是匿名</code>

<code>auth users = rsync</code>

<code>#密码认证文件,格式(虚拟用户名:密码)</code>

<code>secrets file = /etc/rsync.pass</code>

<code>#这里是认证的模块名,在client端需要指定,可以设置多个模块和路径</code>

<code>[rsync]</code>

<code>#自定义注释</code>

<code>comment = rsync</code>

<code>#同步到b服务器的文件存放的路径</code>

<code>path=/app/data/site/</code>

<code>[img]</code>

<code>comment = img</code>

<code>path=/app/data/site/img</code>

创建rsync认证文件 可以设置多个,每行一个用户名:密码,注意中间以“:”分割

<code>echo "rsync:rsync" &gt; /etc/rsync.pass</code>

设置文件所有者读取、写入权限:

<code>chmod 600 /etc/rsyncd.conf</code>

<code>chmod 600 /etc/rsync.pass</code>

启动服务器b上的rsync服务:

<code># rsync --daemon -v</code>

<code>rsync --daemon</code>

监听端口873:

<code>netstat -an | grep 873</code>

<code>lsof -i tcp:873</code>

<code></code>

<code>command pid user fd type device size/off node name</code>

<code>rsync 31445 root 4u ipv4 443872 0t0 tcp *:rsync (listen)</code>

<code>rsync 31445 root 5u ipv6 443873 0t0 tcp *:rsync (listen)</code>

设置rsync为服务启动项(可选):

<code>echo "/usr/local/bin/rsync --daemon" &gt;&gt; /etc/rc.local</code>

要 kill rsync 进程,不要用 kill -hup {pid} 的方式重启进程,以下3种方式任选:

<code># ps -ef|grep rsync|grep -v grep|awk '{print $2}'|xargs kill -9</code>

<code># cat /var/run/rsyncd.pid | xargs kill -9</code>

<code>pkill rsync</code>

再次启动:

<code>/usr/local/bin/rsync --daemon</code>

安装rsync:

安装inotify-tools:

<code>wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz</code>

<code>tar zxf inotify-tools-3.14.tar.gz</code>

<code>cd inotify-tools-3.14</code>

<code>./configure --prefix=/app/local/inotify</code>

安装sersync:

<code>wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz</code>

<code>tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz</code>

<code>mv /app/local/gnu-linux-x86/ /app/local/sersync</code>

<code>cd /app/local/sersync</code>

配置下密码文件,因为这个密码是要访问服务器b需要的密码和上面服务器b的密码必须一致:

<code>echo "rsync" &gt; /app/local/sersync/user.pass</code>

修改权限:

<code>chmod 600 /app/local/sersync/user.pass</code>

修改confxml.conf:

<code>vi /app/local/sersync/confxml.xml</code>

<code>&lt;?xml version="1.0" encoding="iso-8859-1"?&gt;</code>

<code>&lt;head version="2.5"&gt;</code>

<code>&lt;host hostip="localhost" port="8008"&gt;&lt;/host&gt;</code>

<code>&lt;debug start="true"/&gt;</code>

<code>&lt;filesystem xfs="false"/&gt;</code>

<code>&lt;filter start="false"&gt;</code>

<code>&lt;exclude expression="(.*)\.php"&gt;&lt;/exclude&gt;</code>

<code>&lt;exclude expression="^data/*"&gt;&lt;/exclude&gt;</code>

<code>&lt;/filter&gt;</code>

<code>&lt;inotify&gt;</code>

<code>&lt;delete start="true"/&gt;</code>

<code>&lt;createfolder start="true"/&gt;</code>

<code>&lt;createfile start="false"/&gt;</code>

<code>&lt;closewrite start="true"/&gt;</code>

<code>&lt;movefrom start="true"/&gt;</code>

<code>&lt;moveto start="true"/&gt;</code>

<code>&lt;attrib start="false"/&gt;</code>

<code>&lt;modify start="false"/&gt;</code>

<code>&lt;/inotify&gt;</code>

<code>&lt;sersync&gt;</code>

<code>&lt;localpath watch="/home/"&gt; &lt;!-- 这里填写服务器a要同步的文件夹路径--&gt;</code>

<code>&lt;remote ip="8.8.8.8" name="rsync"/&gt; &lt;!-- 这里填写服务器b的ip地址和模块名--&gt;</code>

<code>&lt;!--&lt;remote ip="192.168.28.39" name="tongbu"/&gt;--&gt;</code>

<code>&lt;!--&lt;remote ip="192.168.28.40" name="tongbu"/&gt;--&gt;</code>

<code>&lt;/localpath&gt;</code>

<code>&lt;rsync&gt;</code>

<code>&lt;commonparams params="-artuz"/&gt;</code>

<code>&lt;auth start="true" users="rsync" passwordfile="/app/local/sersync/user.pass"/&gt; &lt;!-- rsync+密码文件 这里填写服务器b的认证信息--&gt;</code>

<code>&lt;userdefinedport start="false" port="874"/&gt;&lt;!-- port=874 --&gt;</code>

<code>&lt;timeout start="false" time="100"/&gt;&lt;!-- timeout=100 --&gt;</code>

<code>&lt;ssh start="false"/&gt;</code>

<code>&lt;/rsync&gt;</code>

<code>&lt;faillog path="/tmp/rsync_fail_log.sh" timetoexecute="60"/&gt;&lt;!--default every 60mins execute once--&gt;&lt;!-- 修改失败日志记录(可选)--&gt;</code>

<code>&lt;crontab start="false" schedule="600"&gt;&lt;!--600mins--&gt;</code>

<code>&lt;crontabfilter start="false"&gt;</code>

<code>&lt;exclude expression="*.php"&gt;&lt;/exclude&gt;</code>

<code>&lt;exclude expression="info/*"&gt;&lt;/exclude&gt;</code>

<code>&lt;/crontabfilter&gt;</code>

<code>&lt;/crontab&gt;</code>

<code>&lt;plugin start="false" name="command"/&gt;</code>

<code>&lt;/sersync&gt;</code>

<code>&lt;!-- 下面这些有关于插件你可以忽略了 --&gt;</code>

<code>&lt;plugin name="command"&gt;</code>

<code>&lt;param prefix="/bin/sh" suffix="" ignoreerror="true"/&gt; &lt;!--prefix /opt/tongbu/mmm.sh suffix--&gt;</code>

<code>&lt;include expression="(.*)\.php"/&gt;</code>

<code>&lt;include expression="(.*)\.sh"/&gt;</code>

<code>&lt;/plugin&gt;</code>

<code>&lt;plugin name="socket"&gt;</code>

<code>&lt;localpath watch="/home/demo"&gt;</code>

<code>&lt;deshost ip="210.36.158.xxx" port="8009"/&gt;</code>

<code>&lt;plugin name="refreshcdn"&gt;</code>

<code>&lt;localpath watch="/data0/htdocs/cdn.markdream.com/site/"&gt;</code>

<code>&lt;cdninfo domainname="cdn.chinacache.com" port="80" username="xxxx" passwd="xxxx"/&gt;</code>

<code>&lt;sendurl base="http://cdn.markdream.com/cms"/&gt;</code>

<code>&lt;regexurl regex="false" match="cdn.markdream.com/site([/a-za-z0-9]*).cdn.markdream.com/images"/&gt;</code>

<code>&lt;/head&gt;</code>

运行sersync:

<code>nohup /app/local/sersync/sersync2 -r -d -o /app/local/sersync/confxml.xml &gt;/app/local/sersync/rsync.log 2&gt;&amp;1 &amp;</code>

<code>nohup /app/local/sersync/sersync2 -r -d -o /app/local/sersync/img.xml &gt;/app/local/sersync/img.log 2&gt;&amp;1 &amp;</code>

-d:启用守护进程模式

-r:在监控前,将监控目录与远程主机用rsync命令推送一遍

-n: 指定开启守护线程的数量,默认为10个

-o:指定配置文件,默认使用confxml.xml文件

本文来自云栖社区合作伙伴“linux中国”,原文发布日期:2015-08-19