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

至此,文件雙向同步已經配置完成。

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