前言:@H_502_3@
一、为什么要用Rsync+sersync架构?@H_502_3@
1、sersync是基于Inotify开发的,类似于Inotify-tools的工具@H_502_3@
2、sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的这个文件或者这个目录。@H_502_3@
@H_502_3@
二、Rsync+Inotify-tools与Rsync+sersync这两种架构有什么区别?@H_502_3@
1、Rsync+Inotify-tools
@H_502_3@
(1):Inotify-tools只能记录下被监听的目录发生了变化(包括增加、删除、修改),并没有把具体是哪个文件或者哪个目录发生了变化记录下来;@H_502_3@
(2):rsync在同步的时候,并不知道具体是哪个文件或者哪个目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此,效率很低。@H_502_3@
2、Rsync+sersync@H_502_3@
(1):sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字;@H_502_3@
(2):rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高。@H_502_3@
小结:当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。@H_502_3@
环境说明:@H_502_3@
操作系统:CentOS 7.0@H_502_3@
源服务器:192.168.1.51@H_502_3@
目标服务器:192.168.1.52@H_502_3@
目的:@H_502_3@
把源服务器上/data/image /data/pic两个目录@H_502_3@
实时同步到目标服务器的data/image /data/pic下@H_502_3@
@H_502_3@
具体操作:@H_502_3@
一、目标服务器安装Rsync服务端@H_502_3@
vi /etc/selinux/config@H_502_3@ |
@H_502_3@
SELINUX=disabled@H_502_3@ |
setenforce 0#立即生效
@H_502_3@
2. 配置防火墙IPTABLES@H_502_3@
@H_502_3@
[root@master2 ~]# vim /etc/sysconfig/iptables |
@H_502_3@
增加规则:-A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT@H_502_3@ |
@H_502_3@
iptables -L -v -n 查看防火墙状态,873端口是否开放@H_502_3@ |
@H_502_3@
3、检查是否安装rsync
@H_502_3@
[root@master2 ~]# rpm -qa|grep rsync@H_502_3@ rsync-3.0.9-15.el7.x86_64@H_502_3@ |
@H_502_3@
@H_502_3@
@H_502_3@
vim /etc/rsyncd.conf@H_502_3@ |
#Rsync configuration:@H_502_3@ uid = root @H_502_3@ gid = root @H_502_3@ use chroot = no @H_502_3@ port = 873 @H_502_3@ max connections = 2000 @H_502_3@ timeout = 200 @H_502_3@ log file = /var/run/rsyncd.log @H_502_3@ pid file = /var/run/rsyncd.pid @H_502_3@ lock file = /var/run/rsyncd.lock @H_502_3@ read only = false @H_502_3@ auth users = lyc @H_502_3@ secrets file = /etc/rsyncd.secret @H_502_3@ @H_502_3@ hosts allow = 192.168.1.0/255 @H_502_3@ hosts deny = 0.0.0.0/32 @H_502_3@ list = yes@H_502_3@ ignore errors = yes@H_502_3@
[image] @H_502_3@ path = /data/image@H_502_3@
[pic]@H_502_3@ pate = /data/pic @H_502_3@
|
@H_502_3@
注解@H_502_3@ #Rsync configuration:@H_502_3@ uid = root #设置rsync运行权限为root@H_502_3@ gid = root #设置rsync运行权限为root@H_502_3@ use chroot = no # 安全相关,默认为true,修改为no,增加对目录文件软连接的备份@H_502_3@ port = 873 # 指定rsync服务的默认端口号@H_502_3@ max connections = 2000 # 并发连接数@H_502_3@ timeout = 200 # 超时时间(秒)@H_502_3@ log file = /var/run/rsyncd.log # 指定日志文件位置,启动rsync后自动产生这个文件,无需提前创建@H_502_3@ pid file = /var/run/rsyncd.pid # 指定rsync的pid目录@H_502_3@ lock file = /var/run/rsyncd.lock # 指定rsync的锁文件【重要】,支持max connections参数的锁文件@H_502_3@ read only = false # no客户端可上传文件,yes只读@H_502_3@ auth users = lyc #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开@H_502_3@ secrets file = /etc/rsyncd.secret #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件@H_502_3@ @H_502_3@ hosts allow = 192.168.1.0/255 #允许进行数据同步的客户端IP地址段,可以设置多个,用英文状态下逗>号隔开@H_502_3@ hosts deny = 0.0.0.0/32 #禁止数据同步的客户端IP地址,这里设置了不禁止@H_502_3@ ################################################# [image] # 模块@H_502_3@ path = /home/ces/ #rsync服务端数据目录路径@H_502_3@
|
5、创建rsync同步密码文件,并设置权限为600@H_502_3@
[root@master2 ~]# echo "lyc:test123" > /etc/rsyncd.secret@H_502_3@ [root@master2 ~]# chmod 600 /etc/rsyncd.secret@H_502_3@ [root@master2 ~]# ll //etc/rsyncd.secret@H_502_3@ -rw------- 1 root root 14 4月 18 09:25 /[root@master2 ~]# cat /etc/rsyncd.secret@H_502_3@ lyc:test123@H_502_3@ |
@H_502_3@
6.启动rsync守护进程,并写入开机自启动@H_502_3@
[root@master2 ~]# rsync --daemon@H_502_3@ [root@master2 ~]# ps -ef | grep rsync@H_502_3@ root 1662 1 0 09:34 ? 00:00:00 rsync --daemon@H_502_3@ root 6310 6068 0 17:02 pts/0 00:00:00 grep --color=auto rsync@H_502_3@ [root@master2 ~]# netstat -nulpt| grep rsync@H_502_3@ tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1662/rsync @H_502_3@ tcp6 0 0 :::873 :::* LISTEN 1662/rsync @H_502_3@
设置开机自启动,写入到/etc/rc.local里面@H_502_3@
vim /etc/rc.local@H_502_3@
# rsync server progress@H_502_3@ /usr/bin/rsync --daemon --config=/etc/rsyncd.conf 7.创建相关待同步的目录/home/ces/并授予权限@H_502_3@
@H_502_3@
[root@master2 ~]# mkdir -p /data/image/data/pic@H_502_3@ [root@master2 ~]# chown -R root.root /data/image 二、在源服务器安装配置Rsync服务端+配置sersync 1、按照上面步骤配置按照Rsync服务端,需要注意的是创建rsync同步密码文件,内容只需要填写密码:test123 @H_502_3@
[root@master1 ~]# echo "test123" > /etc/rsyncd.secret@H_502_3@ [root@master1 ~]# chmod 600 /etc/rsyncd.secret@H_502_3@ [root@master1 ~]# ll -rw------- 1 root root 14 4月 18 09:25 /etc/rsync.password@H_502_3@ [root@master1 ~]# cat /etc/rsyncd.secret@H_502_3@ test123@H_502_3@ 2、手动测试rsync同步情况,此步非常关键,如果测试不成功,后面的sersync配好了也不会同步数据。@H_502_3@
[root@master1 lyc]# rm -rf /test/@H_502_3@ [root@master1 lyc]# mkdir -p /test/@H_502_3@ [root@master1 lyc]# touch /test/lyc{1,2,3}{a,b,c}@H_502_3@ [root@master1 lyc]# ls /test/@H_502_3@ lyc1a lyc1b lyc1c lyc2a lyc2b lyc2c lyc3a lyc3b lyc3c@H_502_3@ [root@master1 lyc]# rsync -avzP /lyc/ lyc@192.168.1.52::rsync --password-file=/etc/rsyncd.secret@H_502_3@ sending incremental file list@H_502_3@ ./@H_502_3@ lyca@H_502_3@ 0 100% 0.00kB/s 0:00:00 (xfer#1,to-check=11/13)@H_502_3@ lycb@H_502_3@ 0 100% 0.00kB/s 0:00:00 (xfer#2,to-check=10/13)@H_502_3@ lycc@H_502_3@ 0 100% 0.00kB/s 0:00:00 (xfer#3,to-check=9/13)@H_502_3@ lycca@H_502_3@ 0 100% 0.00kB/s 0:00:00 (xfer#4,to-check=8/13)@H_502_3@ lyccb@H_502_3@ 0 100% 0.00kB/s 0:00:00 (xfer#5,to-check=7/13)@H_502_3@ lyccc@H_502_3@ 0 100% 0.00kB/s 0:00:00 (xfer#6,to-check=6/13)@H_502_3@ lycla@H_502_3@ 0 100% 0.00kB/s 0:00:00 (xfer#7,to-check=5/13)@H_502_3@ lyclb@H_502_3@ 0 100% 0.00kB/s 0:00:00 (xfer#8,to-check=4/13)@H_502_3@ lyclc@H_502_3@ 0 100% 0.00kB/s 0:00:00 (xfer#9,to-check=3/13)@H_502_3@ lycya@H_502_3@ 0 100% 0.00kB/s 0:00:00 (xfer#10,to-check=2/13)@H_502_3@ lycyb@H_502_3@ 0 100% 0.00kB/s 0:00:00 (xfer#11,to-check=1/13)@H_502_3@ lycyc@H_502_3@ 0 100% 0.00kB/s 0:00:00 (xfer#12,to-check=0/13)@H_502_3@ sent 550 bytes received 239 bytes 1578.00 bytes/sec@H_502_3@ total size is 0 speedup is 0.00@H_502_3@ 3、在源服务器上执行推送命令后,在目的服务器上查看同步目录/home/ces中的内容,如果内容同步完成,进行sersync的配置;未完成,检查rsync配置及认证用户密码信息。@H_502_3@
三、源服务器上开始部署sersync服务@H_502_3@
1、下载sersync
@H_502_3@
@H_502_3@
@H_502_3@
@H_502_3@
@H_502_3@
@H_502_3@
@H_502_3@
@H_502_3@
[root@master1 lyc]#tar -zxf sersync2.5.4_64bit_binary_stable_final.tar.gz-C/usr/local/@H_502_3@ [root@master1 lyc]#mvGNU-Linux-x86/usr/local/sersync@H_502_3@ |
由于谷歌的原因,不能成功下载的话,请手动下载上传到源服务器端@H_502_3@
@H_502_3@
2、配置sersync@H_502_3@
@H_502_3@
[root@master1 local]#mkdir -p sersync/bin sersync/image sersync/pic@H_502_3@ [root@master1 local]#cp sersync/confxml.xmlsersync/image/@H_502_3@ [root@master1 local]#mv sersync/serynce2sersync/bin/@H_502_3@
|
@H_502_3@
@H_502_3@
3、修改配置文件image/confxml.xml、pic/confxml.xml
@H_502_3@
@H_502_3@
[root@master1 local]#vimsersync/image/confxml.xml |
@H_502_3@
@H_502_3@
<?xml version="1.0" encoding="ISO-8859-1"?>@H_502_3@ <head version="2.5">@H_502_3@ <host hostip="localhost" port="8008"></host>@H_502_3@ <debug start="false"/>@H_502_3@ <fileSystem xfs="false"/>@H_502_3@ <filter start="false">@H_502_3@ <exclude expression="(.*)\.svn"></exclude>@H_502_3@ <exclude expression="(.*)\.gz"></exclude>@H_502_3@ <exclude expression="^info/*"></exclude>@H_502_3@ <exclude expression="^static/*"></exclude>@H_502_3@ </filter>@H_502_3@ <inotify>@H_502_3@ <delete start="true"/>@H_502_3@ <createFolder start="true"/>@H_502_3@ <createFile start="false"/>@H_502_3@ <closeWrite start="true"/>@H_502_3@ <moveFrom start="true"/>@H_502_3@ <moveTo start="true"/>@H_502_3@ <attrib start="false"/>@H_502_3@ <modify start="false"/>@H_502_3@ </inotify>@H_502_3@
<sersync>@H_502_3@ <localpath watch="/data/image">@H_502_3@ <remote ip="192.168.1.52" name="image"/>@H_502_3@ <!--<remote ip="192.168.8.39" name="tongbu"/>-->@H_502_3@ <!--<remote ip="192.168.8.40" name="tongbu"/>-->@H_502_3@ </localpath>@H_502_3@ <rsync>@H_502_3@ <commonParams params="-artuz"/>@H_502_3@ <auth start="true" users="lyc" passwordfile="/etc/rsyncd.secret"/>@H_502_3@ <userDefinedPort start="true" port="873"/><!-- port=874 -->@H_502_3@ <timeout start="true" time="200"/><!-- timeout=100 -->@H_502_3@ <ssh start="false"/>@H_502_3@ </rsync>@H_502_3@ <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->@H_502_3@ <crontab start="false" schedule="600"><!--600mins-->@H_502_3@ <crontabfilter start="false">@H_502_3@ <exclude expression="*.PHP"></exclude>@H_502_3@ <exclude expression="info/*"></exclude>@H_502_3@ </crontabfilter>@H_502_3@ </crontab>@H_502_3@ <plugin start="false" name="command"/>@H_502_3@ </sersync>@H_502_3@
<plugin name="command">@H_502_3@ <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->@H_502_3@ <filter start="false">@H_502_3@ <include expression="(.*)\.PHP"/>@H_502_3@ <include expression="(.*)\.sh"/>@H_502_3@ </filter>@H_502_3@ </plugin>@H_502_3@ <plugin name="socket">@H_502_3@ <localpath watch="/opt/tongbu">@H_502_3@ <deshost ip="192.168.138.20" port="8009"/>@H_502_3@ </localpath>@H_502_3@ </plugin>@H_502_3@ <plugin name="refreshCDN">@H_502_3@ <localpath watch="/data0/htdocs/cms.xoyo.com/site/">@H_502_3@ <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>@H_502_3@ <sendurl base="http://pic.xoyo.com/cms"/>@H_502_3@ <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>@H_502_3@ </plugin>@H_502_3@ </head>@H_502_3@
|
[root@master1 local]#vimpic/confxml.xml
<?xml version="1.0"encoding"ISO-8859-1"?>@H_502_3@ <headversion"2.5">@H_502_3@ <hosthostip="localhost"port="8008"></host>@H_502_3@ <debugstart"false"/>@H_502_3@ <fileSystemxfs <filter"false">@H_502_3@ <excludeexpression"(.*)\.svn"></exclude>@H_502_3@ "(.*)\.gz"></exclude>@H_502_3@ "^info/*"></exclude>@H_502_3@ "^static/*"></exclude>@H_502_3@ </filter>@H_502_3@ <inotify>@H_502_3@ <delete"true"/>@H_502_3@ <createFolder <createFile <closeWrite <moveFrom <moveTo <attrib <modify </inotify>@H_502_3@
<sersync>@H_502_3@ <localpathwatch="/data/pic" <remoteip="192.168.1.52"name="pic"/>@H_502_3@ <!--<remote ip="192.168.8.39" name="tongbu"/>-->@H_502_3@ <!--<remote ip="192.168.8.40" name="tongbu"/>-->@H_502_3@ </localpath>@H_502_3@ <rsync>@H_502_3@ <commonParamsparams"-artuz"/>@H_502_3@ <auth="true"users="lyc"passwordfile="/etc/rsyncd.secret"/>@H_502_3@ <userDefinedPort"true""873"/><!-- port=874 -->@H_502_3@ <timeout"true"time"200"/><!-- timeout=100 -->@H_502_3@ <sshstart="false"/>@H_502_3@ </rsync>@H_502_3@ <failLogpath="/tmp/rsync_fail_log.sh"<!--default every 60mins execute once-->@H_502_3@ <crontabschedule"600"><!--600mins-->@H_502_3@ <crontabfilter <exclude"*.PHP"></exclude>@H_502_3@ "info/*"></exclude>@H_502_3@ </crontabfilter>@H_502_3@ </crontab>@H_502_3@ <plugin="false"="command" </sersync>@H_502_3@
>@H_502_3@ <paramprefix"/bin/sh"suffix""ignoreError/><!--prefix /opt/tongbu/mmm.sh suffix-->@H_502_3@ <filter>@H_502_3@ <includeexpression="(.*)\.PHP""(.*)\.sh" </filter>@H_502_3@ </plugin>@H_502_3@ <plugin"socket">@H_502_3@ ="/opt/tongbu"<deshost="192.168.138.20"="8009" </localpath>@H_502_3@ </plugin>@H_502_3@ ="refreshCDN"="/data0/htdocs/cms.xoyo.com/site/"<cdninfodomainname="ccms.chinacache.com"="80"username="xxxx"passwd/>@H_502_3@ <sendurlbase="http://pic.xoyo.com/cms"<regexurlregexmatch="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images" </plugin>@H_502_3@ </head>@H_502_3@ |
@H_502_3@
24行:<localpath="/data/image"> #源服务器本地同步目录@H_502_3@ 25行:<remote="image"/> #目的服务器同步目录@H_502_3@ @H_502_3@ 31行:<auth/> #指定rsync的用户和密码文件@H_502_3@ 32行:<userDefinedPort<!-- port=874 --> #指定rsync的端口@H_502_3@ 33行:<timeoutstart="true"time"200"<!-- timeout=100 --> #超时时间(秒)@H_502_3@
|
@H_502_3@
@H_502_3@
4、创建源服务器端同步目录@H_502_3@
@H_502_3@
[root@master2 ~]# mkdir -p /data/image/data/pic@H_502_3@ [root@master2 ~]# chown -R root.root/data/image/data/pic@H_502_3@
|
@H_502_3@
5、把sersync的执行脚本加入到PATH并启动sersync@H_502_3@
[root@master1 ces]#echo "export PATH=$PATH:/usr/local/sersync/bin/" >>/etc/profile@H_502_3@ [root@master1 ces]#
[root@master1 ces]#sersync2 -d -r -o /usr/local/sersync/image/confxml.xml@H_502_3@
[root@master1 ces]#sersync2 -d -r -o /usr/local/sersync/confxml.xml@H_502_3@
6、启动命令后返回结果如下为正常:@H_502_3@
set the system param@H_502_3@ execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches@H_502_3@ execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events@H_502_3@ parse the command param@H_502_3@ option: -d run as a daemon@H_502_3@ option: -r rsync all the local files to the remote servers before the sersync work@H_502_3@ option: -o config xml name: /usr/local/sersync/confxml.xml@H_502_3@ daemon thread num: 10@H_502_3@ parse xml config file@H_502_3@ host ip : localhosthost port: 8008@H_502_3@ daemon start,sersync run behind the console@H_502_3@ use rsync password-file :@H_502_3@ user islyc@H_502_3@ passwordfile is /etc/rsyncd.secret@H_502_3@ config xml parse success@H_502_3@ please set /etc/rsyncd.conf max connections=0 Manually@H_502_3@ sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)@H_502_3@ Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)@H_502_3@ please according your cpu ,use -n param to adjust the cpu rate@H_502_3@ ------------------------------------------@H_502_3@ rsync the directory recursivly to the remote servers once@H_502_3@ working please wait...@H_502_3@ execute command: cd /ces && rsync -artuz -R --delete ./ --port=873 --timeout=200 lyc@192.168.1.52::rsync --password-file=/etc/rsyncd.secret >/dev/null 2>&1@H_502_3@ run the sersync:@H_502_3@ watch path is: /data/image@H_502_3@ 7、设置开机启动sersync@H_502_3@
@H_502_3@
[root@master1]echo "sersync2 -r -d -o /usr/local/sersync/image/confxml.xml" >> /etc/rc.d/rc.local@H_502_3@ /picconfxml.xml" >> /etc/rc.d/rc.local@H_502_3@ 四、测试@H_502_3@
@H_502_3@
@H_502_3@
@H_502_3@
[root@master1]# touch lyc.txt /data/image /data/pic@H_502_3@ [root@master1 /]# cd/data/image@H_502_3@ [root@master1 image]# ls@H_502_3@ lyc.txt@H_502_3@ [root@master1 /]# cd/data/pic@H_502_3@ [root@master1 pic]# ls@H_502_3@ lyc.txt@H_502_3@
|
@H_502_3@
2、目的服务器端查看@H_502_3@
[root@master2 /]# cd /data/image@H_502_3@ [root@master2 image]# ls@H_502_3@ [root@master2 /]# cd/data/pic@H_502_3@ [root@master2 pic]# ls@H_502_3@ lyc.txt@H_502_3@
目的服务器端执行命令@H_502_3@ rsync --daemon |
源服务器端执行命令@H_502_3@
/usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/image/confxml.xml@H_502_3@ /usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/picconfxml.xml@H_502_3@ |