前言:

一、为什么要用Rsync+sersync架构?

1、sersync是基于Inotify开发的,类似于Inotify-tools的工具

2、sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的这个文件或者这个目录。


二、Rsync+Inotify-tools与Rsync+sersync这两种架构有什么区别?

1、Rsync+Inotify-tools

(1):Inotify-tools只能记录下被监听的目录发生了变化(包括增加、删除、修改),并没有把具体是哪个文件或者哪个目录发生了变化记录下来;

(2):rsync在同步的时候,并不知道具体是哪个文件或者哪个目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此,效率很低。

2、Rsync+sersync

(1):sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字;

(2):rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高。

小结:当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。



单向同步说明:

IP地址

角色

系统

安装软件

172.16.100.3 

Master服务器

CentOS 6.5

Rsync客户端+安装sersync

172.16.100.1

Slave服务器1

CentOS 6.5

Rsync服务端1

172.16.100.2

Slave服务器2

CentOS 6.5

Rsync服务端2

双向同步说明:

IP地址

角色

系统

安装软件

172.16.100.3 

Master服务器

CentOS 6.5

Rsync客户端+安装sersync

172.16.100.1

Slave服务器1

CentOS 6.5

Rsync客户端+安装sersync

172.16.100.2

Slave服务器2

CentOS 6.5

Rsync客户端+安装sersync




单向同步操作步骤:


第一部分:分别在两台slave服务器172.16.100.1/172.16.100.2上操作


1、关闭SElinux、配置防火墙

------------------------------------------------------------------------------------------------------------------------

(1)、vi /etc/selinux/config

SELINUX=disabled     #修改为disabled

# setenforce 0              #使配置立即生效 
# sestatus                     #查看

(2)、vi /etc/sysconfig/iptables  

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT  #允许873端口通过防火墙


(3)、重启防火墙

# /etc/init.d/iptables restart       #重启防火墙使配置生效

2、安装Rsync服务端软件

yum install -y rsync xinetd #安装

vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync   

disable = no #修改为no     

:wq! #保存退出

3、创建rsyncd.conf配置文件

vi /etc/rsyncd.conf #创建配置文件,添加以下代码

uid = root

gid = root

max connections= 36000

use chroot = no

strict modes = yes

log file = /var/log/rsyncd.log

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsyncd.lock               

[tongbu]                                                           

comment = web files
path = /opt/website

ignore errors = yes

read only = no

auth users = ming

secrets file = /etc/rsyncd.pwd 

#hosts allow = 172.16.100.1 

#hosts deny = *

备注:

同步目录的帐户和存放帐户和密码的文件可以不用设置

uid = root                                   #备份以什么身份进行,用户ID 
gid = root                                   #备份以什么身份进行,组ID 
use chroot = no                          #禁锢在源目录 
max connections = 36000           #最大连接数,0代表没有限制 
strict modes = yes                       #是否检查口令文件的权限 
pid file = /var/run/rsyncd.pid       #运行进程的pid文件 
log file = /var/log/rsyncd.log        #日志记录文件 
[tongbu]                                     #指定认证的备份模块名 
path = /opt/website                    #需要备份的目录的路径 
ignore errors = yes                      #忽略一些无关的IO错误 
read only = no                            #设置为no,即可以传至服务器的相应目录。 
write only = no                           #设置为no,表示客户端可以下载文件 
hosts allow = 172.16.100.1       #可以连接rsync服务器的主机的IP地址 
hosts deny = *                           #设置禁止连接rsync服务器的主机地址,*表示  拒绝所有除了hosts allow定义的 
auth users = ming                      #连接模块的用户名 
secrets file = /etc/rsyncd.pwd     #连接模块用户名的密码文件存放路径
4、创建用户认证文件
vi /etc/rsyncd.pwd #配置文件,添加以下内容

ming:ming.com  #格式,用户名:密码,可以设置多个,每行一个用户名:密码

:wq!  #保存,退出


ming:代表用户名  [系统是没有此用户的,也不用另外创建]

ming.com:代表密码

5、设置文件权限

chmod 600 /etc/rsyncd.conf  #设置文件所有者读取、写入权限

chmod 600 /etc/rsyncd.pwd  #设置文件所有者读取、写入权限


6、启动xinetd
/etc/init.d/xinetd start #启动(CentOS中是以xinetd来管理Rsync服务的)service xinetd stop   #停止service xinetd restart #重新启动7、启动rsync(以守护进程方式启动)

