Linux系统sersync数据实时同步

客户端安装
288

1、[root@Client etc]# vi /etc/rsyncd.conf (没有该文件需要创建)
uid = rsync
gid = rsync
use chroot = no
max connetctions = 200
timeout = 100
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[backup]
path = /home/backup/
ignore errors
read only = false
list = false
hosts allow = 192.168.1.299
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password

模块名称   backup
288上同步文件路径    path = /home/backup/
允许的主机   192.168.1.299
用户名称   rsync_backup
存放密码文件,没有该文件需要创建   /etc/rsync.password

--------------
2、添加用户
useradd rsync -s /sbin/nologin -M
3、改变目录权限
sudo chown -R rsync.rsync /home/backup   
---------------
4、配置密码文件
[root@Client ~]# sudo cat /etc/rsync.password                            

rsync_backup:rsync.conf

---------------
5、改变密码文件权限
chmod 600 /etc/rsync.password
6、格式化文件
dos2unix /etc/rsyncd.conf
7、开启服务后台运行
sudo rsync --daemon
显示线程
netstat -lntup|grep rsync

---------------

299

backup299
1、主服务器上配置密码文件
[root@Master ~]# cat /etc/rsync.password
rsync.conf
2、chmod 600 /etc/rsync.password
 ls -ld /etc/rsync.password

----------
3、测试手工同步
 sudo rsync -avzP /etc/hosts rsync_backup@192.168.1.288::backup --password-file=/etc/rsync.password
 
sending incremental file list
hosts
            240 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)
rsync: chgrp ".hosts.YFJxa5" (in backup) failed: Operation not permitted (1)

sent 299 bytes  received 124 bytes  56.62 bytes/sec
total size is 240  speedup is 0.65
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1183) [sender=3.1.1]

4、安装sersync服务
sersync2.5.4_64bit_binary_stable_final.tar.gz
在/usr/local/下解压缩
tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
GNU-Linux-x86/

GNU-Linux-x86/sersync2

GNU-Linux-x86/confxml.xml

GNU-Linux-x86就是sersync安装软件,为了方便将它改名

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

 /usr/local/GNU-Linux-x86  变成    /usr/local/sersync

 为了后续方便管理,创建几个目录用于存放各类文件
 [root@Master sersync]# mkdir -p conf bin logs
 [root@Master sersync]# mv confxml.xml conf
 [root@Master sersync]# ls
bin  conf  logs  sersync2
[root@Master sersync]# cd conf
[root@Master conf]# ls
confxml.xml

在配置配置文件之前备份
[root@Master conf]# cp confxml.xml confxml.xml.$(date +%F)

5、修改配置文件内容(confxml.xml)
1)、修改24-28行
<localpath watch="/home/backup299/"> 本地数据的路径
            <remote ip="192.168.1.288" name="backup"/>远端IP与模块名称
                </localpath>
2)、修改31-34行内容——认证
<commonParams params="-aruz"/>
<auth start="true" users="rsync_backup"  passwordfile="/etc/rsync.password"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
3)、修改36-37行
修改成我们刚刚创建好的logs目录
<failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecut
e="60"/><!--default every 60mins execute once-->

修改完成后测试时最终的配置文件如下
<?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="true"/>
    </inotify>

    <sersync>
    <localpath watch="/home/backup299/">
        <remote ip="192.168.1.288" name="backup"/>
        <!--<remote ip="192.168.8.39" name="tongbu"/>-->
        <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    </localpath>
    <rsync>
        <commonParams params="-aruz"/>
        <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
        <userDefinedPort start="false" port="874"/><!-- port=874 -->
        <timeout start="true" time="100"/><!-- timeout=100 -->
        <ssh start="false"/>
    </rsync>
    <failLog path="/usr/local/sersync/sersync/logs/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>

6、开启sersync守护进程
首先配置全局环境变量,使得后同可以直接调用sersync命令
 sudo vi /etc/profile
export PATH=$PATH:/usr/local/sersync/bin

tail -1 /etc/profile

source /etc/profile

7、启动sersync服务./sersync2 -r -d -o /usr/local/sersync/sersync/conf/confxml.xml.2018-08-08
-r初始化数据  

-d后台启动

-o指定路径
 sudo ./sersync2 -r -d -o /usr/local/sersync/sersync/conf/confxml.xml.2018-08-08
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/sersync/conf/confxml.xml.2018-08-08
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    rsync_backup
passwordfile is     /etc/rsync.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 ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /home/backup299 && rsync -aruz -R --delete ./  --timeout=100 rsync_backup@192.168.1.288::backup --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync:
watch path is: /home/backup299
/usr/local/sersync/sersync$

sudo chmod 777 rsync_fail_log.sh

8、测试数据同步
299
/home/backup299/
新建文件或文件夹

288
/home/backup/
同步更新相同文件或文件夹

https://blog.csdn.net/mingongge/article/details/52985259

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