前言
之前的文章又寫過rsync+inotify數據實時同步,在文章最後我說過了要更新出來rsync+sersync,今天來仔細分析分析一下他們兩者的區別
rsync+sersync簡述
- sersync是
基於Inotify開發
的,類似於Inotify-tools的工具 - sersync可以記錄下被監聽目錄中發生變化的(包括增加、刪除、修改)具體某一個文件或某一個目錄的名字,然後使用rsync同步的時候,只同步發生變化的這個文件或者這個目錄。
Rsync+Inotify-tools與Rsync+sersync這兩種架構有什麼區別?
rsync+inotify-tools
-
Inotify-tools只能記錄下被監聽的目錄發生了變化(包括增加、刪除、修改),
並沒有把具體
是哪個文件或者哪個目錄發生了變化記錄下來; -
rsync在同步的時候,並不知道具體是哪個文件或者哪個目錄發生了變化,每次都是對
整個目錄進行同步
,當數據量很大時,整個目錄同步非常耗時(rsync要對整個目錄遍歷查找對比文件),因此,效率很低。
rsync+sersync
- sersync可以記錄下被監聽目錄中發生變化的(包括增加、刪除、修改)
具體
某一個文件或某一個目錄的名字; - rsync在同步的時候,
只同步發生變化
的這個文件或者這個目錄(每次發生變化的數據相對整個同步目錄數據來說是很小的,rsync在遍歷查找比對文件時,速度很快),因此,效率很高。
總結:當同步的目錄數據量不大時,中小型公司建議使用Rsync+Inotify-tools;當數據量很大(幾百G甚至1T以上)、文件很多時,建議使用Rsync+sersync。
實驗環境
名稱 | IP | 操作系統 |
---|---|---|
源服務器 | 192.168.10.1 | Centos7.3 |
目標服務器 | 192.168.10.5 | Centos7.3 |
目的:實現源服務器/var/www/html
的內容備份同步到目標服務器/rsync
目標服務器部署rsync
關閉防火牆或者開啓873端口
[root@rsync ~]# systemctl stop firewalld
[root@rsync ~]# setenforce 0
rsync默認已經集成了Linux7.x圖形化環境,不過我們也可以使用yum源下載個最新版也是沒問題的
[root@rsync ~]# yum -y install rsync
[root@rsync ~]# vim /etc/rc.d/rc.local #設置開機自啓動
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
[root@rsync ~]# chmod +x /etc/rc.d/rc.local
[root@rsync ~]# systemctl start rsyncd
創建rsyncd.conf配置文件
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
secretsfile = /etc/rsyncd.pass
motd file = /etc/rsyncd.motd
[rsync]
path = /rsync
comment = rsync
uid = root
gid = root
port = 873
use chroot = no
read only = no
list = no
maxconnections = 200
timeout = 600
auth users = rsync
hosts allow = 192.168.10.0/24
# hosts deny =
配置文件詳細說明
log file =/var/log/rsyncd.log #日誌文件位置,啓動rsync後自動產生這個文件,無需提前創建
pidfile =/var/run/rsyncd.pid #pid文件的存放位置
lock file =/var/run/rsync.lock #支持max connections參數的鎖文件
secretsfile = /etc/rsyncd.pass #用戶認證配置文件,裏面保存用戶名稱和密碼,後面會創建這個文件
motd file =/etc/rsyncd.Motd #rsync啓動時歡迎信息頁面文件位置(文件內容自定義)
[rsync] #共享模塊名稱,若需要同步到多個目錄,可以寫多個
path = /var/www/html/#rsync服務端數據目錄路徑
comment = rsync#模塊名稱與[md]自定義名稱相同
uid = root #設置rsync運行權限爲root
gid = root #設置rsync運行權限爲root
port=873 #默認端口
use chroot= no #默認爲true,修改爲no,增加對目錄文件軟連接的備份
read only =no #設置rsync服務端文件爲讀寫權限
list = no #不顯示rsync服務端資源列表
maxconnections = 200 #最大連接數
timeout =600 #設置超時時間
auth users= rsync #執行數據同步的用戶名,可以設置多個,用英文狀態下逗號隔開
hosts allow= 192.168.10.0/24 #允許進行數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開
hosts deny= #禁止數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開
創建用戶文件
格式,用戶名:密碼,可以設置多個,每行一個用戶名:密碼
[root@rsync rsync]# cat /etc/rsyncd.pass
rsync:123.com
[root@rsync ~]# chmod 600 /etc/rsyncd.conf
[root@rsync ~]# chmod 600 /etc/rsyncd.pass
[root@rsync ~]#systemctl restart rsyncd #重新啓動
配置源服務器rsync客戶端
同上,先關閉防火牆或開放端口,然後下載rsync
在配置文件添加需要發佈同步的目錄
[root@sersync ~]# systemctl stop firewalld
[root@sersync ~]# setenforce 0
[root@serysyc ~]# whereis rsync
rsync: /usr/bin/rsync /etc/rsync.pass /usr/share/man/man1/rsync.1.gz
[root@serysyc ~]# vim /etc/rc.d/rc.local
/usr/bin/rsync --daemon
[root@serysyc ~]# vim /etc/rsyncd.conf
log file = /var/log/rsync.log
pidfile = /var/run/rsync.pid
lock file = /var/run/rsync.lock
motd file = /etc/rsync.motd
[rsync] # 應該與目標服務器的共享模塊名稱對應,有多個可以添加多個分別對應自己模塊名來同步多目錄多數據
comment = rsync
uid = root
gid = root
port = 873
hosts allow = 192.168.10.0/24
path = /var/www/html
read only = no
auth users = rsync
secrets file = /etc/passwd.txt
[root@serysyc ~]# chmod +x /etc/rc.d/rc.local
[root@serysyc ~]# systemctl start rsyncd
創建認證密碼文件
編輯文件,添加以下內容,該密碼應與目標服務器中的/etc/rsyncd.pass中的密碼一致
[root@serysyc ~]# vim /etc/passwd.txt
123.com
[root@serysyc ~]# chmod 600 /etc/passwd.txt
測試從源服務器向目標服務器同步數據
[root@serysyc ~]# yum -y install httpd
[root@serysyc ~]# cd /var/www/html/
[root@serysyc html]# echo '123' > index.html
[root@serysyc ~]# rsync -avH --delete /var/www/html/ rsync@192.168.10.5::rsync --password-file=/etc/passwd.txt
目標服務器查看同步的數據
[root@rsync rsync]# ll
總用量 4
-rw-r--r--. 1 root root 4 6月 14 20:11 index.html
[root@rsync rsync]# cat index.html
123
源服務器安裝sersync工具,實時觸發rsync進行同步
sersync軟件包:
鏈接:https://pan.baidu.com/s/1VI_Gm-FbQPe3cFyZtJeIuA
提取碼:4sku
上傳sersync2.5.4_64bit_binary_stable_final.tar.gz到/root目錄下
[root@serysyc ~]# tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@serysyc ~]# mv GNU-Linux-x86/ /usr/local/sersync
[root@serysyc ~]# cd /usr/local/sersync/
[root@serysyc sersync]# cp confxml.xml confxml.xml.bak
[root@serysyc sersync]# vim confxml.xml
參數說明:
localpath watch="/var/www/html":#源服務器同步目錄
192.168.10.5:#目標服務器IP地址
name=“rsync”: #目標服務器rsync同步目錄共享模塊名稱
users=“rsync”: #目標服務器rsync同步用戶名
passwordfile="/etc/passwd.pass": #目標服務器rsync同步用戶的密碼在源服務器的存放路徑
remote ip=“192.168.10.5”: #目標服務器ip,每行一個
failLogpath="/tmp/rsync_fail_log.sh" #腳本運行失敗日誌記錄
start=“true” #設置爲true,每隔600分鐘執行一次全盤同步
🆗,現在檢測實時同步
在源服務器/var/www/html目錄寫入數據
[root@serysyc html]# echo '123' > 2.txt
[root@serysyc html]# echo '123.com' > 3.txt
[root@serysyc html]# echo '123.com' > 4.txt
[root@serysyc ~]# /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
## 執行後完成此命令後,會一直處在持續監控狀態,達到實時同步目的
....
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /var/www/html && rsync -artuz -R --delete ./ [email protected]::rsync --password-file=/etc/passwd.txt >/dev/null 2>&1
run the sersync:
watch path is: /var/www/html
在目標服務器查看同步情況
[root@rsync rsync]# ll
總用量 20
-rw-r--r--. 1 988 1001 7 6月 14 23:39 1.txt
-rw-r--r--. 1 root root 4 6月 14 21:57 2.txt
-rw-r--r--. 1 root root 8 6月 14 22:00 3.txt
-rw-r--r--. 1 root root 7 6月 14 22:27 4.txt
-rw-r--r--. 1 988 1001 4 6月 14 20:11 index.html
設置sersync監控開機自動執行
結合crontab計劃任務,定時檢查sersync2是否運行,如果沒有,則執行腳本重新執行/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
來持續監控源服務器
vim /etc/rc.d/rc.local #編輯,在最後添加一行
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml #設置開機自動運行此服務
chmod +x /etc/rc.d/rc.local
添加腳本監控sersync正常運行
#!/bin/sh
sersync="/usr/local/sersync/sersync2"
confxml="/usr/local/sersync/confxml.xml"
status=$(ps -aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ];
then
$sersync -d -r -o $confxml &
else
exit 0;
fi
做計劃任務,每一小時檢查一次
* */1 * * * /root/check_sersync.sh
遇到的報錯及解決方法
報錯一:
如果前面測試連接時就報錯
如下:
[root@serysyc ~]# rsync -avH --delete /var/www/html/ rsync@192.168.10.5::rsync --password-file=/etc/passwd.txt
sending incremental file list
rsync: failed to set times on "." (in rsync): Operation not permitted (1)
rsync: delete_file: unlink(aaaa) failed: Permission denied (13)
./
1.txt
rsync: mkstemp ".1.txt.g3qgm2" (in rsync) failed: Permission denied (13)
sent 132 bytes received 30 bytes 324.00 bytes/sec
total size is 12 speedup is 0.07
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
解決辦法
修改配置文件/etc/rsyncd.conf
uid = root
gid = root
因爲同步的數據在源服務器可能是root管理,同步過來還得是root,再者就是把源數據的所屬者改成和目標服務器的一樣,在同步數據。
報錯二:
如下
[root@sersync ~]# rsync -avzH --delete /var/www/html [email protected]::rsync --password-file=/etc/passwd.txt
rsync: failed to connect to 192.168.10.5 (192.168.10.5): Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(125) [sender=3.1.2]
解決辦法:
有經驗的人一看報錯信息就知道是rsyncd服務沒有起來
重啓rsyncd
systemctl start rsyncd
確保873端口開啓
[root@sersync ~]# telnet 192.168.10.5 873
Trying 192.168.10.5...
Connected to 192.168.10.5.
Escape character is '^]'.
@RSYNCD: 31.0
[root@sersync ~]# netstat -anpt |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 3725/rsync
tcp 0 0 192.168.10.1:52144 192.168.10.5:873 TIME_WAIT -
tcp6 0 0 :::873 :::* LISTEN 3725/rsync
還有如果都沒有問題,檢查你的防火牆
,是否關閉或開放873端口