rsync --daemon

8加入开机自动启动


echo "rsync --daemon" >> /etc/rc.local

9、检查rsync是否启动


 lsof -i :873


第二部分:在Master源服务器172.16.100.3上操作

1、关闭SElinux、配置防火墙

------------------------------------------------------------------------------------------------------------------------

(1)、vi /etc/selinux/config

SELINUX=disabled     #修改为disabled

# setenforce 0              #使配置立即生效 
# sestatus                     #查看

(2)、vi /etc/sysconfig/iptables  

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT  #允许873端口通过防火墙


(3)、重启防火墙

# /etc/init.d/iptables restart       #重启防火墙使配置生效

2、安装Rsync客户端软件

whereis rsync                    #查看系统是否已安装rsync,出现下面的提示,说明已经安装

rsync: /usr/bin/rsync /usr/share/man/man1/rsync.1.gz

yum install -y xinetd         #只安装xinetd即可,CentOS中是以xinetd来管理rsync服务的

yum install rsync xinetd    #如果默认没有rsync,运行此命令进行安装rsync和xinetd

vi /etc/xinetd.d/rsync        #编辑配置文件,设置开机启动rsync

disable = no                      #修改为no

/etc/init.d/xinetd start         #启动(CentOS中是以xinetd来管理rsync服务的)

3、创建认证密码文件

vi /etc/rsyncd.pwd                    #编辑文件,添加以下内容

ming.com                                   #和服务端设置的密码一样

chmod 600 /etc/rsyncd.pwd     #设置文件权限,只设置文件所有者具有读取、写入权限即可

4、测试源服务器172.16.100.3到两台目标服务器172.16.100.1172.16.100.2 之间的数据同步

mkdir -p /opt/website/test              #在源服务器上创建测试文件夹

在Master源服务器上,执行以下两条命令:

rsync -avH --port=873 --progress --delete  /opt/website/  ming@172.16.100.1::tongbu --password-file=/etc/rsyncd.pwd

rsync -avH --port=873 --progress --delete  /opt/website/  ming@172.16.100.2::tongbu --password-file=/etc/rsyncd.pwd


运行完成后,分别在两台slave目标服务器172.16.100.1,172.16.100.2上查看,在/opt/website/目录下有test文件夹,说明数据同步成功。


第三部分:继续在源服务器172.16.100.3上操作

【安装Sersync工具,实时触发rsync进行同步】



1、查看服务器内核是否支持inotify

ll /proc/sys/fs/inotify   #列出文件目录,出现下面的内容,说明服务器内核支持inotify

-rw-r--r-- 1 root root 0 Mar  7 02:17 max_queued_events

-rw-r--r-- 1 root root 0 Mar  7 02:17 max_user_instances

-rw-r--r-- 1 root root 0 Mar  7 02:17 max_user_watches

备注:Linux下支持inotify的内核最小为2.6.13,可以输入命令:uname -a查看内核

CentOS 5.X 内核为2.6.18,默认已经支持inotify

2、修改inotify默认参数(inotify默认内核参数值太小)    [可省略此步骤]

查看系统默认参数值

sysctl -a | grep max_queued_events

结果是:fs.inotify.max_queued_events = 16384

sysctl -a | grep max_user_watches

结果是:fs.inotify.max_user_watches = 8192

sysctl -a | grep max_user_instances

结果是:fs.inotify.max_user_instances = 128

修改参数:

sysctl -w fs.inotify.max_queued_events="99999999"

sysctl -w fs.inotify.max_user_watches="99999999"

sysctl -w fs.inotify.max_user_instances="65535"

vi /etc/sysctl.conf #添加以下代码

fs.inotify.max_queued_events=99999999

fs.inotify.max_user_watches=99999999

fs.inotify.max_user_instances=65535

参数说明:

max_queued_events

inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确

max_user_watches

要同步的文件包含多少目录,可以用:find /data/share/tomcatrw/static_html -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/data/share/tomcatrw/static_html为同步文件目录)

max_user_instances

每个用户创建inotify实例最大值

3、安装sersync

sersync下载地址:

wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz

上传sersync2.5.4_64bit_binary_stable_final.tar.gz到/usr/local/src目录下

cd /usr/local/src

tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz  #解压

mv GNU-Linux-x86  /usr/local/sersync  #移动目录到/usr/local/sersync

4、配置sersync

