sersync 实现实时数据同步 —— 筑梦之路

sersync类似于inotify,同样用于监控,但它克服了inotify的缺点.

inotify最大的不足是会产生重复事件,或者同一个目录下多个文件的操作会产生多个事件,例如,当监控目录中有5个文件时,删除目录时会产生6个监控事件,从而导致重复调用rsync命令。比如:vim文件时,inotify会监控到临时文件的事件,但这些事件相对于rsync来说是不应该被监控的

sersync 优点:

  • sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤,所以在结合rsync同步的时候,节省了运行时耗和网络资源,因此更快。
  • sersync配置很简单,其中提供了静态编译好的二进制文件和xml配置文件,直接使用即可
  • sersync使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态
  • sersync有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对同步失败的文件重新同步
  • sersync不仅可以实现实时同步,另外还自带crontab功能,只需在xml配置文件中开启,即也可以按要求隔一段时间整体同步一次,而无需再额外配置crontab功能
  •  自带socket和HTTP协议扩展,定制特殊需求,二次开发

上图执行原理解释

  1. Inotify监控指定目录对应事件的变化,当有事件变化时进入事件过滤队列。

  2. 过滤队列负责过滤掉不需要触发复制的数据,也可以过滤短事件内产生的重复inotify事件信息,过滤后

    的事件触发Rsync对应变化数据进行复制。

  3. 图中线程组(线程是等待线程队列的守护线程),当事件队列中有事件发生后,线程组守护线程会逐个

       唤醒复制线程,当队列Inotify事件较多的时候,复制线程就会全部唤醒一起工作,提升复制效率。

  4. 除了线程组以外,还有Sersync服务线程负责处理复制失败的文件,它们再次复制,对于再次复制失败的

      文件,会记录到rsync_fail_log.sh脚本中,然后再定期执行脚本,同时利用自带的cron功能,实现每隔一段

            时间将所有未复制的数据整体复制。

Sersync部署

确保rsync服务正确,c/s端

rsync -avz --delete /data/ [email protected]::backup

确保当前客户端机器,是否支持Inotify

ls /proc/sys/fs/inotify/

max_queued_events  max_user_instances  max_user_watches

Sersync安装

https://github.com/wsgzao/sersync

下载获取sersync软件包,上传至linux

ls /MyInotify/tools/

sersync_installdir_64bit.zip

pwd

/MyInotify/tools/sersync_installdir_64bit/sersync/bin

chmod +x sersync

Sersync配置文件

修改配置文件,修改如下部分

vim /MyInotify/tools/sersync_installdir_64bit/sersync/conf/confxml.xml

# 配置文件解释
# sersync的过滤功能,排除指定的文件不复制

  6     <filter start="false">
  7         <exclude expression="(.*)\.svn"></exclude>
  8         <exclude expression="(.*)\.gz"></exclude>
  9         <exclude expression="^info/*"></exclude>
 10         <exclude expression="^static/*"></exclude>
 11     </filter>
 
# 指定监控事件,也就是触发监控管理的事件,true开启,false关闭
 12     <inotify>
 13         <delete start="true"/>
 14         <createFolder start="true"/>
 15         <createFile start="false"/>
 16         <closeWrite start="true"/>
 17         <moveFrom start="true"/>
 18         <moveTo start="true"/>
 19         <attrib start="false"/>
 20         <modify start="false"/>
 21     </inotify>
 
 # sersync主要功能点
 23     <sersync>
 24         <localpath watch="/data">
 # 填写rsync同步服务端ip地址,以及同步的模块名,支持复制到多台服务器
 25             <remote ip="192.168.178.110" name="bakcup"/>
 26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
 27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 28         </localpath>
 
 # 配置rsync的信息
 29         <rsync>
 # 命令的参数
 30             <commonParams params="-artuz"/>
 # 填写rsync服务端验证的账号,密码文件
 31             <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
 32             <userDefinedPort start="false" port="874"/><!-- port=874 -->
 33             <timeout start="false" time="100"/><!-- timeout=100 -->
 34             <ssh start="false"/>
 35         </rsync>


Sersync部署应用

生成sersync命令快捷键

ln -s /MyInotify/tools/sersync_installdir_64bit/sersync/bin/sersync /usr/local/bin/

sersync -h

set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_______________________________________________________

参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序

sersync -r -d -o /MyInotify/tools/sersync_installdir_64bit/sersync/conf/confxml.xml

set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -r     rsync all the local files to the remote servers before the sersync work
option: -d     run as a daemon
option: -o     config xml name:  /MyInotify/tools/sersync_installdir_64bit/sersync/conf/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost    host port: 8008
daemon start,sersync run behind the console
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data && rsync -artuz -R --delete ./ 192.168.100.110::bakcup >/dev/null 2>&1
run the sersync:
watch path is: /data

添加sersync命令至开机启动

echo "/MyInotify/tools/sersync_installdir_64bit/sersync/bin/sersync -d" >> /etc/rc.local

tail -1 /etc/rc.local

/MyInotify/tools/sersync_installdir_64bit/sersync/bin/sersync -d

猜你喜欢

转载自blog.csdn.net/qq_34777982/article/details/127186729