b.gif
b.gif
一:基本服务:
rsync服务搭建其实很简单,主要是服务器端要设置好,一切的东西是以服务器端为主的。
首先是要安装rsync服务,这个服务可以用yum进行安装,也可以编译安装,但是主要的配置文件需要我们自己进行配置。
主要的配置文件主要有3个。
第一个是主配置文件:rsyncd.conf:这个配置文件主要配置的是他的主要内容,比如pid路径,属主和属组,访问权限,最大连接数,连接显示的信息,最主要的是要同步的目录的权限。
其次是:rsyncd.password,这个文件主要设置的是在客户端要用这个里面配置的用户名和密码进行同步服务
最后一个文件:rsyncd.motd 这个文件就是为了同步登陆的时候显示的一个说明一样,这个和系统中/etc/motd的作用是相同的。这个文件只要在rsyncd.conf指定了正确的位置,那么不管客户端是否密码输入正确,模块名称是否写对,都会在客户端显示出来。
例如:你在服务器上有个用户“zz“这个用户自己的密码是111111,而我们在rsyncd.conf这个文件中设置的
授权访问者就是zz这个用户(auth users = zz),但是我们在同步服务中不建议密码也设置成111111,这样会出现安全的问题,这个在samba中也很常见,但是在samba中我们还是需要用smbpasswd这个命令设置密码的,而rsync中是不需要的,只要在rsyncd.passwd这个文件中加上如下信息:
zz:123456 
 之后再客户端也建立这样的一个文件但是里面的内容是
123456
(直接是密码即可)
这个时候同步的设置就完成了。之后是启动服务:
因为主配置文件是我们自己建立的因此启动需要我们加上配置文件的路径:
rsync --daemon --config=/usr/local/rsync/rsyncd.conf &
这样做的目的就是让服务器端的rsync服务当做守护进程来运行。
这个时候再客户端可以使用
rsync --list-only zz@192.168.233.132::cui 来检验cui这个模块是否能够检测到,可能会要求输入密码进行解决。
如果这样就不用输入密码了:rsync --password-file=/usr/local/rsync/rsyncd.password --list-only zz@192.168.233.132::cui
这样就能显示服务器端详细的可以同步的目录的信息和motd的信息了。
如下为服务器和客户端的信息:
服务器:
 
图片
客户端:

  图片
 
以上的工作顺利完成了,我们剩下的工作就好弄了,因为服务已经建立,并且通信正常。

二:实战:
上面的工作是手动来同步数据的,而生产环境中这么做没有任何提高效率的意义,如果要实现服务器和客户端的文件自动同步就可以使用一下两种方法:
1)需要安装inotify这个程序了。编译安装过程很简单。
 
图片
 在上图中我们要把同步主服务器指定目录的下的东西实时的同步到其他3个目标服务器上,这个时候要在那3个服务器上启动rsync这个守护进程,:rsync --daemon --config=/usr/local/rsync/rsync.conf & 查看启用了873端口,那么守护进程就可以用了。
之后我们需要同步主服务器端,设置上面提到的一些设置如rsync.passwd和一些权限的设置,之后建立脚本:

/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' \
-e modify,delete,create,attrib \
/data/www/wwwroot/ \
| while read file
    do
         /usr/bin/rsync -avz --delete --progress --password-file=/usr/local/rsync/rsyncd.password /data/www/wwwroot/ c
ui
@192.168.233.131:: cui

    done

如果有多台目标服务器需要同步数据,那么就将彩色的条目复制下,更改除了红色字体外其他颜色的响应的内容就可以了。
注意下就是ubuntu系统,她默认的rsync是已经安装好了的,但是没有配置文件,我们需要将/usr/share/doc/rsync/examples/rsyncd.conf
复制过来,修改响应的条目就可以用了。
之后运行这个脚本,之后再同步主服务器上这个目录修改了任何的东西,在目标服务器的这个目录中就会修改了。相当方便。

