sersync是使用c++编写,在结合rsync同步的时候,节省了运行时耗和网络资源,因此更快。sersync配置起来很简单。另外本项目相比较其他脚本开源项目,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态,同步及时快速。

服务器:192.168.1.10(sersync2发送端)

客户端:192.168.1.20(rsync接收端)


系统环境:

[root@localhost /]# uname -a

Linux localhost.localdomain 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

首先说明的是,两台服务器都要安装rsync

[root@localhost /]#yum -y install rsync


1,客户端rsync配置

[root@localhost ~]# vi /etc/rsyncd.conf                      


uid=root

gid=root

max connections=0

use chroot=no

strict modes=yes  #是否检查口令文件权限

port=873

#address=192.168.1.20

log file=/var/log/rsyncd.log

pid file=/var/run/rsyncd.pid

lock file=/var/run/rsyncd.lock


[tongbu]

path = /rsyncdata

comment = tongbu test

ignore errors

read only = no

list=no

hosts allow = *

auth users= ruser

secrets file=/etc/rsync.pas

[root@localhost ~]#mkdir /rsyncdata

配置认证文件

# echo "ruser:123123" > /etc/rsync.pas

# chmod 600 /etc/rsync.pas  这里设置600权限,一个是为了保障安全,另一个是为了防止程序运行出错

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

重启:

# killall rsync && rsync --daemon --config=/etc/rsyncd.conf

开机启动

# echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.local


2,服务器端sersync配置


创建连接rsyncd的密码文件(如果不用密码文件验证,这一步可以不做)

# echo "123123" >/etc/rsync.pas

# chmod 600 /etc/rsync.pas

[root@localhost ~]# cd /usr/local/

[root@localhost local]#wget http://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@localhost local]# tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@localhost local]#mv GNU-Linux-x86 sersync

[root@localhost local]# cd GNU-Linux-x86/

[root@localhost sersync]# vi confxml.xml


<localpath watch="/sersyncdata">       #监控的目录

    <remote ip="192.168.1.20" name="tongbu"/>       #客户端IP,tongbu为客户端rsync模块名,如果同步到多台服务器,只需复制上一行内容修改IP即可。

</localpath>


<auth start="true" users="ruser" passwordfile="/etc/rsync.pas"/>

这一步是验证密码文件,默认为false,如果启用验证,改为true


[root@localhost ~]#mkdir /sersyncdata

[root@localhost sersync]#./sersync2 -r      #第一次完整同步

[root@localhostsersync]#./sersync2 -d      #开启守护进程

可以将以上两条命令简写:

[root@localhost sersync]#./sersync2 -r -d           #同步并开启守护

还可以设置环境变量在任何目录下都能启动:

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

[root@localhost]# source /etc/profile

启动sersync

[root@localhost]# sersync2 -r -d -o /usr/local/sersync/confxml.xml


重启

# killall sersync2 && sersync2 -r -d -o /usr/local/sersync/confxml.xml

开机启动

# echo "sersync2 -r -d -o /usr/local/sersync/confxml.xml" >> /etc/rc.local


自己测试遇到的问题:

1)测试时发现一个问题,就是要同步本机多个目录到服务器的多个目录,发现在一个confxml.xml中配置多个localpatch watch不能实现

<localpath watch="/sersyncdata"> 

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

</localpath>

<localpath watch="/data"> 

    <remote ip="192.168.1.20" name="tongbu2"/>  

</localpath>

自己的解决方法如下,如果别人有好的方法,请共享

[root@localhost sersync]cp confxml.xml confxml2.xml

[root@localhost]# sersync2 -r -d -o /usr/local/sersync/confxml.xml

[root@localhost]# sersync2 -r -d -o /usr/local/sersync/confxml2.xml

[root@localhost /]# ps aux|grep rsync

root      7842  0.0  0.1 145536   860 ?        Ssl  13:37   0:00 sersync2 -r -d -o /usr/local/sersync/confxml.xml

root      7850  0.0  0.1 135292   844 ?        Ssl  13:37   0:00 sersync2 -r -d -o /usr/local/sersync/confxml2.xml

这样启动两个配置文件,就能实现两个目录同步了。

2)开启密码验证会出错,

@ERROR: auth failed on module tongbu 

rsync error: error starting client-server protocol (code 5) at main.c

这是验证失败的错误提示,检查rsyncd.conf文件,发现

secrets file=/etc/rsync.pas写错了,写成了secrests,改过来就正常了

