rsync+sersync实现文件双向同步

rsync+sersync实现文件双向同步

资源信息

服务器信息
系统:centos7.3
内核:3.10.0-514.el7.x86_64
selinux:关闭
iptables:开启

服务器 IP
rsync-1 192.168.157.3
rsync-2 192.168.157.4

软件包信息

软件包 版本
rsync rsync-3.1.2-10.el7.x86_64
sersync sersync2.5.4_64bit_binary_stable_final.tar.gz

简要说明

rsync
官网地址:https://rsync.samba.org/
官网最新版本:3.1.3
rsync是一个开源软件,提供了在类unix系统上的快速增量文件传输。
在文件传输过程中,rsync得益于自己的rsync算法,该算法通过扫描并计算出本地文件与远程文件之间的差异部分,仅将差异部分进行同步,这样尽可能的减少了同步时间。

rsync的功能点:

  • 可以更新整个文件目录结构和文件系统
  • 可以选择性的保留文件的软链接、硬链接、文件所有权、权限、设备、时间
  • 安装时不需要任何权限
  • 对于传输多个文件时,内部流水线减少了传输延迟
  • 可以使用rsh、ssh或sockets来进行传输
  • 支持匿名传输,这是镜像的常用选择

sersync
sersync2.5.4_64bit下载地址
sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。目前常用的同步解决方案为rsync+inotify-tools和google的开源项目Openduckbill(依赖于inotify-tools),这两个都是基于脚本语言编写的。
sersync优点包括以下:

  • sersync使用c++语言开发,而且对linux系统 文件系统产生的临时文件和重复的文件操作进行过滤,所以在结合rsync同步的时候,节省了运行时耗和网络资源,速度更快。
  • sersync配置简单,其bin目录下已经有编译好的二进制文件sersync2,配合bin目录下的confxml.xml配置文件,可以直接使用。
  • sersync相对于inotify使用了多线程进程同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态
  • 出错处理机制,通过失败队列对出错的文件重新同步
  • 自带crontab功能,在confxml.xml文件中配置开启即可按照时间段进行同步,而无需使用crontab
  • socket与http插件扩展,便于二次开发

sersync基于inotify开发,本质都是对目录,文件的监听,依赖于rsync进行文件同步。

sersync和inotify的区别与选择
sersync和inotify的区别详见该博主的博文https://www.xubaojin.com/post/35.html,简单明了。

安装配置

rsync

rsync的安装
在此使用centos自带的yum源进行安装

[root@rsync-1 ~]# rpm -qa | grep rsync
[root@rsync-1 ~]# yum install rsync -y
[root@rsync-1 ~]# rpm -qa | grep rsync
rsync-3.1.2-10.el7.x86_64

注:rsync-2同步安装

rsync的配置
因为是需要配合sersync进行使用的,需要将rsync配置为daemon方式运行。
一、修改rsync-1的配置文件/etc/rsyncd.conf

[root@rsync-1 ~]# cat /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = root      # 指定运行rsync daemon的用户
gid = root      # 指定运行rsync daemon的组
use chroot = no
address = 192.168.157.3 # 指定监听地址
port = 873      # 默认监听端口
max connections = 0     #最大连接数,0为无限制
pid file = /var/run/rsyncd.pid  # 指定pid文件
log file = /var/log/rsyncd.log # 指定日志文件
exclude = lost+found/   # 指定不同步的目录
ignore errors
#transfer logging = yes
#timeout = 900
#ignore nonreadable = yes
#dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
[update]        # update模块
path = /root/updatedir  # update模块需要同步目录
comment = test rsync + sersync  # update模块的简要说明
read only = no  # 是否只读
list = no       # 当用户查询该服务器上的可用模块时,是否列出该模块
auth users = rsync_daemon        # 同步文件使用到的虚拟用户
secrets file = /etc/rsync_update.passwd # 指定该虚拟用户对应的密码文件,该文件权限为(400)
hosts allow = 192.168.157.4     # 指定可以连接该模块的主机(x.x.x.x x.x.x.x/x)

二、修改rsync-2的配置文件/etc/rsyncd.conf

