#

#sersync 下载地址

#rsync CentOS系统包里面有

#rsync 详解

#sersync 详解


#逻辑图

wKiom1j7ZfbxjhBgAAAR4BwWo3k935.png


主机名IP地址
Storage_1             目标服务器192.168.174.137
Storage_2               源服务器192.168.174.138

#均关闭防火墙和selinux


#目标服务器rsync 作为守护进程  , 源服务器rsync 作为客户端


#rsync支持推送和拉取

#sersync 实时监控目录


#目标服务器,源服务器 安装rsync

[root@Storage_1 ~]# yum install -y rsync
[root@Storage_2 ~]# yum install -y rsync



#目标服务器上操作

#修改目标服务器rsync的配置文件

[root@Storage_1 ~]# vim /etc/rsyncd.conf
[root@Storage_1 ~]# cat /etc/rsyncd.conf | grep -v "#" | grep -v "^$"
uid = root
gid = root
use chroot = yes
max connections = 200
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
secrets file = /etc/rsyncd.passwd
timeout = 900
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[ftp]
       path = /opt
       comment = ftp export area
auth users = rsync
ignore errors
list no
hosts allow = 192.168.174.0/24
read only = no


#rsync 也支持匿名用户   只需注释 secrets file = /etc/rsyncd.passwd   auth users = rsync


#创建认证文件  rsync为配置文件中的 auth users的值

[root@Storage_1 ~]# echo "rsync:123456" /etc/rsyncd.passwd


#修改配置文件的权限

[root@Storage_1 ~]# chmod 600 /etc/rsyncd.passwd


#开启服务

[root@Storage_1 ~]# rsync --daemon --config=/etc/rsyncd.conf




#源服务器上操作


#创建认证文件

[root@Storage_2 ~]# echo "123465" > /etc/rsyncd.passwd




#修改权限

[root@Storage_2 ~]# chmod 600 /etc/rsyncd.passwd


#同步测试

[root@Storage_2 ~]# echo "hello" >> /opt/test.txt
[root@Storage_2 ~]# cd /opt/
[root@Storage_2 ~]# rsync -artuz -R --delete ./ rsync@192.168.174.137::ftp --password-file=/etc/rsyncd.passwd

#查看结果

[root@Storage_1 ~]# cd /opt/
[root@Storage_1 opt]# ls
test.txt
[root@Storage_1 opt]# cat test.txt 
hello

#同步成功!





#安装sersync

[root@Storage_2 ~]# wget https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz 
[root@Storage_2 ~]# tar -xf sersync2.5.4_64bit_binary_stable_final.tar.gz 
[root@Storage_2 ~]# mkdir -p  /usr/local/sersync/{bin,conf}
[root@Storage_2 ~]# cd GNU-Linux-x86/
[root@Storage_2 GNU-Linux-x86]# ls
confxml.xml  sersync2
[root@Storage_2 GNU-Linux-x86]# cp confxml.xml   /usr/local/sersync/conf/
[root@Storage_2 GNU-Linux-x86]# cp sersync2   /usr/local/sersync/conf/
[root@Storage_2 GNU-Linux-x86]# cd /usr/local/sersync/

#修改sersync配置文件

[root@Storage_2 sersync]# vim conf/confxml.xml 
[root@Storage_2 sersync]# cat conf/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/">
    <remote ip="192.168.174.137" name="ftp"/>
    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
    <commonParams params="-artuz"/>
    <auth start="true" users="rsync" passwordfile="/etc/rsyncd.passwd"/>
    <userDefinedPort start="false" port="874"/><!-- port=874 -->
    <timeout start="false" time="100"/><!-- timeout=100 -->
    <ssh start="false"/>
</rsync>
<failLog path="/var/run/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/p_w_picpaths"/>
</localpath>
    </plugin>
</head>



#具体含义请参考 http://www.linuxidc.com/Linux/2012-02/53572.htm


#设置环境变量

[root@Storage_2 sersync]# echo "export PATH=$PATH:/usr/local/sersync/bin/" >> /etc/profile 
[root@Storage_2 sersync]# source /etc/profile





#开启服务

