sersync和rsync数据实时同步配置

有Linux系统sersync数据实时同步.txt是完整文件,本文档可不看
服务器
从 129
主 244
配置主服务器(源)
sersync下载地址
https://code.google.com/archive/p/sersync/downloads
http://sersync.sourceforge.net/
解压:
tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz

安装与配置sersync和rsync(从)
在Ubuntu server上,rsync和inotify默认都是安装了的。配置以xinetd方式运行。

安装
tqhy@tqhy-train1:/etc$ sudo apt-get install rsync xinetd

配置
1、修改rsync启动方式,启动rsync作为使用xinetd的守护进程
sudo vi /etc/default/rsync
修改下面这行
RSYNC_ENABLE=inetd

2、创建 /etc/xinetd.d/rsync 通过xinetd使rsync开始工作
tqhy@tqhy-train1:/etc/xinetd.d$ sudo vi rsync

service rsync
    {
        disable         = no
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
    }


3、创建/etc/rsyncd.secrets,配置用户名和密码
tqhy@tqhy-train1:~/backup$ sudo vi /etc/rsyncd.secrets
# 配置用户名和密码,密码可以任意设置
tqhy:tqhy817

4、修改rsyncd.secrets文件的权限
sudo chmod 600 /etc/rsyncd.secrets

5、创建 /etc/rsyncd.conf 配置rsync使其在.
$ sudo vi /etc/rsyncd.conf

max connections = 2
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsync.log
timeout = 300

[backup] # 模块名
comment = Public Share
# path为需要同步的文件夹路径
path = /home/tqhy/backup
read only = no
list = yes
uid = root
gid = root

# 必须和 rsyncd.secrets中的用户名对应
auth users = tqhy
secrets file = /etc/rsyncd.secrets

用户组最好设置为root,不然文件权限同步过去的时候会变化。

6、启动/重启 xinetd
tqhy@tqhy-train1:~/backup$ sudo /etc/init.d/xinetd restart
[ ok ] Restarting xinetd (via systemctl): xinetd.service.

查看rsyncserver是否启动
tqhy@tqhy-train1:~/backup$ sudo netstat -ntpl|grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 18894/xinetd

测试,在244上执行
tqhy@tqhy-test:~/backup244$ rsync tqhy@192.168.1.250::backup
Password:
drwxrwxr-x 4,096 2018/08/07 10:28:02 .


244服务器
下载sersync
wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz

tqhy@tqhy-test:~$ sudo mkdir /usr/local/sersync

tqhy@tqhy-test:~$ sudo tar -xzvf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/sersync
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml

配置sersync

tqhy@tqhy-test:/usr/local/sersync/GNU-Linux-x86$ sudo vi confxml.xml 
<filter start="false">                                                               #关闭文件过滤监控
<inotify>                                                                            #监控参数
    <modify start="true"/>                                                           #允许修改监控 
</inotify>                      
<sersync>                                                                            #监控参数
     <localpath watch="/home/tqhy/backup244/">                                                  #监控目录
           <remote ip="192.168.1.250" name="backup"/>                             #同步到远程主机的rsync 块 psn_html下
     </localpath>
    <rsync>                                                                          #rsync参数
      <commonParams params="-auvPz"/>
      <auth start="true" users="tqhy" passwordfile="/etc/rsyncd.secrets"/>                #用户名密码
      <timeout start="false" time="300"/>                                            #超时设置
    </rsync>
    <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="30"/>                      #同步失败日志脚本(30分钟后会重新同步) 
</sersync>


创建/etc/rsyncd.secrets,配置用户名和密码
tqhy@tqhy-train1:~/backup$ sudo vi /etc/rsyncd.secrets
# 配置用户名和密码,密码可以任意设置
tqhy:tqhy817

修改rsyncd.secrets文件的权限
sudo chmod 600 /etc/rsyncd.secrets

