rsync+inotify實時同步環境部署記錄

rsync+inotify實時同步環境部署記錄
在使用rsync首次全量同步後,結合inotify對源目錄進行實時監控,只有有文件變動或新文件產生,就會立刻同步到目標目錄下,非常高效使用!
一)單向同步
需求:10.60.123.133同步/usr/local/tomcat-api/webapps/upload數據到10.60.123.134對應目錄
(1)10.60.123.133是源服務器,作爲rsync的客戶端,部署rsync+inotify。
(2)10.60.123.134是目標服務器,作爲rsync的服務端。只需要安裝配置rsync即可,不需要安裝inotify。

第一部分:在目標服務器10.60.123.134上部署rsync服務端:
1)關閉selinux
[root@bastion-IDC ~]# vim /etc/selinux/config
SELINUX=disabled
[root@bastion-IDC ~]# setenforce 0

2)防火牆上允許以上三臺源服務器訪問它的22端口和873端口
[root@bastion-IDC ~]# vim /etc/sysconfig/iptables
.......
.......
-A INPUT -s 192.168.1.106 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 192.168.1.57 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 192.168.1.139 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 192.168.1.106 -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
-A INPUT -s 192.168.1.57 -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
-A INPUT -s 192.168.1.139 -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT

[root@bastion-IDC ~]# /etc/init.d/iptables restart

如若hosts.allow裏面做了限制,同樣也要開放以上三臺源服務器的權限
[root@bastion-IDC ~]# vim /etc/hosts.allow
#

hosts.allow This file contains access rules which are used to

allow or deny connections to network services that

either use the tcp_wrappers library or that have been

started through a tcp_wrappers-enabled xinetd.

#

See 'man 5 hosts_options' and 'man 5 hosts_access'

for information on rule syntax.

See 'man tcpd' for information on tcp_wrappers

#
sshd:192.168.1.106,192.168.1.57,192.168.1.139:allow
sshd:all:deny
[root@bastion-IDC ~]# cat /etc/hosts.deny
#

hosts.deny This file contains access rules which are used to

deny connections to network services that either use

the tcp_wrappers library or that have been

started through a tcp_wrappers-enabled xinetd.

#

The rules in this file can also be set up in

/etc/hosts.allow with a 'deny' option instead.

#

See 'man 5 hosts_options' and 'man 5 hosts_access'

for information on rule syntax.

See 'man tcpd' for information on tcp_wrappers

#

3)安裝部署rsync服務
注意:CentOS中是以xinetd來管理Rsync服務的
[root@bastion-IDC ~]# yum install rsync xinetd
[root@bastion-IDC ~]# vim /etc/xinetd.d/rsync
.....
disable = no #由默認的yes改爲no,設置開機啓動rsync

啓動xineted服務
[root@bastion-IDC ~]# /etc/init.d/xinetd start

創建/etc/rsyncd.conf文件,並配置同步策略。添加如下代碼:
[root@bastion-IDC ~]# vim /etc/rsyncd.conf
log file = /var/log/rsyncd.log
#日誌文件位置,啓動rsync後自動產生這個文件,無需提前創建
pidfile = /var/run/rsyncd.pid
#pid文件的存放位置
lock file = /var/run/rsync.lock
#支持max connections參數的鎖文件
secrets file = /etc/rsync.passwd
#用戶認證配置文件,裏面保存用戶名稱和密碼,後面會創建這個文件
motd file = /etc/rsyncd.Motd
#rsync啓動時歡迎信息頁面文件位置(自己創建這個文件,內容隨便自定義)

[pic_upload]
#自定義名稱
path = /usr/local/tomcat-api/webapps
#rsync服務端數據目錄路徑,即同步到目標目錄後的存放路徑
comment = pic_upload
#模塊名稱與[pic_upload]自定義名稱相同
uid = nobody
#設置rsync運行的uid權限。這個要保證同步到目標目錄後的權限和源目錄一致,即都是nobody!
gid = nobody
#設置rsync運行的gid權限。
port=873
#默認的rsync端口
use chroot = no
#默認爲true,修改爲no或false,增加對目錄文件軟連接的備份
read only = no
#設置rsync服務端文件爲讀寫權限
list = no
#不顯示rsync服務端資源列表
max connections = 200
#最大連接數
timeout = 600
#設置超時時間
auth users = RSYNC_USER
#執行數據同步的用戶名,需要後面手動設置。可以設置多個,用英文狀態下逗號隔開
hosts allow = 10.60.123.133
#允許進行數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開
#hosts deny = 192.168.1.194
#禁止數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開(如果沒有禁止,就不用設置這一行)

創建用戶認證文件 (多個模塊用的是這同一個認證文件)
[root@bastion-IDC ~]# vim /etc/rsync.passwd
RSYNC_USER:123456@rsync

設置文件權限,即rsyncd.conf和rsync.pass認證文件都是600權限!
[root@bastion-IDC ~]# chmod 600 /etc/rsyncd.conf
[root@bastion-IDC ~]# chmod 600 /etc/rsync.passwd

重啓rsync服務
[root@bastion-IDC ~]# /etc/init.d/xinetd restart
[root@bastion-IDC ~]# lsof -i:873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
xinetd 22041 root 5u IPv6 3336440 0t0 TCP *:rsync (LISTEN)

