0. 前提:

192.168.1.9:安装rsync server服务。
192.168.1.10:安装rsync,sersync服务。

监控192.168.1.10(/data/{www,bbs}) --> 192.168.1.9(/data/{www,bbs})
【原理】:

  • 监控同步服务器(192.168.1.10)上开启sersync服务,监控路径下文件系统发生的变化;
  • 调用rsync命令将变化的文件同步到目标服务器(192.168.1.9);

1. 部署rsync server服务

1.1 配置rsync配置文件

# vim /etc/rsyncd.conf
uid = ftp
gid = ftp
use chroot = no
max connections = 5000
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
motd file = /etc/rsyncd.motd
secrets file =/etc/rsyncd.password
auth users = rsync_backup
port = 5678
#########################################
[www]
path = /data/www
comment = www
ignore errors = yes
read only = no
hosts allow = 192.168.1.10
hosts deny = *
########################################
[bbs]
path = /data/bbs
comment = bbs
ignore errors = yes
read only = no
hosts allow = 192.168.1.10
hosts deny = *

1.2 rsync配置文件说明:

# 同步后文件uid/gid
uid = ftp
gid = ftp
# 默认为true,修改为no,增加对目录文件软连接的备份。涉及安全选项,建议关闭。
use chroot = no
# 最大连接数
max connections = 36000
#  超时时间
timeout = 600
# pid文件文件
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
# 每次连接都展示提示消息的文件位置
motd file = /etc/rsyncd.motd
# 用户名密码文件位置
secrets file =/etc/rsyncd.password
# 用户
auth users = rsync_backup
# 端口号
port = 5678
#########################################
# 认证模块名称
[www]
# 同步存储位置
path = /data/www
# 备注说明
comment = www
# 忽略无关错误
ignore errors = yes
# 设置为读写权限
read only = no
# 运行的IP
hosts allow = 192.168.1.10
# 禁止的IP
hosts deny = *
########################################
[bbs]
path = /data/bbs
comment = bbs
ignore errors = yes
read only = no
hosts allow = 192.168.1.10
hosts deny = *

1.3 创建密码文件修改权限600

# vim /etc/rsyncd.password
rsync_backup:hello
# chmod 600 /etc/rsyncd.password 

1.4 创建提示文件(可有可无)

# cat /etc/rsyncd.motd
############################
####### START RSYNC ########
############################

1.5 创建相关待同步的目录

# mkdir /data/{www,bbs}  -pv
# chown ftp:ftp /data/{www,bbs}

注意:同步目录的所属组和所属用户要和rsync配置文件上的uid和gid一致。

1.6 启动守护进程,并写入开机自启动

# rsync --daemon
#指定配置文件启动
# rsync --daemon --config=/etc/rsyncd.conf
# echo "/usr/bin/rsync --daemon --config=/etc/rsyncd.conf"  >>/etc/rc.local

1.7 测试(192.168.1.10)

在192.168.1.10上创建密码文件,只需要写入密码即可,然后赋予600权限。通过rsync进行同步测试文件是否可以同步成功。在192.168.1.9服务上/data/{www,bbs}上查看是否有同步的文件。必须保证这一步成功,否则后续的操作无法成功。

# cat /etc/rsync.passwd
hello
# chmod 600 /etc/rsync.passwd
# rsync -artuz --port=5678 anaconda-ks.cfg rsync_backup@192.168.1.9::bbs --password-file=/etc/rsync.passwd
############################
####### START RSYNC ########
############################
# rsync -artuz --port=5678 anaconda-ks.cfg rsync_backup@192.168.1.9::www --password-file=/etc/rsync.passwd
############################
####### START RSYNC ########
############################

1.8 rsync参数说明:

# 本地复制:
rsync [选项] SRC... [DEST]
# 通过远程shell复制
下载数据:rsync [选项] [USER@]HOST:SRC...[DEST]
上传数据:rsync [选项] SRC...[USER@]HOST:DEST
# 通过rsync进程复制
下载数据:
rsync [选项] [USER@]HOST::SRC...[DEST]
rsync [选项] rsync:://[USER@]HOST[:PORT]/SRC...[DEST]
上传数据:
rsync [选项] SRC...[USER@]HOST::DEST
rsync [选项] SRC...rsync://[USER@]HOST[:PORT]/DEST
选项:
-v:显示详细信息
-q:静默模式,无错误信息
-a:归档模式,保留文件属性
-r:递归模式
-b:如果目标路径已经纯质同名文件,将旧的文件重命名为~filename,可以使用--suffix指定不同的备份前缀。
--back-dir:将备份文件保存到指定目录
-t:保留修改的时间属性
-l:保留符合链接
-u:如果目标地址中的文件比将要下载的文件新,则不执行同步。也就是说,不会用就旧文件覆盖新的文件。
-p:保留文件权限属性
-H:保留硬链接
-A:保留ACL权限
-X:保留文件附加属性
-o:保留文件所有者属性
-g:保留文件所属组属性
--devices:保留设备文件
--specials:保留特殊文件
-W:不做增量检测,直接复制全部文件
--delete:删除那些仅在目录路径中存在的文件。
-z:传输过程中对数据进行压缩
--include=PATTERN 匹配不排除的文件
--exclude=PATTERN 匹配需要排除的文件
--progress:显示数据传输的进度信息
--partial:保留因故障未传输完成的文件
--password-file=FILE:指定密码文件
--list-only:仅列出服务器模块列表,需要rsync服务器设置list=true

