Sersync+Rsync实现触发式文件同步实战过程

 更新时间:2020年4月25日 17:29  点击:2239

背景

  通常我们在服务器上使用rsync加上crontab来定时地完成一些同步、备份文件的任务。随着业务和应用需求的不断扩大、实时性要求越来越高。一般rsync是通过校验所有文件后,进行差量同步,如果文件量十分庞大,那么rsync进行校验的过程也是十分耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过crontab方式进行触 发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。而Sersync+Rsync的组合能够较好地解决这种问题。

Sersync介绍

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

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

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

4、本项目有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则每10个小时对同步失败的文件重新同步。

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

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

实战过程

一、服务器环境

服务端:172.16.57.26 centos6.7 rsync-server 接收文件

客户端:172.16.57.25 centos6.7 sersync+rsync-client 发送文件

二、服务端安装rsync-server

1、安装rsync

# rpm -qa | grep rsync #查看rsync是否已经安装,如果没有安装,yum install直接安装即可

2、使用xinetd方式启动rsync

# vim /etc/xinetd.d/rsync #修改disable = no,flags = IPv4

3、修改rsync配置文件

# mkdir /etc/rsyncd
# vim /etc/rsyncd/rsyncd.conf #修改配置文件如下
# GLOBAL OPTIONS
motd file=/etc/motd
port=873
pid file=/var/run/rsyncd.pid
lock file = /var/lock/rsyncd
log file=/var/log/rsyncd
transfer logging = yes
log format = [op]:%o [ip]:%a [module]:%m [path]:%P [file]:%f [size]:%l
syslog facility=daemon
max connections=100
[recv]
comment = "recv data from 57.25"
path = /opt/rsync_data/recv #这边的目录的宿主要改为apprun,在这里同步过程中使用的是普通账户apprun
list = yes
use chroot = yes
uid = apprun
gid = apprun
read only = no
write only = no
exclude =
include =
auth users = rsync
secrets file = /etc/rsyncd/rsyncd.secrets
strict modes = yes
hosts allow = 172.16.57.25
hosts deny = *
# ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf

4、建立用户认证文件

# vim /etc/rsyncd/rsyncd.secrets
rsync:111111   #格式  用户名:口令
#chmod 600 /etc/rsyncd/rsyncd.secrets #权限设为600,否则启动会报错

5、启动rsync

# /etc/init.d/xinetd start
# netstat -tpln | grep 873 #查看873端口是否已经在监听了

三、客户端安装sersync+rsync-client

1、安装rsync,和服务端一样,没有安装的话yum install安装

2、安装sersync

# tar xzvf sersync2.5_64bit_binary_stable_final.tar.gz
# mv GNU-Linux-x86 /opt/programs/sersync #解压并拷贝到安装目录

3、配置sersync

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
  <host hostip="localhost" 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="true"/>
  <closeWrite start="true"/>
  <moveFrom start="true"/>
  <moveTo start="true"/>
  <attrib start="true"/>
  <modify start="true"/>
  </inotify>
  <sersync>
  <localpath watch="/opt/rsync_data/send"> #监控目录,一旦本地目录有文件变化,将同步到服务端
    <remote ip="172.16.57.26" name="recv"/>#服务端ip和同步模块
  </localpath>
  <rsync>
    <commonParams params="-artuz"/> #rsync同步参数
    <auth start="true" users="rsync" passwordfile="/etc/rsync.pas"/> #服务端认证密码
    <userDefinedPort start="false" 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="false" schedule="600"><!--600mins-->
    <crontabfilter start="false">
    <exclude expression="*.php"></exclude>
    <exclude expression="info/*"></exclude>
    </crontabfilter>
  </crontab>
  <plugin start="false" name="command"/>
  </sersync>
</head>

4、服务端密码认证

# vim /etc/rsync.pas #在相应的目录下配置身份验证文件,里面输入服务端的密码,并chmod 600
# chmod 600 /etc/rsync.pas

5、启动sersync

# ./sersync2 -d -o confxml.xml

四、测试认证

在客户端下监控目录/opt/rsync_data/send下添加文件或者删除,服务端的接受目录都会实时地进行更新。

在此例中,服务器iptables和selinux均处于关闭状态。

note: 这种方法同步文件的时候,同步文件的数量如果很多,可能会有部分文件在同步过程中缺失。查阅相关资料后,找到了如下的解决方案。由于本例中,使用的是xinetd方式启动的rsync服务,在xinetd的配置文件中,修改几个参数如下:

# vim /etc/xinetd.conf

修改几个参数:

 cps       = 500 30
 instances    = UNLIMITED
 per_source   = UNLIMITED

总结

以上所述是小编给大家介绍的Sersync+Rsync实现触发式文件同步,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对猪先飞网站的支持!

[!--infotagslink--]

相关文章