[root@Storage_2 sersync]# sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml 
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -r rsync all the local files to the remote servers before the sersync work
option: -d run as a daemon
option: -o config xml name:  /usr/local/sersync/conf/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhosthost port: 8008
daemon start,sersync run behind the console 
use rsync password-file :
user isrsync
passwordfile is /etc/rsyncd.passwd
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) 
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /opt && rsync -artuz -R --delete ./ rsync@192.168.174.137::ftp --password-file=/etc/rsyncd.passwd >/dev/null 2>&1 
run the sersync: 
watch path is: /opt


#从上面的信息可以看出 sersync 是要调用rsync 的




#测试

[root@Storage_2 ~]# echo "123456" > /opt/test2.txt


#观察

[root@Storage_1 opt]# ls
test2.txt  test.txt
[root@Storage_1 opt]# cat test2.txt 
123456



#排错

错误一:

@ERROR: auth failed on module xxxxx

rsync: connection unexpectedly closed(90 bytes read so far)

rsync error: error in rsync protocoldata stream (code 12) at io.c(150)

说明:这是因为密码设置错了,无法登入成功,检查一下rsyncd.passwd,看客服是否匹配。还有服务器端没启动rsync 服务也会出现这种情况。

 

错误二:

password file must not beother-accessible

continuing without password file

Password:

说明:这是因为rsyncd.passwd的权限不对,应该设置为600。如:chmod600 rsyncd.passwd

 

错误三:

@ERROR: chroot failed

rsync: connection unexpectedly closed(75 bytes read so far)

rsync error: error in rsync protocoldata stream (code 12) at io.c(150)

说明:这是因为你在 rsyncd.conf中设置的 path 路径不存在,要新建目录才能开启同步

 

错误四:

rsync: failed to connect to218.107.243.2: No route to host (113)

rsync error: error in socket IO (code10) at clientserver.c(104) [receiver=2.6.9]

说明:防火墙问题导致,这个最好先彻底关闭防火墙,排错的基本法就是这样,无论是S还是C,还有ignore errors选项问题也会导致

 

错误五:

@ERROR: access denied to www fromunknown (192.168.1.123)

rsync: connection unexpectedly closed(0 bytes received so far) [receiver]

rsync error: error in rsync protocoldata stream (code 12) at io.c(359)

说明:此问题很明显,是配置选项host allow的问题,初学者喜欢一个允许段做成一个配置,然后模块又是同一个,致使导致

 

错误六:

rsync error: received SIGINT,SIGTERM, or SIGHUP (code 20) at rsync.c(244) [generator=2.6.9]

rsync error: received SIGUSR1 (code19) at main.c(1182) [receiver=2.6.9]

说明:导致此问题多半是服务端服务没有被正常启动,到服务器上去查查服务是否有启动,然后查看下 /var/run/rsyncd.pid 文件是否存在,最干脆的方法是杀死已经启动了服务,然后再次启动服务或者让脚本加入系统启动服务级别然后shutdown -r now服务器

 

错误七:

rsync: read error: Connection resetby peer (104)

rsync error: error in rsync protocoldata stream (code 12) at io.c(604) [sender=2.6.9]

说明:原数据目录里没有数据存在



#rsync 命令参数

rsync的命令格式:

rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST

 

rsync [OPTION]... [USER@]HOST:SRC DEST 

 

rsync [OPTION]... SRC [SRC]... DEST

 

rsync [OPTION]... [USER@]HOST::SRC [DEST]

 

rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST

 

rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DES

 

常用选项:

-v, --verbose 详细模式输出

-q, --quiet 精简输出模式

-z,在传输时对备份文件进行压缩处理

-c, --checksum 打开校验开关,强制对文件传输进行校验

-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD

-r, --recursive 对子目录以递归模式处理

-R, --relative 使用相对路径信息

-l, --links 保留软链结

-H, --hard-links 保留硬链结

-p, --perms 保持文件权限

-o, --owner 保持文件属主信息

-g, --group 保持文件属组信息

-D, --devices 保持设备文件信息

-t, --times 保持文件时间信息


#参考:http://www.linuxidc.com/Linux/2012-07/64738.htm

#参考:http://daguanren.blog.51cto.com/9097475/1856461