cd  /usr/local/sersync #进入sersync安装目录

cp confxml.xml confxml.xml-bak  #备份原文件

vim confxml.xml  #编辑,修改下面的代码

<?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="/opt/website">

<remote ip="172.16.100.1" name="tongbu"/>

<remote ip="172.16.100.2" name="tongbu"/>

<!--<remote ip="192.168.8.40" name="tongbu"/>-->

</localpath>

<rsync>

<commonParams params="-artuz"/>

<auth start="true" users="ming" passwordfile="/etc/rsyncd.pwd"/><!--这里一定要设置为true,才会自动同步-->

<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="true" 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/p_w_picpaths"/>

</localpath>

</plugin>

</head>

*配置文件里"绿色字体"表示要根据实际情况修改的部分。


参数说明:

localpath watch="/opt/website":    #Master服务器同步目录

172.16.100.1,172.16.100.2:       #Slave服务器IP地址

name="tongbu":                           #Slave服务器rsync同步目录模块名称

users="ming":                                #Slave服务器rsync同步用户名

passwordfile="/etc/rsyncd.pwd":    #Slave服务器rsync同步用户的密码在源服务器的存放路径

remote ip="172.16.100.1":               #Slave服务器ip,每行一个

remote ip="172.16.100.2":                #Slave服务器ip,每行一个

failLog path="/tmp/rsync_fail_log.sh" #脚本运行失败日志记录

start="true"                                        #设置为true,每隔600分钟执行一次全盘同步

auth start="true"                                #这里一定要设置为true,才会自动同步

5、设置sersync监控开机自动执行

vi /etc/rc.d/rc.local  #编辑,在最后添加一行

/usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.xml  #设置开机自动运行脚本

:wq!  #保存退出

6、添加脚本监控sersync是否正常运行

vim  /root/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

:wq!  #保存退出


chmod +x /root/check_sersync.sh #添加脚本执行权限

vi /etc/crontab #编辑,在最后添加下面一行

*/5 * * * *  /root/check_sersync.sh > /dev/null 2>&1  #每隔5分钟执行一次脚本

service crond reload  #重新加载服务


7、测试sersync实时触发rsync同步脚本是否正常运行

在Master服务器172.16.100.3上创建文件inotify_rsync_ceshi

mkdir -p /opt/website/inotify_rsync_ceshi

重新启动Master服务器:172.16.100.3

等系统启动之后,查看两台Slave服务器172.16.100.1,172.16.100.2的/opt/website/下是否有inotify_rsync_ceshi文件夹

然后再在Master服务器172.16.100.3创建文件夹inotify_rsync_ceshi_new

mkdir -p /opt/website/inotify_rsync_ceshi_new

继续查看两台目标服务器172.16.100.1,172.16.100.2的/opt/website/下是否有inotify_rsync_ceshi_new文件夹

如果以上测试都通过,说明inotify实时触发rsync同步脚本运行正常。


至此,Linux下Rsync+sersync实现数据实时单向同步完成。




双向同步操作步骤
第一部分:在Master源服务器172.16.100.3上操作


1、创建rsyncd.conf配置文件

vi /etc/rsyncd.conf #创建配置文件,添加以下代码

uid = root

gid = root

max connections= 36000

use chroot = no

strict modes = yes

log file = /var/log/rsyncd.log

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsyncd.lock               

[tongbu]                                                           

comment = web files
path = /opt/website

ignore errors = yes

read only = no

auth users = steven

secrets file = /etc/rsyncd.txt

#hosts allow = 172.16.100.1 

#hosts deny = *

*配置文件里"绿色字体"表示要修改的部分,不能和其他的配置文件一样。


备注:

uid = root                                   #备份以什么身份进行,用户ID 
gid = root                                   #备份以什么身份进行,组ID 
use chroot = no                          #禁锢在源目录 
max connections = 36000           #最大连接数,0代表没有限制 
strict modes = yes                       #是否检查口令文件的权限 
pid file = /var/run/rsyncd.pid       #运行进程的pid文件 
log file = /var/log/rsyncd.log        #日志记录文件 
[tongbu]                                     #指定认证的备份模块名 
path = /opt/website                    #需要备份的目录的路径 
ignore errors = yes                      #忽略一些无关的IO错误 
read only = no                            #设置为no,即可以传至服务器的相应目录。 
write only = no                           #设置为no,表示客户端可以下载文件 
hosts allow = 172.16.100.1       #可以连接rsync服务器的主机的IP地址 
hosts deny = *                           #设置禁止连接rsync服务器的主机地址,*表示  拒绝所有除了hosts allow定义的 
auth users = steven                    #连接模块的用户名 
secrets file = /etc/rsyncd.txt        #连接模块用户名的密码文件存放路径
2、创建用户认证文件
vi /etc/rsyncd.txt    #配置文件,添加以下内容

