Rsync原理:
Rsync是基於rsync算法校驗源(SRC)與目標(DEST)之間的差異實現數據同步的。也就是說,當使用rsync工具同步數據的時候,只複製源發生改變的文件,到目標(DEST),這類似於增量備份。所以rsync同步數據的速度是很快的。但是rsync的傳輸性能很差,在海量小文件需要同步時,表現很不好。因爲rsync是基於文件同步的(不是塊級別的),在傳輸之前還要進行校驗。
Rsync的工作模式有多種,使用rsync的服務模式來構建:主/從服務器的數據同步。此時rsync工作爲守護進程監聽在:TCP/873,能收到客戶端的數據同步請求。
inotify原理:
inotify可以監控文件,也可以監控目錄。當監控目錄時,它可以同時監控目錄及目錄中的各子目錄及文件的變化。由於intify使用文件掃描符作爲藉口,因此可以使用通常的文件I/O操作例如select、pool、epoll來見識文件系統的變化
環境需求:
Host1:
CentOS-7-x86_64
hostname:ws1
ipaddr:10.0.0.61
Host2:
CentOS-6-x86_64
hostname:ws2
ipaddr:10.0.0.62
注意:兩臺主機均爲最小化安裝iptables和selinux均處於關閉狀態
配置Host1
CentOS 7最小化安裝,默認已安裝rsync包
編輯rsync的配置文件
# vim /etc/rsyncd.conf uid = root //rsync同步共享用戶的身份 gid = root //rsync同步共享用戶組的身份 use chroot = no //不使用chroot port = 873 //定義監聽的端口,默認873 max connections = 200 //服務器最大併發數 timeout = 300 //會話超時時長 pid file = /var/run/rsyncd.pid //pid文件存放路徑 lock file = /var/run/rsyncd.lock //lock文件存放路徑,服務啓動停止時會用到 log file = /var/log/rsyncd.log //日誌文件存放路徑 log format = %h %o %1 %b //日誌記錄格式 ignore errors //忽略錯誤信息 read only = false //是否允許用戶上傳文件,默認爲true list = false //是否允許用戶列出文件,默認爲true write only = false //是否允許下載,默認爲true hosts allow = 10.0.0.61/8 //允許哪些主機訪問 hosts deny = * s //拒絕哪些主機訪問 auth users = sync //同步時認證所需的用戶 secrets file = /etc/rsyncd.passwd //認證時所需的密碼文件 [data] //模塊名稱,客戶端需精確指定模塊名稱 path = /data //模塊對應的目錄路徑
創建認證時的密碼文件/etc/rsyncd.passwd(需要用戶名和密碼、明文存儲,僅屬主可讀)
# echo "sync:123456" > /etc/rsynd.passwd # chmod 600 /etc/rsynd.passwd
創建模塊對應的目錄,並確保屬主和屬組與配置文件中定義的uid和gid一致
# mkdir -p /data/ # ls -ld /data/ drwxr-xr-x 2 root root 21 5月 27 22:47 /data/ # cp /var/log/message /data/
啓動服務,並檢查進程和監聽端口
# systemctl start rsyncd.service # ps aux | grep rsync root 3121 0.0 0.1 114640 1164 ? Ss 22:47 0:00 /usr/bin/rsync --daemon --no-detach root 3123 0.0 0.0 112660 956 pts/0 R+ 22:48 0:00 grep --color rsync # ss -tunl | grep 873 tcp LISTEN 0 5 *:873 *:* tcp LISTEN 0 5 :::873 :::*
配置host2
創建認證時的密碼文件~/rsyncd。passwd(僅需密碼,明文存儲、僅屬主可讀)
# echo "123456" > rsyncd.passwd # chmod 600 rsyncd.passwd
測試查看rsync服務器上data模塊中的文件
]# rsync --password-file=rsyncd.passwd [email protected]::data drwxr-xr-x 21 2016/05/27 22:47:34 . -rw------- 242668 2016/05/27 22:47:35 messages
注意:再一次踩坑,一定一定一定檢查防火牆
建立本地數據目錄文件/data,將/data目錄與rsync服務器上data模塊中定義的目錄/data作爲數據同步目錄,即這兩個目錄中的數據要時刻保持一致。
測試:將host1的/data目錄中的數據取過來。
# rsync -avz --password-file=rsyncd.passwd [email protected]::data /data/ receiving incremental file list ./ messages sent 76 bytes received 26839 bytes 53830.00 bytes/sec total size is 242668 speedup is 9.02
然後在本地創建幾個文件使用rsync命令同步到Host1的data目錄中
# rsync -avz --password-file=rsyncd.passwd /data/ [email protected]::data sending incremental file list ./ rsyncd.passwd sent 2567 bytes received 34 bytes 5202.00 bytes/sec total size is 30649078 speedup is 11783.57
注意:目錄後面的/ 如果少加了一個/,會將data目錄本身而不是目錄下的文件同步過去
# rm -f /data/fstab # rsync -avz --delete --password-file=rsyncd.passwd /data/ [email protected]::data sending incremental file list deleting fstab sent 2521 bytes received 12 bytes 5066.00 bytes/sec total size is 30649078 speedup is 12099.91
此時手動同步兩個節點上目錄中數據文件是沒有問題了,現在需要實現的是Host2上的數據一改變立刻同步Host1上的數據,這就需要用到inotify了
inptify
inotify是內核中功能,但是需要通過系統調用接口程序才能夠使用,這個接口程序由inotify-tools提供,所以嘛咱得安裝!!!此處我們在host2上安裝
# yum install -y inotify-tools # # rpm -ql inotify-tools /usr/bin/inotifywait /usr/bin/inotifywatch /usr/lib64/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0.4.1
以下是幾個inotify相關的內核參數,可能根據實際需要調整
# ls -l /proc/sys/fs/inotify/ 總用量 0 -rw-r--r-- 1 root root 0 5月 28 00:23 max_queued_events -rw-r--r-- 1 root root 0 5月 28 00:23 max_user_instances -rw-r--r-- 1 root root 0 5月 28 00:23 max_user_watches
實時監控數據目錄文件,發生改變立刻調用rsync進行數據同步,腳本如下
# vim auto_rsync.sh #!/bin/bash sync_user="sync" sync_host="10.0.0.61" sync_mod="data" data_dir="/data/" rsync_cmd="rsync -vzrtopg --delete --progress --password-file=./rsyncd.passwd $data_dir $sync_user@$sync_host::$sync_mod" inotifywait -mrq $data_dir --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib | while read files; do $rsync_cmd done
啓動腳本,然後修改Host2 數據目錄中的文件,觀察變化
bash auto_rsync.sh sending incremental file list ./ deleting issue deleting .auto_rsync.sh.swp auto_rsync.sh 359 100% 0.00kB/s 0:00:00 (xfer#1, to-check=108/110) sent 2800 bytes received 34 bytes 5668.00 bytes/sec total size is 30649390 speedup is 10814.89