2)另一种方法,在同步主服务器上安装sersync(http://sersync.googlecode.com/files/sersync2.5_32bit_binary_stable_final.tar.gz)这个程序解压缩后,里面有2个文件:confxml.xml  sersync2
我们需要编辑confxml.xml这个文件,其实这个文件就是将同步的命令写入脚本中,直接运行就不用写那么长的命令了
rsync -artuz -R --delete ./ cui@192.168.233.131::cui --password-file=/usr/local/rsync/rsyncd.password >/dev/null 2>&1
如果直接运行命令那么就需要写上面那么长的命令,而编辑完成后就不需要了,而且还加入了很多其他的功能。

完整的文件如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip=" 192.168.233.132" port="8008"></host> #本地IP地址
    <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=" /data/www/wwwroot">  #本地需要同步的目录
            <remote ip=" 192.168.233.131" name=" cui"/> #远程的目标服务器的IP地址和模块名称
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params=" -artuz"/>  #rsync的参数
            <auth start=" true" users=" cui" passwordfile=" /usr/local/rsync/rsyncd.password"/> #授权文件的参数
            <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-->   #为true 即多长时间监控一次 ,默认为600分钟,我们设置1分钟,方便实验
            <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>

编辑完成后,在
[root@server GNU-Linux-x86]# ls
confxml.xml  sersync2
直接运行下面的命令
[root@server GNU-Linux-x86]# sersync2 -r -d  #首先完整的同步一次,之后将允许在守护进行在后台允许。
set the system param
execute锛.cho 50000000 > /proc/sys/fs/inotify/max_user_watches
execute锛.cho 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d     run as a daemon
option: -r     rsync all the local files to the remote servers before the sersync work
daemon thread num: 10
parse xml config file
host ip : 192.168.233.132    host port: 8008
daemon start锛.ersync run behind the console
use rsync password-file :
user is    cui
passwordfile is     /usr/local/rsync/rsyncd.password
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 锛.se -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data/www/wwwroot && rsync -artuz -R --delete ./ cui@192.168.233.131::cui --password-file=/usr/local/rsync/rsyncd.password >/dev/null 2>&1
run the sersync:
watch path is: /data/www/wwwroot

./sersync2.1 -h 查看帮助文件
./sersync2.1 -r 在同步程序开启前对整个路径与远程服务器整体同步一遍
./sersync2.1 -d 开启守护进程模式,在后台运行
./sersync2.1 -o 制定配置文件名,如果配置文件名称不是confxml.xml请使用'-o xxxxx.xml'
./sersync2.1 -n 制定同步守护线程数量,默认为10个,适用于现在的4核服务器。如果需 增加或减少使用 '-n 数量'.
通常使用的方法是
./sersync2.1 -d -r 

就同步成功了。而且实现了实时更新的功能,很方便,相对于之前的操作,不用写脚本了,如果能够有个前端的程序,那么图形界面就可以操作,那样就更好了。
FAQ:

如果出现如下问题:
一:

 图片
问题主要从以下方面查抄问题:
1:服务器端:rsyncd.password的权限是否正确(应该是0600)
2:服务器端:保证rsyncd.password这个文件的内容填写正确,尤其是用户名,密码是可以自己随意写的,但是客户端的密码也必须和这个随意的密码保持一致。
3:客户端:当要使用--password-file这个命令的时候要保证这个文件的密码正确,以及他的权限为0600,如果不是会提示
password file must not be other-accessible

以上为情况。请根据自己的需要斟酌使用。

以下为同步文件的时候正确显示如下:

 
图片

二:
ERROR: module is read only
rsync error: syntax or usage error (code 1) at main.c(747) [receiver=2.6.8]
rsync: connection unexpectedly closed (4 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6]

解决:提示打开了read only,将配置文件 read only = no

三:
rsync: failed to set times on “directory” Operation not permitted (1)
这个问题就是,因为 /etc/rsync.conf 文件内,你指定的uid 、 gid  的问题, 这样,你同步文件要写的目录 ,他的属主和属组 都要是/etc/rsync.conf 文件内指定的uid 、 gid  ,
这样就不会 rsync: failed to set times on  之类的错误了 !
备注:  参考文献:http://blog.johntechinfo.com/sersyncguild  
            本文章,参考了网上的很多文章,在此不能一一列举,表示感谢。
           初版完成时间:2011年11月21日