steven:steven.com     #格式,用户名:密码,可以设置多个,每行一个用户名:密码

:wq!  #保存,退出


steven:       代表用户名  [系统是没有此用户的,也不用另外创建]

steven.com:代表密码

3、设置文件权限

chmod 600 /etc/rsyncd.conf  #设置文件所有者读取、写入权限

chmod 600 /etc/rsyncd.txt    #设置文件所有者读取、写入权限


4、启动xinetd
/etc/init.d/xinetd start #启动(CentOS中是以xinetd来管理Rsync服务的)service xinetd stop   #停止service xinetd restart #重新启动5、启动rsync(以守护进程方式启动)

rsync --daemon

6加入开机自动启动


echo "rsync --daemon" >> /etc/rc.local

7、检查rsync是否启动


 lsof -i :873


第二部分:在Slave服务器172.16.100.1/172.16.100.2上操作

【安装Sersync工具,实时触发rsync进行同步】


1、安装sersync

sersync下载地址:

wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz

上传sersync2.5.4_64bit_binary_stable_final.tar.gz到/usr/local/src目录下

cd /usr/local/src

tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz  #解压

mv GNU-Linux-x86  /usr/local/sersync  #移动目录到/usr/local/sersync

2、配置sersync

cd  /usr/local/sersync #进入sersync安装目录

cp confxml.xml confxml.xml-bak  #备份原文件

vim confxml.xml  #编辑,修改下面的代码

<?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="/opt/website">

<remote ip="172.16.100.3" name="tongbu"/>

<!--<remote ip="192.168.8.40" name="tongbu"/>-->

</localpath>

<rsync>

<commonParams params="-artuz"/>

<auth start="true" users="steven" passwordfile="/etc/rsyncd.txt"/><!--这里一定要设置为true,才会自动同步-->

<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="true" 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/p_w_picpaths"/>

</localpath>

</plugin>

</head>

*配置文件里"绿色字体"表示要修改的部分。


参数说明:

localpath watch="/opt/website":    #Master服务器同步目录

name="tongbu":                           #rsync同步目录模块名称

users="steven":                              #rsync同步用户名

passwordfile="/etc/rsyncd.txt":       #rsync同步用户的密码在源服务器的存放路径

remote ip="172.16.100.3":               #Master服务器ip,每行一个

failLog path="/tmp/rsync_fail_log.sh" #脚本运行失败日志记录

start="true"                                        #设置为true,每隔600分钟执行一次全盘同步

auth start="true"                                #这里一定要设置为true,才会自动同步

3、设置sersync监控开机自动执行

vi /etc/rc.d/rc.local  #编辑,在最后添加一行

/usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.xml  #设置开机自动运行脚本

:wq!  #保存退出

4、添加脚本监控sersync是否正常运行

vim  /root/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

:wq!  #保存退出


chmod +x /root/check_sersync.sh #添加脚本执行权限

vi /etc/crontab #编辑,在最后添加下面一行

*/5 * * * *  /root/check_sersync.sh > /dev/null 2>&1  #每隔5分钟执行一次脚本

service crond reload  #重新加载服务

5、测试sersync实时触发rsync同步脚本是否正常运行

在Master服务器172.16.100.3上创建文件inotify_rsync_ceshi

mkdir -p /opt/website/inotify_rsync_ceshi

重新启动Master服务器:172.16.100.3

等系统启动之后,查看两台Slave服务器172.16.100.1,172.16.100.2的/opt/website/下是否有inotify_rsync_ceshi文件夹

然后再在Master服务器172.16.100.3创建文件夹inotify_rsync_ceshi_new

mkdir -p /opt/website/inotify_rsync_ceshi_new

继续查看两台目标服务器172.16.100.1,172.16.100.2的/opt/website/下是否有inotify_rsync_ceshi_new文件夹

如果以上测试都通过,说明inotify实时触发rsync同步脚本运行正常。


至此,Linux下Rsync+sersync实现数据实时双向同步完成。