sersync介绍

sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。目前使用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google开源项目Openduckbill(依赖于inotify- tools),这两个都是基于脚本语言编写的。相比较上面两个项目,本项目优点是:
sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤(详细见附录,这个过滤脚本程序没有实现),所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。

相比较上面两个项目,sersync配置起来很简单,其中bin目录下已经有基本上静态编译的2进制文件,配合bin目录下的xml配置文件直接使用即可。

  • 另外本项目相比较其他脚本开源项目,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。
  • 本项目有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对同步失败的文件重新同步。
  • 本项目自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次。无需再额外配置crontab功能。
  • 本项目socket与http插件扩展,满足您二次开发的需要。

为什么要用Rsync+sersync架构

sersync是基于Inotify开发的,类似于Inotify-tools的工具

  • sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的这个文件或者这个目录

Rsync+Inotify-tools与Rsync+sersync区别

  1. Rsync+Inotify-tools

    • Inotify-tools只能记录下被监听的目录发生了变化(包括增加、删除、修改),并没有把具体是哪个文件或者哪个目录发生了变化记录下来;
    • rsync在同步的时候,并不知道具体是哪个文件或者哪个目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此,效率很低。
  2. Rsync+sersync

    • sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字;
    • rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高。

注:当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。

环境约定

服务端配置:

  • 系统版本:Centos7
  • IP地址:192.168.148.128
  • 主机名:labs-1

客户端配置:

  • 系统版本:Centos7
  • IP地址:192.168.148.129
  • 主机名:labs-2
  • 测试同步目录:/var/www/html

配置前准备

以下操作在所有服务器上面都要配置
1.关闭防火墙及SELINUX

systemctl stop firewalld
systemctl disable firewalld
setenforce 0
vim /etc/selinux/conf
SELINUX=disabled
reboot

2.安装rsync

yum -y install rsync

服务端配置

服务端仅需要配置rsync,并保证rsync正常运行即可。
1.增加开机启动

systemctl enabld rsyncd

2.修改rsync.conf配置文件

cp /etc/rsync.conf
vim /etc/rsync.conf
....
uid = root  # 进程运行属主
gid = root  # 进程运行数组
use chroot = yes # 是否锁定家目录 
max connections = 10 # 最大连接数
timeout = 600  # 超时时间
log file = /var/log/rsyncd.log  # 日志文件
ignore errors  # 忽略I/O等错误
read only = false # 是否为只读(false的话服务端文件有读写权限)
list = false     # 不显示服务端资源列表
hosts allow = 192.168.148.0/24  # 允许访问的主机/段
auth users = tongbu    # 认证用户
secrets file = /etc/rsyncd.password  # 认证用户的密码文件

[www]   # 模块名(必须全局唯一)
comment = www   # 注释
path = /var/www/html  # 路径

3.创建rsyncd.password文件

vim /etc/rsyncd.password
tongbu:123
chmod 600 /etc/rsyncd.password

4.启动rsync服务

[root@labs-1 etc]# systemctl start rsyncd
[root@labs-1 etc]# ps aux | grep rsync
root       1292  0.0  0.1 114696  1160 ?        Ss   22:09   0:00 /usr/bin/rsync --daemon --no-detach
root       1365  0.0  0.0 112660   968 pts/0    S+   22:47   0:00 grep --color=auto rsync
[root@labs-1 etc]# ss -anptu | grep 873
tcp    LISTEN     0      5         *:873                   *:*                   users:(("rsync",pid=1292,fd=3))
tcp    LISTEN     0      5        :::873                  :::*                   users:(("rsync",pid=1292,fd=5))

客户端配置

客户端上面只需要保证rsync命令可用即可,无需启动rsync服务。
1.创建rsyncd.password文件,需要说明的是客户端的rsyncd.password文件中只需要写密码即可。(此处是坑,需要注意。)

vim /etc/rsyncd.password
tongbu:123
chmod 600 /etc/rsyncd.password

2.首先来测试一下rsync是否正常使用。我们把测试同步目录(/var/www/html)下面的数据同步到服务端。

[root@labs-2 html]# rsync -azP /var/www/html/ tongbu@192.168.148.128::www 
Password: 
sending incremental file list
./
123.php
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=6/8)
abc.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=5/8)
ccc.php
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=4/8)
haha
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=3/8)
index.html
              5 100%    0.00kB/s    0:00:00 (xfr#5, to-chk=2/8)
test.php
              0 100%    0.00kB/s    0:00:00 (xfr#6, to-chk=1/8)
xixi
              0 100%    0.00kB/s    0:00:00 (xfr#7, to-chk=0/8)
[root@labs-2 html]# rsync -azP /var/www/html/ tongbu@192.168.148.128::www --password-file=/etc/rsyncd.password 
sending incremental file list
./
123.php
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=6/8)
abc.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=5/8)
ccc.php
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=4/8)
haha
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=3/8)
index.html
              5 100%    0.00kB/s    0:00:00 (xfr#5, to-chk=2/8)
test.php
              0 100%    0.00kB/s    0:00:00 (xfr#6, to-chk=1/8)
xixi
              0 100%    0.00kB/s    0:00:00 (xfr#7, to-chk=0/8)

确认上述操作成功之后,开始配置sersync。

2.配置sersync

 #下载
 wget --no-check-certificate https://dl.lianst.com/%E5%BC%80%E6%BA%90%E8%BD%AF%E4%BB%B6/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
 
 # 解压
 tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
 
 # 重命名
 mv GNU-Linux-x86/ sersync
 
 # 备份config文件
 cp confxml.xml confxml.xml.bak
 
 # 编辑配置文件

     24         <localpath watch="/var/www/html">  # 配置本地同步路径
     25             <remote ip="192.168.148.128" name="www"/> # 配置服务端IP和模块名
     26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
     27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
     28         </localpath>
     29         <rsync>
     30             <commonParams params="-artuz"/>
     31             <auth start="true" users="tongbu" passwordfile="/etc/rsyncd.password"/>  # 配置同步账号和密码文件
     32             <userDefinedPort start="false" port="874"/><!-- port=874 -->
     33             <timeout start="false" time="100"/><!-- timeout=100 -->
     34             <ssh start="false"/>
     35         </rsync>
     36         <failLog path="/var/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
     37         <crontab start="false" schedule="600"><!--600mins-->
     38             <crontabfilter start="false">
     39                 <exclude expression="*.php"></exclude>
     40                 <exclude expression="info/*"></exclude>
     41             </crontabfilter>
     42         </crontab>

# 启动sersync

 ./sersync2 -d -r -o ./confxml.xml
 [root@labs-2 sersync]# ./sersync2 -d -o -r ./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: -d     run as a daemon
    option: -o     config xml name:  -r
    daemon thread num: 10
    parse xml config file
    XML Parsing error inside file '-r'.
    Error: File not found
    At line 0, column 0.
    [root@labs-2 sersync]# ./sersync2 -d -r -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: -d     run as a daemon
    option: -r     rsync all the local files to the remote servers before the sersync work
    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    tongbu
    passwordfile is     /etc/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 ,use -n param to adjust the cpu rate
    ------------------------------------------
    rsync the directory recursivly to the remote servers once
    working please wait...
    execute command: cd /var/www/html && rsync -artuz -R --delete ./ tongbu@192.168.148.128::www --password-file=/etc/rsyncd.password >/dev/null 2>&1 
    run the sersync: 
    watch path is: /var/www/html

此时,客户端上面的/var/www/html下面的文件如果有变动的话,sersync会自动把变动的数据同步到服务端的。
附件:rsync配置笔记.txt

文章目录