rsync數據同步優缺點
與傳統的cp、tar備份方式相比,rsync具有安全性高、備份迅速、支持增量備份等優點,通過rsync可以解決對實時性要求不高的數據備份需求,例如定期的備份文件服務器數據到遠端服務器,對本地磁盤定期做數據鏡像等。
隨着應用系統規模的不斷擴大,對數據的安全性和可靠性也提出的更好的要求,rsync在高端業務系統中也逐漸暴露出了很多不足。首先,rsync同步數據時,需要掃描所有文件後進行比對,進行差量傳輸。如果文件數量達到了百萬甚至千萬量級,掃描所有文件將是非常耗時的。而且正在發生變化的往往是其中很少的一部分,這是非常低效的方式。其次,rsync不能實時的去監測、同步數據,雖然它可以通過linux守護進程的方式進行觸發同步,但是兩次觸發動作一定會有時間差,這樣就導致了服務端和客戶端數據可能出現不一致,無法在應用故障時完全的恢復數據。基於以上原因,rsync+inotify組合出現了!
inotify
inotify是一種強大的、細粒度的、異步的文件系統事件監控機制,linux內核從2.6.13起,加入了inotify支持,通過inotify可以監控文件系統中添加、刪除,修改、移動等各種細微事件,利用這個內核接口,第三方軟件就可以監控文件系統下文件的各種變化情況,而inotify-tools就是這樣的一個第三方軟件。
一、環境準備
IP地址: inotify_rsync_client:192.168.1.103 rsync_server:192.168.1.189 操作系統: centos 6.9 64位
二、rsync部署(每步說明了在那裏配置)
1、關閉SELINUX(inotify_rsync_client、rsync_server均配置) vi /etc/selinux/config 修改下面一行代碼: SELINUX=disabled 運行此命令立即生效。 setenforce 0 2、開啓防火牆tcp 873端口、或關掉防火牆(inotify_rsync_client、rsync_server均配置) vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT 或者關閉防火牆 service iptables stop chkconfig iptables off 3、安裝rsync服務端軟件(inotify_rsync_client配置) yum install rsync xinetd vi /etc/xinetd.d/rsync 修改disable的值爲no: disable = no 啓動xinetd(CentOS中是以xinetd來管理Rsync服務的) /etc/init.d/xinetd start chkconfig xinetd on 4、安裝xinetd(rsync_server配置) yum install rsync xinetd vi /etc/xinetd.d/rsync 修改disable的值爲no: disable = no 啓動xinetd(CentOS中是以xinetd來管理Rsync服務的) /etc/init.d/xinetd start chkconfig xinetd on rsync --daemon --config=/etc/rsyncd.conf echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.local 5、創建rsyncd.conf配置文件(rsync_server配置。假如需要同步多個目錄,注意加多個目錄;此root是rsync的認證賬號,後面步驟會配置認證賬號和密碼) 創建配置文件: vi /etc/rsyncd.conf log file = /var/log/rsyncd.log pidfile = /var/run/rsyncd.pid lock file = /var/run/rsync.lock secrets file = /etc/rsyncd.secret motd file = /etc/rsyncd.motd [test] path = /home/xyz/ comment = test uid = root gid = root incoming chmod = Du=rwx,Dog=rx,Fu=rwx,Fgo=rx port=873 use chroot = no read only = no list = no max connections = 200 timeout = 600 auth users = root hosts allow = 192.168.1.103 hosts deny = * 目錄權限(如果uid和gid都是root,這裏不用操作): cd /home chown -hR root.root xyz/ #如果xyz目錄是在root下新建的,默認就是root權限。 6.創建用戶認證文件(rsync_server) 配置文件 vi /etc/rsyncd.passwd root:123456 保存退出 7.設置文件權限(rsync_server) 設置文件所有者讀取、寫入權限 chmod 600 /etc/rsyncd.conf chmod 600 /etc/rsyncd.passwd 8.啓動rsync(rsync_server) /etc/init.d/xinetd start 參考指令 停止:service xinetd stop 啓動:service xinetd restart 9、創建用戶認證文件(inotify_rsync_client配置) 配置文件 vi /etc/rsyncd.passwd 123456 保存退出 chmod 600 /etc/rsyncd.passwd 10、從inotify_rsync_client手動rsync同步到rsync_server看下,只有成功後(查看日誌),下面inotify纔會成功。(inotify_rsync_client上運行此命令測試) rsync -avzrtopgL --progress /root/ [email protected]::test/ --password-file=/etc/rsyncd.passwd
三、sersync部署(inotify_rsync_client配置)
1、查看服務器內核是否支持inotify 列出文件目錄 ll /proc/sys/fs/inotify 出現下面的內容、說明服務器內核支持inotify -rw-r--r-- 1 root root 0 Dec 25 12:03 max_queued_events -rw-r--r-- 1 root root 0 Dec 25 15:05 max_user_instances -rw-r--r-- 1 root root 0 Dec 25 12:03 max_user_watches 備註:centos6.9默認支持inotify 2.修改inotify默認參數(inotify默認內核參數值太小) sysctl -a | grep max_queued_events sysctl -a | grep max_user_watches sysctl -a | grep max_user_instances 修改參數 sysctl -w fs.inotify.max_queued_events="99999999" sysctl -w fs.inotify.max_user_watches="99999999" sysctl -w fs.inotify.max_user_instances="65535" 生效 sysctl -p 參數說明: max_queued_events: inotify隊列最大長度,如果值太小,會出現” Event Queue Overflow “錯誤,導致監控文件不準確 max_user_watches: 要同步的文件包含多少目錄,可以用:find /var/www/synctest -type d | wc -l 統計,必須保證max_user_watches值大於統計結果(這裏/var/www/synctest爲同步文件目錄) max_user_instances: 每個用戶創建inotify實例最大值 3、最好更改最大連接數、最大文件描述符。 vi /etc/pam.d/login session required /lib64/security/pam_limits.so vi /etc/security/limits.conf * soft nproc 65535 * hard nproc 65535 * soft nofile 65535 * hard nofile 65535 重啓服務器 4、安裝編譯工具和inotify-tools [root@Monitor nginx]# yum install make gcc gcc-c++ [root@Monitor nginx]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz [root@Monitor nginx]# tar xf inotify-tools-3.14.tar.gz [root@Monitor nginx]# cd inotify-tools-3.14 [root@Monitor inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify [root@Monitor inotify-tools-3.14]#make [root@Monitor inotify-tools-3.14]#make && make install 5、命令和手動測試 一共安裝了2個工具(命令),即inotifywait和inotifywatch inotifywait:在被監控的文件或目錄上等待特定文件系統事件(open、close、delete等)發生,執行後處於阻塞狀態,適合在shell腳本中使用。 inotifywatch:收集被監視的文件系統使用度統計數據,指定文件系統事件發生的次數統計。 1)inotify命令常用參數詳解 [root@nfs-server inotify-tools]# ./bin/inotifywait --help 2)測試監控事件create: [root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data ##實行當前命令後,界面處於阻塞狀態,只有在另外一個客戶端測試時,纔會顯示監控狀態 31/01/16 10:23 /data/ceshi.txt 31/01/16 10:24 /data/what 31/01/16 10:27 /data/a 31/01/16 10:27 /data/b 31/01/16 10:27 /data/c 31/01/16 10:27 /data/d 31/01/16 10:27 /data/e 31/01/16 10:27 /data/f 克隆一個NFS客戶端切換到/data目錄下,新建ceshi.txt、what文件,觀察監控的變化 [root@nfs-server data]# touch ceshi.txt [root@nfs-server data]# mkdir /who [root@nfs-server data]# touch what [root@nfs-server data]# [root@nfs-server data]# touch {a..f} 3)測試監控事件delete: [root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete /data ##監控/data目錄刪除時間 監控如下: 31/01/16 10:31 /data/reew.txt 31/01/16 10:31 /data/test1.txt [root@nfs-server data]# rm -f reew.txt test1.txt ##刪除這兩個文件 [root@nfs-server data]# 4)測試監控事件create、delete: [root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create /data 監控輸出信息: 31/01/16 10:35 /data/hero.sh 31/01/16 10:35 /data/a 31/01/16 10:35 /data/f 31/01/16 10:36 /data/b 31/01/16 10:36 /data/c 31/01/16 10:36 /data/d 31/01/16 10:36 /data/e [root@nfs-server data]# touch hero.sh [root@nfs-server data]# ll [root@nfs-server data]# rm -f [a..f] [root@nfs-server data]# rm -f {a..f} [root@nfs-server data]# 一般工作中使用到: [root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /data 6、工作環境中通過腳本實時同步 #!/bin/bash inotify=/usr/local/inotify-tools/bin/inotifywait $inotify -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /data |while read file do cd / && rsync -az /data --delete [email protected]::test --password-file=/etc/rsync.password done chmod +x inotify.sh sh -x inotify.sh 7、同步文件測試 8、後臺運行腳本或者設置定時任務