实时同步最原始的方法是inotify+rsync,但是inotify有些缺陷。而rsync+sersync都能克服,配置起来也比较简单。
当同步的目录数据量不大时,建议使用rsync+inotify。当同步的目录数据量很大时(几百G甚至1T以上)文件很多时,建议使用rsync+sersync

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

sersync原理:
sersync记录下被监听目录中发生变化的(包括增加、删除、修改),然后自动触发rsync同步文件。



一、环境准备



IP地址:
sersync_rsync_client:192.168.1.103
rsync_server:192.168.1.189

操作系统:
centos 6.9 64位



rsync开太多个线程有什么影响 rsync支持多线程_xml


二、rsync部署(每步说明了在那里配置)



1、关闭SELINUX(sersync_rsync_client、rsync_server均配置)
vi /etc/selinux/config
修改下面一行代码:
SELINUX=disabled
运行此命令立即生效。
setenforce 0


2、开启防火墙tcp 873端口、或关掉防火墙(sersync_rsync_client、rsync_server均配置)
vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
或者关闭防火墙
service iptables stop
chkconfig iptables off


3、安装rsync服务端软件(sersync_rsync_client配置)
yum install rsync


4、安装xinetd(rsync_server配置)
yum install rsync xinetd
vi /etc/xinetd.d/rsync
修改disable的值为no:
disable = no

启动xinetd(CentOS中是以xinetd来管理Rsync服务的)
/etc/init.d/xinetd start
chkconfig xinetd on
rsync --daemon --config=/etc/rsyncd.conf
echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.local


5、创建rsyncd.conf配置文件(rsync_server配置。假如需要同步多个目录,注意加多个目录;此root是rsync的认证账号,后面步骤会配置认证账号和密码)
创建配置文件:
vi /etc/rsyncd.conf
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsyncd.secret
motd file = /etc/rsyncd.motd
[test]
path = /home/xyz/
comment = test
uid = root
gid = root
incoming chmod = Du=rwx,Dog=rx,Fu=rwx,Fgo=rx
port=873
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = root
hosts allow = 192.168.1.103
hosts deny = *
目录权限(如果uid和gid都是root,这里不用操作):
cd /home
chown -hR root.root xyz/   #如果xyz目录是在root下新建的,默认就是root权限。


6.创建用户认证文件(rsync_server)
配置文件
vi /etc/rsyncd.passwd
root:123456
保存退出


7.设置文件权限(rsync_server)
设置文件所有者读取、写入权限
chmod 600 /etc/rsyncd.conf
chmod 600 /etc/rsyncd.passwd


8.启动rsync(rsync_server)
/etc/init.d/xinetd start
参考指令
停止:service xinetd stop
启动:service xinetd restart


9、创建用户认证文件(sersync_rsync_client配置)
配置文件
vi /etc/rsyncd.passwd
123456
保存退出
chmod 600 /etc/rsyncd.passwd


10、从sersync_rsync_client手动rsync同步到rsync_server看下,只有成功后(查看日志),下面sersync才会成功。(sersync_rsync_client上运行此命令测试)
rsync -avzrtopgL --progress /root/ root@192.168.1.189::test/ --password-file=/etc/rsyncd.passwd




三、sersync部署(sersync_rsync_client配置)



1、查看服务器内核是否支持inotify
列出文件目录
ll /proc/sys/fs/inotify
出现下面的内容、说明服务器内核支持inotify
-rw-r--r-- 1 root root 0 Dec 25 12:03 max_queued_events
-rw-r--r-- 1 root root 0 Dec 25 15:05 max_user_instances
-rw-r--r-- 1 root root 0 Dec 25 12:03 max_user_watches
备注:centos6.9默认支持inotify


2.修改inotify默认参数(inotify默认内核参数值太小)
sysctl -a | grep max_queued_events
sysctl -a | grep max_user_watches
sysctl -a | grep max_user_instances
修改参数
sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"
生效
sysctl -p
参数说明:
max_queued_events:
inotify队列最大长度,如果值太小,会出现” Event Queue Overflow “错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /var/www/synctest -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/var/www/synctest为同步文件目录)
max_user_instances:
每个用户创建inotify实例最大值