第二部分:在源服務器10.60.123.133部署rsync客戶端和inotify監控
關閉selinux,做爲客戶端的rsync可以不用在iptables裏開放873端口
安裝rsync,啓動rsync服務。
創建同步的密碼文件,這個文件名可以跟服務端的認證文件不一樣,但是裏面的密碼必須一致!用於rsync同步命令中。不過,最好兩邊的文件設置成一樣,便於管理
[root@static-img ~]# vim /etc/rsync.passbr/>123456@rsync
設置rsync.pass密碼文件爲600權限
[root@static-img ~]# chmod 600 /etc/rsync.pass
查看服務器內核是否支持inotify,出現下面的內容,說明服務器內核支持inotify
[root@static-img ~]# ll /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Oct 26 12:03 max_queued_events
-rw-r--r-- 1 root root 0 Oct 26 12:03 max_user_instances
-rw-r--r-- 1 root root 0 Oct 26 12:03 max_user_watches

注意:Linux下支持inotify的內核最小爲2.6.13,可以輸入命令:uname -a查看內核
CentOS 5.X 內核爲2.6.18,默認已經支持inotify
[root@static-img ~]# uname -a
Linux static-img 2.6.32-573.22.1.el6.x86_64 #1 SMP Wed Mar 23 03:35:39 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

下面開始安裝inotify-tools
[root@static-img ~]# yum install make gcc gcc-c++ #安裝編譯工具
[root@static-img ~]# cd /usr/local/src
[root@static-img src]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@static-img src]# tar zxvf inotify-tools-3.14.tar.gz
[root@static-img src]# cd inotify-tools-3.14
[root@static-img inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@static-img inotify-tools-3.14]# make && make install

發現已經成功安裝inotify-tools了
[root@static-img inotify-tools-3.14]# ll -d /usr/local/inotify/
drwxr-xr-x 6 root root 4096 Oct 26 12:01 /usr/local/inotify/

設置系統環境變量
[root@static-img ~]# vim /etc/profile
......
export PATH=$PATH:/usr/local/inotify/bin
[root@static-img ~]# source /etc/profile

添加庫文件
[root@static-img ~]# vim /etc/ld.so.conf
/usr/local/inotify/lib
[root@static-img ~]# ldconfig

修改inotify默認參數(inotify默認內核參數值太小)
查看系統默認參數值
[root@static-img ~]# sysctl -a | grep max_queued_events
fs.inotify.max_queued_events = 16384
[root@static-img ~]# sysctl -a | grep max_user_watches
fs.inotify.max_user_watches = 8192
[root@static-img ~]# sysctl -a | grep max_user_instances
fs.inotify.max_user_instances = 128

修改參數:
[root@static-img ~]# sysctl -w fs.inotify.max_queued_events="99999999"
[root@static-img ~]# sysctl -w fs.inotify.max_user_watches="99999999"
[root@static-img ~]# sysctl -w fs.inotify.max_user_instances="65535"

參數說明:
max_queued_events:
inotify隊列最大長度,如果值太小,會出現" Event Queue Overflow "錯誤,導致監控文件不準確
max_user_watches:
要同步的文件包含多少目錄,可以用:find /Data/xqsj_upload -type d | wc -l 統計這些源目錄下的目錄數,必須保證max_user_watches值大於統計結果(這裏/Data/xqsj_upload爲同步的源文件目錄)
max_user_instances:
每個用戶創建inotify實例最大值

4)接着執行同步操作:
分別在三臺源服務器上執行rsync首次全量同步的操作(加--delete參數,保持目標目錄和源目錄下文件絕對一致)

在10.60.123.133服務器上
第一次全量同步:
rsync -avH --port=873 --progress --delete /usr/local/tomcat-api/webapps/upload [email protected]::pic_upload --password-file=/etc/rsync.pass
待第一次rsync全量同步完成後,就進行rsync+inotify實時同步腳本操作。
實時同步腳本里添加的是--delete-before參數,而不是--delete參數(第一次全量同步時rsync用的參數),二者區別:
--delete參數:表示rsync同步前,暴力刪除目標目錄中的所有文件,然後再執行同步操作。
--delete-before參數:表示rsync同步前,會先對目標目錄進行一次掃描檢索,刪除目標目錄中對比源目錄的多餘文件,然後再執行同步操作。顯然比--delete參數安全些。

vim /etc/rsync_pic_upload_inotify.sh
#!/bin/bash
SRCDIR=/usr/local/tomcat-api/webapps/upload
USER=RSYNC_USER
IP=10.60.123.134
DESTDIR=pic_upload
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $SRCDIR | while read file
do
/usr/bin/rsync -avH --port=873 --progress --delete-before $SRCDIR $USER@$IP::$DESTDIR --password-file=/etc/rsync.pass
echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
done

然後啓動同步腳本,放在後臺執行!
[root@fangfull_web1 rsync]# nohup sh rsync_pic_upload_inotify.sh &
最後進行測試:
比如在源目錄/usr/local/tomcat-api/webapps/upload中創建一個文件或目錄,會自動實時同步到目標機器10.60.123.134的目標目錄/usr/local/tomcat-api/webapps/upload中。

果在同步過程中,發現中途報錯!重複執行同步命令一直是報這個錯誤:
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at
main.c(1505)

最後發現原因:
是因爲在同步的時候,源目錄下有軟鏈接文件!
rsync同步軟鏈接文件,應該加參數-l

所以,最好在使用rsync同步命令的時候,後面跟-avpgolr參數組合(將上面的-avH改成-avpgolr)
-a:遞歸
-v:打印詳細過程
-p:保持文件屬性
-g:文件所屬組不變
-o:文件所屬者不變
-l:軟連接屬性
-r:同步目錄時的參數

二)雙向同步
需求:10.60.123.133同步/usr/local/tomcat-api/webapps/upload數據到10.60.123.134對應目錄,反向也可。
雙向主要差異就是二臺均要安裝rsync+inotify 之間873端口互通,二臺都要有/etc/rsyncd.conf ip配置對端 uid = root才行, /etc/rsync.passwd 配置用戶加密碼,/etc/rsync.pass配置密碼文件,/etc/rsync_pic_upload_inotify.sh配置對端地址便可。

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