一般咱们在服务器上使用rsync加上crontab来定时地完成一些同步、备份文件的任务。随着业务和应用需求的不断扩大、实时性要求愈来愈高。通常rsync是经过校验全部文件后,进行差量同步,若是文件量十分庞大,那么rsync进行校验的过程也是十分耗时的。并且正在发生变化的每每是其中不多的一部分,这是很是低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它能够经过crontab方式进行触 发同步,可是两次触发动做必定会有时间差,这样就致使了服务端和客户端数据可能出现不一致,没法在应用故障时彻底的恢复数据。而Sersync+Rsync的组合可以较好地解决这种问题。php
服务端:172.16.57.26 centos6.7 rsync-server 接收文件linux
客户端:172.16.57.25 centos6.7 sersync+rsync-client 发送文件c++
rpm -qa | grep rsync #查看rsync是否已经安装,若是没有安装,yum install直接安装便可
2. 使用xinetd方式启动rsyncexpress
vim /etc/xinetd.d/rsync #修改disable = no,flags = IPv4
3. 修改rsync配置文件vim
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. 创建用户认证文件centos
vim /etc/rsyncd/rsyncd.secrets test:111111 #格式 用户名:口令 chmod 600 /etc/rsyncd/rsyncd.secrets #权限设为600,不然启动会报错
5. 启动rsyncbash
/etc/init.d/xinetd start netstat -tpln | grep 873 #查看873端口是否已经在监听了
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>
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均处于关闭状态。
---------------------------------------------
2016.7.24更新
这种方法同步文件的时候,同步文件的数量若是不少,可能会有部分文件在同步过程当中缺失。查阅相关资料后,找到了以下的解决方案。因为本例中,使用的是xinetd方式启动的rsync服务,在xinetd的配置文件中,修改几个参数以下:
vim /etc/xinetd.conf 修改几个参数: cps = 500 30 instances = UNLIMITED per_source = UNLIMITED
具体的含义能够本身man xinetd.conf查看一下。
目前尚未同步大文件的需求,可能同步大文件存在一些延迟,还有待解决。