[root@rsync-2 ~]# cat /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = root      # 指定运行rsync daemon的用户
gid = root      # 指定运行rsync daemon的组
use chroot = no
address = 192.168.157.4 # 指定监听地址
port = 873      # 默认监听端口
max connections = 0     #最大连接数,0为无限制
pid file = /var/run/rsyncd.pid  # 指定pid文件
log file = /var/log/rsyncd.log # 指定日志文件
exclude = lost+found/   # 指定不同步的目录
ignore errors
#transfer logging = yes
#timeout = 900
#ignore nonreadable = yes
#dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
[update]        # update模块
path = /root/updatedir  # update模块需要同步目录
comment = test rsync + sersync  # update模块的简要说明
read only = no  # 是否只读
list = no       # 当用户查询该服务器上的可用模块时,是否列出该模块
auth users = rsync_daemon        # 同步文件使用到的虚拟用户
secrets file = /etc/rsync_update.passwd # 指定该虚拟用户对应的密码文件,该文件权限为(400)
hosts allow = 192.168.157.3     # 指定可以连接该模块的主机(x.x.x.x x.x.x.x/x)

注:在修改rsyncd.conf配置文件时,要去掉后面的 #开头注释语,或者另起一行,否则起不到注释作用

三、根据配置文件创建相应的目录、文件、防火墙规则
创建需要同步的目录

[root@rsync-1 ~]# mkdir /root/updatedir
[root@rsync-2 ~]# mkdir /root/updatedir

创建虚拟用户rsync_daemon使用的密码文件/etc/rsync_update.passwd
该文件的权限必须为400

[root@rsync-1 ~]# echo "rsync_daemon:123456789" > /etc/rsync_update.passwd
[root@rsync-1 updatedir]# chmod 400 /etc/rsync_update.passwd
[root@rsync-2 ~]# echo "rsync_daemon:123456789" > /etc/rsync_update.passwd
[root@rsync-2 updatedir]# chmod 400 /etc/rsync_update.passwd

开通防火墙规则

[root@rsync-1 ~]# iptables -I INPUT -p tcp -m state --state NEW -m tcp -s 192.168.157.4 --dport 873 -j ACCEPT
[root@rsync-1 ~]# service iptables save
[root@rsync-2 ~]# iptables -I INPUT -p tcp -m state --state NEW -m tcp -s 192.168.157.3 --dport 873 -j ACCEPT
[root@rsync-2 ~]# service iptables save

启动rsyncd服务

[root@rsync-1 ~]# systemctl start rsyncd
[root@rsync-2 ~]# systemctl start rsyncd

查看端口

[root@rsync-1 ~]# ss -antuple | grep 873
tcp    LISTEN     0      5      192.168.157.3:873                   *:*                   users:(("rsync",pid=2320,fd=4)) ino:21550 sk:ffff880038b79f00 <->

简单测试
一、rsync-1rsync-2同步
rsync-1服务器操作:

[root@rsync-1 updatedir]# ls
[root@rsync-1 updatedir]# touch file{1..5}
[root@rsync-1 updatedir]# rsync -av /root/updatedir/ [email protected]::update
Password:
sending incremental file list
./
file1
file2
file3
file4
file5

sent 314 bytes  received 114 bytes  77.82 bytes/sec
total size is 0  speedup is 0.00

rsync-2服务器查看:

[root@rsync-2 updatedir]# ls
file1  file2  file3  file4  file5

二、rsync-2rsync-1同步
rsync-2服务器操作:

[root@rsync-2 updatedir]# touch file{6..10}
[root@rsync-2 updatedir]# rsync -av /root/updatedir/ [email protected]::update
Password:
sending incremental file list
./
file10
file6
file7
file8
file9

sent 388 bytes  received 114 bytes  111.56 bytes/sec
total size is 0  speedup is 0.00

rsync-1服务器查看:

[root@rsync-1 updatedir]# ls
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

至此,我们的rsync已经配置成功了。

sersync

安装完成了rsync后,我们需要配置sersync来对文件的变更进行实时监听,并触发rsync对变更的文件进行实时同步。
sersync安装
sersync下载
需要可以科学上网哦,或者在网上找别人下载好的资源。