3)客户端自己建立的文件,不会同步删除,只有在sersync2重启服务后,才会同步删除。


3,sersync2其他命令参数

1)查看启动参数帮助

./sersync2 --help


2)指定配置文件

./sersync2 -o XXXX.xml

对于sersync使用可执行文件目录下的默认配置文件confxml.xml,如果需要使用另一个配置文件,可以使用-o参数指定其它配置文件。


3)指定默认的线程池的线程总数

./sersync2 -n num

例如 ./sersync -n 5 则指定线程总数为5,如果不指定,默认启动线程池数量是10,如果cpu使用过高,可以通过这个参数调低,如果机器配置较高,可以用-n跳高线程总数。


4)不进行同步,只运行插件

./sersync2 -m pluginName

    例如./sersync -m command,则在监控到文件事件后,不对远程目标服务器进行同步,而是直接运行command插件。


5)多个参数可以配合使用

./sersync2 -n 8 -o abc.xml -r -d

表示,设置线程池工作线程为8个,指定abc.xml作为配置文件,在实时监控前作一次整体同步,以守护进程方式在后台运行。



附:在网络上搜集到的错误提示:


问题一: 

@ERROR: chroot failed 

rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3] 


原因: 

服务器端的目录不存在或无权限,创建目录并修正权限可解决问题。 


问题二: 

@ERROR: auth failed on module tee 

rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3] 


原因: 

服务器端该模块(tee)需要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。 

提供正确的用户名密码解决此问题。 


问题三: 

@ERROR: Unknown module ‘tee_nonexists' 

rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3] 


原因: 

服务器不存在指定模块。提供正确的模块名或在服务器端修改成你要的模块以解决问题。 



问题1: 

在client上遇到问题: 

rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/ 

rsync: could not open password file "/etc/rsync.pas": No such file or directory (2) 

Password: 

@ERROR: auth failed on module backup 

rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7] 

遇到这个问题:client端没有设置/etc/rsync.pas这个文件,而在使用rsync命令的时候,加了这个参数-- 

password-file=/etc/rsync.pas 


问题2: 

rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/ 

@ERROR: auth failed on module backup 

rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7] 

遇到这个问题:client端已经设置/etc/rsync.pas这个文件,里面也设置了密码111111,和服务器一致,但是 

服务器段设置有错误,服务器端应该设置/etc/rsync.pas ,里面内容root:111111 ,这里登陆名不可缺少 


问题3: 

rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/ 

@ERROR: chdir failed 

rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7] 

遇到这个问题,是因为服务器端的/home/backup 其中backup这个目录并没有设置,所以提示:chdir failed 


问题4: 

rsync: write failed on "/home/backup2010/wensong": No space left on device (28) 

rsync error: error in file IO (code 11) at receiver.c(302) [receiver=3.0.7] 

rsync: connection unexpectedly closed (2721 bytes received so far) [generator] 

rsync error: error in rsync protocol data stream (code 12) at io.c(601) [generator=3.0.7] 

磁盘空间不够,所以无法操作。 

可以通过df /home/backup2010 来查看可用空间和已用空间 


问题5: 

1、权限问题 

类似如下的提示:rsync: opendir "/kexue" (in dtsChannel) failed: Permission denied (13)注意查看同步的目录权限是否为755 

2、time out 

rsync: failed to connect to 203.100.192.66: Connection timed out (110) 

rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5] 

检查服务器的端口netstat –tunlp,远程telnet测试。 

3、服务未启动 

rsync: failed to connect to 10.10.10.170: Connection refused (111) 

rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5] 

启动服务:rsync --daemon --config=/etc/rsyncd.conf 

4、磁盘空间满 

rsync: recv_generator: mkdir "/teacherclubBackup/rsync……" failed: No space left on device (28) 

*** Skipping any contents from this failed directory *** 

5、Ctrl+C或者大量文件 

rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [receiver=3.0.5] 

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

6、xnetid启动 

rsync: read error: Connection reset by peer (104) 

rsync error: error in rsync protocol data stream (code 12) at io.c(759) [receiver=3.0.5] 

查看rsync日志 

rsync: unable to open configuration file "/etc/rsyncd.conf": No such file or directory 

xnetid查找的配置文件位置默认是/etc下,根据具体情况创建软链接。例如: 

ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf 

或者更改指定默认的配置文件路径,在/etc/xinetd.d/rsync配置文件中。