2. 部署sersync服务

2.1 安装sersync服务

# tar -xvf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
# mv GNU-Linux-x86 sersync
# tree sersync
sersync
├── confxml.xml #配置文件
└── sersync2 #二进制启动文件
# cp confxml.xml{,.bak}

2.2 配置sersync

修改配置文件:

     <sersync>
        <localpath watch="/opt/tongbu">   # 定义本地要同步的目录
            <remote ip="127.0.0.1" name="tongbu1"/> # 要同步到的机器,rsync server下的tongbu1模块
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/> #同步的参数
            <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> # 用户名和密码文件
            <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-->
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <plugin start="false" name="command"/>
    </sersync>

修改后:

    <sersync>
        <localpath watch="/data/www">
            <remote ip="192.168.1.9" name="www"/>
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.passwd"/> #开启认证
            <userDefinedPort start="true" port="5678"/><!-- port=874 --> #开启端口
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/usr/local/sersync/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>

2.3 多实例配置

# cp confxml.xml bbs_confxml.xml
# cp confxml.xml www_confxml.xml
#  bbs_confxml.xml修改的配置文件
    <sersync>
        <localpath watch="/data/bbs">
            <remote ip="192.168.1.9" name="bbs"/>
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.passwd"/>
            <userDefinedPort start="true" port="5678"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/usr/local/sersync/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>
---------------------------------------------------------------------------------------------------
# www_confxml.xml修改的配置文件:
    <sersync>
        <localpath watch="/data/www">
            <remote ip="192.168.1.9" name="www"/>
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.passwd"/>
            <userDefinedPort start="true" port="5678"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/usr/local/sersync/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>
 

2.4 启动sersync守护进程:

# usr/local/sersync/sersync2 -dro /usr/local/sersync/www_confxml.xml
# /usr/local/sersync/sersync2 -dro /usr/local/sersync/bbs_confxml.xml
通过在/data/{bbs,www}中创建文件,来测试是否可以进行同步。

参数说明:
-d:启用守护进程模式
-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
-n: 指定开启守护线程的数量,默认为10个
-o:指定配置文件,默认使用confxml.xml文件
-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
-m:单独启用其他模块,使用 -m socket 开启socket模块
-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序

2.5 配置文件参数说明

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
# hostip与port是针对插件的保留字段,对于同步功能没有任何作用,保留默认即可;
    <host hostip="localhost" port="8008"></host>
# 设置为true,表示开启debug模式,会在sersync运行的控制台打印inotify时间与rsync同步命令;对于排查错误可以打开进行排查
    <debug start="false"/>
# 对于xfs文件系统的用户,需要将这个选项开启,才能使用sersync正常工作;
    <fileSystem xfs="false"/>
# 开启表示对匹配的文件进行过滤。
    <filter start="false">
 <exclude expression="(.*)\.svn"></exclude>
 <exclude expression="(.*)\.gz"></exclude>
 <exclude expression="^info/*"></exclude>
 <exclude expression="^static/*"></exclude>
    </filter>
    <inotify>
# 设置为true后,如果删除本地文件也会同步删除远程的文件,建议设置为false。
 <delete start="true"/>
# 如果为false,将不会对产生的目录进行监控,该目录下的子文件也不会被监控。
 <createFolder start="true"/>
# createFile(监控文件事件选项)设置为false来提高性能,减少rsync通讯;因为拷贝文件到监控目录会产生create事件与close_write事件,所以如果关闭create事件,只监控文件拷贝结束时的时间close_write,同样可以实现文件完整同步;
 <createFile start="false"/>
 <closeWrite start="true"/>
 <moveFrom start="true"/>
 <moveTo start="true"/>
 <attrib start="false"/>
 <modify start="false"/>
    </inotify>

    <sersync>
 <localpath watch="/opt/tongbu">   # 本地同步的文件夹路径
     <remote ip="127.0.0.1" name="tongbu1"/> #远程同步到的IP地址和模块名
     <!--<remote ip="192.168.8.39" name="tongbu"/>-->
     <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 </localpath>
 <rsync>
     <commonParams params="-artuz"/>
     <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>  # 认证用户名和认证密码文件
     <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-->
     <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/images"/>
 </localpath>
    </plugin>
</head>
02-24 13:18