获取到软件包后,上传到服务器解压。

[root@rsync-1 ~]# ls
sersync2.5.4_64bit_binary_stable_final.tar.gz  updatedir
[root@rsync-1 ~]# tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@rsync-1 ~]# ls GNU-Linux-x86/
confxml.xml  sersync2

可以看到配置极其简单,一个二进制文件,一个配置文件。
sersync配置
一、rsync-1服务器配置:

[root@rsync-1 GNU-Linux-x86]# diff confxml.xml confxml.xml.origin
24,25c24,25
<       <localpath watch="/root/updatedir">
<           <remote ip="192.168.157.4" name="update"/>
---
>       <localpath watch="/opt/tongbu">
>           <remote ip="127.0.0.1" name="tongbu1"/>
31c31
<           <auth start="true" users="rsync_daemon" passwordfile="/etc/rsync.pas"/>
---
>           <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>

  • watch 指定了监控的文件目录
  • remote ip 指定远程rsync服务监听的ip,
  • name 指定了远程rsync服务开启的模块
  • start=“true” 说明远程rsync服务开启了用户密码验证
  • users 指定了远程rsync服务用户密码验证指定的虚拟用户
  • passwordfile 指定了连接远程rsync服务虚拟用户的密码文件,权限需为400,用户自己手动创建

密码文件配置

[root@rsync-1 GNU-Linux-x86]# echo 123456789 > /etc/rsync.pas
[root@rsync-1 GNU-Linux-x86]# chmod 400 /etc/rsync.pas

二、rsync-2服务器配置

[root@rsync-2 GNU-Linux-x86]# diff confxml.xml confxml.xml.origin
24,25c24,25
<       <localpath watch="/root/updatedir">
<           <remote ip="192.168.157.3" name="update"/>
---
>       <localpath watch="/opt/tongbu">
>           <remote ip="127.0.0.1" name="tongbu1"/>
31c31
<           <auth start="true" users="rsync_daemon" passwordfile="/etc/rsync.pas"/>
---
>           <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>

密码文件配置

[root@rsync-2 GNU-Linux-x86]# echo 123456789 > /etc/rsync.pas
[root@rsync-2 GNU-Linux-x86]# chmod 400 /etc/rsync.pas

sersync启动
一、rsync-1服务器启动

[root@rsync-1 GNU-Linux-x86]# /root/GNU-Linux-x86/sersync2 -d -r -o /root/GNU-Linux-x86/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:  /root/GNU-Linux-x86/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 rsync_daemon
passwordfile is         /etc/rsync.pas
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 /root/updatedir && rsync -artuz -R --delete ./ [email protected]::update --password-file=/etc/rsync.pas >/dev/null 2>&1
run the sersync:
watch path is: /root/updatedir
  • -d 以后台daemon的方式运行
  • -r 第一次启动时,使用rsync将本地文件全部同步至远程服务器。
  • -o 加载配置文件

二、rsync-2服务器启动

[root@rsync-2 GNU-Linux-x86]# /root/GNU-Linux-x86/sersync2 -d -r -o /root/GNU-Linux-x86/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:  /root/GNU-Linux-x86/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 rsync_daemon
passwordfile is         /etc/rsync.pas
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 /root/updatedir && rsync -artuz -R --delete ./ [email protected]::update --password-file=/etc/rsync.pas >/dev/null 2>&1
run the sersync:
watch path is: /root/updatedir

简单测试

  1. 首先在任意服务器上清空原来的文件
  2. 在rsync-1上写入一个500M大小的rsync-1文件
[root@rsync-1 updatedir]# dd if=/dev/zero of=./rsync-1 bs=500M count=1
  1. 在rsync-2上写入一个300M大小的rsync-1文件
[root@rsync-2 updatedir]# dd if=/dev/zero of=./rsync-2 bs=300M count=1

查看两边服务器同步情况
rsync-1:

[root@rsync-1 updatedir]# du -sh *
500M    rsync-1
300M    rsync-2

rsync-2:

[root@rsync-2 updatedir]# du -sh *
500M    rsync-1
300M    rsync-2

至此,文件双向同步已经配置完成。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章