3、最好更改最大连接数、最大文件描述符。
vi /etc/pam.d/login
session required /lib64/security/pam_limits.so
vi /etc/security/limits.conf
*                soft    nproc          65535
*                hard    nproc          65535
*                soft    nofile         65535
*                hard    nofile         65535
重启服务器


4、安装sersync
cd /usr/local/src
wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz
如果下载不了,用github这个
wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
mv GNU-Linux-x86 /usr/local/sersync
cd /usr/local/sersync


5、配置sersync
cp confxml.xml confxml.xml-bak
vi confxml.xml
设置如下代码:(如果需要同步多目录,就再建一个如confxmldata.xml,port端口8008不能冲突(测试好像不改也没关系),改成其他端口。localpath watch 改下,failLog path这个脚本文件也改下,每个xml文件中只保留自己的localpath watch )
<?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="false"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="false"/>
        <modify start="false"/>
    </inotify>
    <sersync>
        <localpath watch="/home/xyz">
            <remote ip="192.168.1.189" name="test"/>
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="root" passwordfile="/etc/rsyncd.passwd"/>
            <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="false" schedule="600"><!--600mins-->
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <plugin start="false" name="command"/>
    </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>
    <plugin name="socket">
        <localpath watch="/opt/tongbu">
            <deshost ip="192.168.138.20" port="8009"/>
        </localpath>
    </plugin>
    <plugin name="refreshCDN">
        <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
            <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
            <sendurl base="http://pic.xoyo.com/cms"/>
            <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
        </localpath>
    </plugin>
</head>
参数说明:
如果A服务器新建、删除目录都会触发同步到B服务器。但是A服务器删除文件,不会触发到B服务器。
localpath watch=”/home/xyz“
name=”test”: #目标服务器rsync同步目录模块名称
users=”root”: #目标服务器rsync同步用户名
passwordfile=”/etc/rsyncd.passwd”: #目标服务器rsync同步用户的密码在源服务器的存放路径
remote ip=”192.168.1.189”: #对端服务器ip
failLog path=”/tmp/rsync_fail_log.sh” #脚本运行失败日志记录
start=”true” #设置为true,每隔600分钟执行一次全盘同步
delete start   #只做增量备份,可以吧这个设置为flase。但是如果两边要一样就设置为true
<!-- :另外注释不用用#号,要注意


6、先执行下同步。
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml



7、设置sersync监控开机自动执行(如果同步多个目录,下面还需要加一条)
vi /etc/rc.d/rc.local
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml  (开机自启动,建议不要-r,只有第一次才需要加-r)
-o指定配置文件
-d在后台启动
-r在一个开始两台服务器不一致,先做一次同步,任何再有新数据就同步了



8、添加脚本监控sersync是否正常运行(如果需要同步多个目录,另外一个就用crontab做个定时执行上面那个命令,因为我没试过用下面这个脚本来检测两个文件,怕有bug)
vi /home/crontab/check_sersync.sh (没有目录先新建下)
#!/bin/sh
sersync="/usr/local/sersync/sersync2"
confxml="/usr/local/sersync/confxml.xml"
status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ];
then
$sersync -d -r -o $confxml &
else
exit 0;
fi

添加脚本执行权限
chmod +x /home/crontab/check_sersync.sh

做定时任务:
vi /etc/crontab
*/5 * * * * root /home/crontab/check_sersync.sh > /dev/null 2>&1

重新加载服务
/etc/init.d/crond restart


9、测试是否会实时同步



四、备注



1、日志在/var/log/rsyncd.log文件 2、验证触发同步命令 cd /usr/local/sersync/ ./sersync2 -d -r 或者 /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml  (-o应该是接文件)





转载于:https://blog.51cto.com/net881004/2346924