启动sersync
tqhy@tqhy-test:/usr/local/sersync/GNU-Linux-x86$ sudo /usr/local/sersync/GNU-Linux-x86/sersync2 -o ./confxml.xml -n 20 -d
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: -o config xml name: ./confxml.xml
option: -n thread num is: 20
option: -d run as a daemon
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is tqhy
passwordfile is /etc/rsyncd.secrets
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 22 = 1(primary thread) + 1(fail retry thread) + 20(daemon sub threads)
Max threads numbers is: 42 = 22(Thread pool nums) + 20(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
run the sersync:
watch path is: /home/tqhy/backup244
tqhy@tqhy-test:/usr/local/sersync/GNU-Linux-x86$

tqhy@tqhy-test:/usr/local/sersync/GNU-Linux-x86$ sudo /usr/local/sersync/GNU-Linux-x86/sersync2 -r -d -o 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: confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is tqhy
passwordfile is /etc/rsyncd.secrets
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 /home/tqhy/backup244 && rsync -auvPz -R --delete ./ tqhy@192.168.1.250::backup --password-file=/etc/rsyncd.secrets >/dev/null 2>&1
tqhy@tqhy-test:/usr/local/sersync/GNU-Linux-x86$ run the sersync:
watch path is: /home/tqhy/backup244
tqhy@tqhy-test:/usr/local/sersync/GNU-Linux-x86$

http://blog.51cto.com/joeyang/1541922

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
 <!--##设置hostip和port,这个是针对插件保留的字段,对同步功能没有任何作用,默认即可-->
    <host hostip="localhost" port="8008"></host>
<!--##debug模式 默认:关闭,开启debug模式,会在sersync正在运行的控制台,打印inotify事件与rsync同步命令-->
    <debug start="false"/>
<!--##文件系统xfs 默认:关闭-->
    <fileSystem xfs="false"/>
<!--##同步时忽略推送的文件即文件过滤功能(正则表达式),默认:关闭-->
    <filter start="false">
        <exclude expression="(.*).svn"></exclude>
        <exclude expression="(.*).gz"></exclude>
        <exclude expression="^info/*"></exclude>
        <exclude expression="^static/*"></exclude>
    </filter>
<!--##设置监控事件参数-->
<!--对于大多数应用,可以尝试把createFile(监控文件事件选项)设置为false来提高性能,减少 rsync通讯。因为拷贝文件到监控目录会产生create事件与close_write事件,所以如果关闭create事件,只监控文件拷贝结束时的事 件close_write,同样可以实现文件完整同步。
注意:必须将createFolder保持为true,如果将createFolder设为false,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控。所以除非特殊需要,请开启。默认情况下对创建文件(目录)事件与删除文件(目录)事件都进行监控,如果项目中不需要删除远程目标服务器的文件(目录),则可以将delete 参数设置为false,则不对删除事件进行监控-->
    <inotify>
        <delete start="true"/>
        <createFolder start="true"/>
        <createFile start="false"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="true"/>
        <modify start="true"/>
    </inotify>
    <sersync>
<!--##设置本地的同步路径-->
        <localpath watch="/data/res.jinhui365.com">
<!--##远程ip和模块名称-->
            <remote ip="172.16.1.12" name="res"/>
            <remote ip="172.16.1.14" name="res"/>
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
<!--##rsync同步参数-->
            <commonParams params="-auvPz"/>
<!--##rsync同步认证-->
            <auth start="true" users="res" passwordfile="/etc/password.rsync"/>
<!--##设置rsync远程服务端口,远程非默认端口需打开自定义-->
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
<!--##设置超时时间-->
            <timeout start="true" time="100"/><!-- timeout=100 -->
<!--##设置rsync+ssh加密传输模式 默认:关闭 ,开启需要设置SSH加密证书-->
            <ssh start="false"/>
        </rsync>
<!--##执行失败的语句存放在/tmp/rsync_fail_log.sh中,隔60分钟后会再次执行同步失败的语句-->
        <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">
<!--当文件同步完成后,会调用command插件,如同步文件是test.php,则test.php文件在改动之后,调用rsync同步到远程服务器后,调用command插件,执行/bin/sh test.php  suffix >/dev/null 2>&1  如果suffix设置了,则会放在inotify事件test.php之后,如果ignoreError为true,则会添加>/dev/null 2>&1当然还可以设置command的filter,当filter为ture,include可以只对正则匹配到的文件,调用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">
<!-- “refreshCDN”,就在同步过程中将文件发送到目的服务器后刷新cdn接口。如果不想使用,则将start属性设为false即可。如果需要使用其他插件,则查看其他plugin标签,将插件名称改为xml中其它插件的名称即可。该模块根据chinaCDN的协议,进行设计,当有文件产生的时候,就向cdn接口发送需要刷新的路径位置
其中 localpath watch 是需要监控的目录。
cdnifo标签制定了cdn接口的域名,端口号,以及用户名与密码。
sendurl标签是需要刷新的url的前缀。
regexurl标签中的,regex属性为true时候,使用match属性的正则语句匹配inotify返回的路径信息,并将正则匹配到的部分作为url一部分,-->
<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>


开启sersync守护进程
echo 'export PATH=$PATH:/usr/local/sersync/GNU-Linux-x86'>>/etc/profile

原文地址:https://www.cnblogs.com/lely/p/10364249.html