一、前言
無論使用什麼操作系統下,都經常有同步文件的需求,不管發生在本地,還是發生在本地和遠程主機之間。那麼應該怎麼做呢?
使用拷貝類的命令,本地使用cp命令,複製到遠程主機使用scp這樣的命令,保證複製到目標和源的一模一樣。
但是,這種複製一般來說,只能判斷文件在目標中是否存在,然後對存在的文件或目錄可以選擇覆蓋或者不復制,覆蓋能保證文件一致,不復制就不能保證一致,不存在的則創建這個文件或者目錄。
每次同步如果都採用覆蓋,即使只是少數文件發生部分變化,也是全部文件都覆蓋,源和目標倒是保證一致了。但這種方式,會造成極大的網絡帶寬壓力和源、目標磁盤IO的壓力。
如果源中有1G數據怎麼辦,或者更多數據怎麼辦?
Linux下提供了一款工具rsync,它能夠實現本地之間或者本地和遠程之間的數據同步,它能通過分塊滾動校驗和算法和hash算法來計算文件的差異,源端負責計算出差異,將匹配塊引用和差異序列發回目標端,由目標端重新組織生成新的文件。
它的特徵是:
-
分塊提取特徵碼,比較差異
-
對有差異的文件,進行增量複製
這樣大大的節省了帶寬和IO。
二、rsync的推拉模型
(一)服務模式
rsync的使用模型中,生成環境用的最多的還是跨主機間同步,也就是要使用服務模式。
rsync的服務模式
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
-v, --verbose | 詳細信息輸出 |
-q, --quiet | 不顯示非錯誤信息 |
-a, --archive | 歸檔模式 |
-r, --recursive | 遞歸目錄 |
-z, --compress | 啓用數據壓縮 |
--delete | 刪除目標目錄中多餘的文件 |
--progress | 顯示進度 |
--password-file=FILE | 密碼文件存放路徑,注意這個文件的權限 |
(二)拉模型
拉模型指的是主動去服務器的指定端口獲取數據
通過rsync服務拉模型同步:
目標節點主動的連接到源節點後,雙方比較文件差異,然後從源節點同步文件差異生成新文件以求保持一致。
目標節點需要定時去訪問源節點,時間間隔不宜過於快,也不宜過慢。
而且比較差異,是遍歷整個目錄文件,是非常佔資源的事情。
從以上分析可以看出,目標節點主動拉取數據是基於查詢機制的,非常沒有效率。
那麼,解決問題的辦法是使用一種機制,能夠獲取到源節點數據的變化之後,再啓動同步,這將大大減少資源的佔用。而Linux給我們提供了這種機制,就是inotify。
inotify: Linux從2.6.13內核開始引入。通過inotify可以監控文件系統的添加、刪除、修改、移動等各種細微事件,利用這個內核接口,第三方軟件可以監控文件系統的各種變化,而inotify-tools就是其中一種工具。
(三)推模型
推模型指的是將數據主動推送至服務器指定端口
推模型一樣存在怎樣推送、間隔多久推送的問題!
但是,推模型可以結合inotify來決定什麼時候推送數據。
讓源節點通過inotify探查到同步目錄中文件發生了改變,然後源節點主動的將文件推至目標節點,這將大大提高效率。
這樣,源節點使用inotify + rsync客戶端,目標節點配置rsync的服務器。
下面就依照推模型的規劃來完成本次實驗。
三、inotify + rsync的推模型方案實驗
本次實驗使用的操作系統是CentOS 6.5。
# uname -r
2.6.32-431.el6.x86_64
其確保內核的版本是2.6.13之上。
(一)目標節點配置
1、準備服務程序
rsync需要依賴超級守護進程xinetd,服務默認監聽在TCP的873端口,客戶端連接此端口,來推送數據或者拉取數據。
# yum -y install xinetd
# yum -y install rsync
[root@localhost ~]# sed -i 's/yes/no/' /etc/xinetd.d/rsync
[root@localhost ~]# cat /etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
service rsync
{
disable = no
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
# mkdir /tmp/data/ –v
2、提供配置文件
# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
max connections = 10
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
[data]
path = /tmp/data
ignore errors = yes
read only = true
write only = no
list = true
host allow = 192.168.60.0/24
host deny = *
uid = root
gid = root
auth users = tiger, tom
secrets file = /etc/rsyncd.secrets
3、準備用戶認證文件
# vim /etc/rsyncd.secrets
tiger:tigeradm
tom:tomadm
# chmod 600 /etc/rsyncd.secrets
說明
write only如果是yes(true)就不許下載,也就是說,如果是no(false)就允許下載,但要檢查是否有權限。
auth users 用戶認證
secrets file 指明用戶認證所使用的用戶名和密碼所存儲的文件。注意這個文件的權限要400或者600。
4、啓動服務
啓動超級守護進程,讓它代爲監聽TCP 873端口。
# service xinetd start
Starting xinetd: [ OK ]
# ss -tnlp | grep :873
LISTEN 0 64 :::873 :::* users:(("xinetd",1443,5))
(二)源節點配置
1、安裝rsync
# yum -y install rsync
# mkdir /data/
# rsync -vv rsync://[email protected]/data/* /data/
opening tcp connection to 192.168.60.144 port 873
sending daemon args: --server --sender -vve.Ls . "data/*"
Password:
delta-transmission enabled
a.txt
test.txt
sent 91 bytes received 189 bytes 20.74 bytes/sec
total size is 0 speedup is 0.00
2、安裝inotify-tools
# tar xf inotify-tools-3.13.tar.gz
# cd inotify-tools-3.13
# ./configure
# make
# make install
3、inotifywait
inotify-tools提供了有用的工具inotifywait。
-r | 監視目錄及其子目錄 |
-m | 始終處於事件監聽狀態不退出 |
-q --quiet | 靜默,只打印時間 |
-e --event | 指定監聽的事件,如果不指定,監聽所有事件 |
--exclude | 使用擴展正則表達式描述的排除文件 |
--excludei | 使用擴展正則表達式描述的排除文件,忽略大小寫 |
--format | 指定打印的格式,類似printf。 %w 發生事件的目錄名稱 %f 發生事件的文件名稱 %e 發生的事件 %Xe 使用X分隔發生的事件 %T --timefmt選項指定的時間格式 |
--timefmt | 使用和strftime兼容的格式的時間格式 |
access | 文件或目錄內容被讀取 |
modify | 文件或目錄內容被寫入 |
attrib | 文件或目錄屬性改變,即元數據被修改 |
close_write | 在可寫模式打開後關閉 |
close_nowrite | 在只讀模式打開後關閉 |
close | 不管是否讀寫模式,只要是關閉 |
open | 打開 |
moved_to | 文件或目錄移動至監控目錄 |
moved_from | 文件或目錄從監控目錄移動走 |
move | 只要監控目錄有移動事件 |
create | 在監控目錄有文件或目錄被創建 |
delete | 在監控目錄有文件或目錄被刪除 |
delete_self | 監控的目錄和文件本身被刪除 |
unmount | 包含文件或目錄的文件系統卸載 |
[ 事件分析 ]
使用下面的命令,來分析一下各種操作產生的事件是什麼,從而分析出哪些事件是需要監控的。
# inotifywait -rm --format "%T %w %f %e" --timefmt "%Y-%m-%d %H:%M:%S" --excludei ".*\.(swp|swx|swpx)$" /data/
[root@localhost /]# cd /data/
無事件
[root@localhost data]# ls
2014-08-31 06:32:55 /data/ OPEN,ISDIR
2014-08-31 06:32:55 /data/ CLOSE_NOWRITE,CLOSE,ISDIR
事件:open close_nowrite close
[root@localhost data]# touch test
2014-08-31 06:35:54 /data/ test CREATE
2014-08-31 06:35:54 /data/ test OPEN
2014-08-31 06:35:54 /data/ test ATTRIB
2014-08-31 06:35:54 /data/ test CLOSE_WRITE,CLOSE
事件:create open attirb close_write close
[root@localhost data]# mkdir content
2014-08-31 06:36:58 /data/ content CREATE,ISDIR
2014-08-31 06:36:58 /data/ content OPEN,ISDIR
2014-08-31 06:36:58 /data/ content CLOSE_NOWRITE,CLOSE,ISDIR
事件:create open close_nowrite close
[root@localhost data]# ls content/
2014-08-31 06:40:10 /data/ content OPEN,ISDIR
2014-08-31 06:40:10 /data/content/ OPEN,ISDIR
2014-08-31 06:40:10 /data/ content CLOSE_NOWRITE,CLOSE,ISDIR
2014-08-31 06:40:10 /data/content/ CLOSE_NOWRITE,CLOSE,ISDIR
事件:open close_nowrite close
[root@localhost data]# cp /etc/issue content/
2014-08-31 06:41:23 /data/ OPEN,ISDIR
2014-08-31 06:41:23 /data/ CLOSE_NOWRITE,CLOSE,ISDIR
2014-08-31 06:41:25 /data/content/ issue CREATE
2014-08-31 06:41:25 /data/content/ issue OPEN
2014-08-31 06:41:25 /data/content/ issue MODIFY
2014-08-31 06:41:25 /data/content/ issue CLOSE_WRITE,CLOSE
事件:open close_nowrite close create open modify
[root@localhost data]# cat content/issue
2014-08-31 06:44:09 /data/content/ issue OPEN
2014-08-31 06:44:09 /data/content/ issue ACCESS
2014-08-31 06:44:09 /data/content/ issue CLOSE_NOWRITE,CLOSE
事件:open access close_nowrite close
[root@localhost data]# nano testfile
2014-08-31 07:50:35 /data/ testfile CREATE
2014-08-31 07:50:35 /data/ testfile OPEN
2014-08-31 07:50:35 /data/ testfile MODIFY
2014-08-31 07:50:35 /data/ testfile CLOSE_WRITE,CLOSE
事件:create open modify close_write close
[root@localhost data]# vim content/issue
2014-08-31 06:51:03 /data/ OPEN,ISDIR
2014-08-31 06:51:03 /data/ CLOSE_NOWRITE,CLOSE,ISDIR
2014-08-31 06:51:03 /data/ OPEN,ISDIR
……
2014-08-31 06:51:03 /data/content/ issue OPEN
2014-08-31 06:51:03 /data/content/ .issue.swpx CREATE
2014-08-31 06:51:03 /data/content/ .issue.swpx OPEN
2014-08-31 06:51:03 /data/content/ .issue.swpx CLOSE_WRITE,CLOSE
2014-08-31 06:51:03 /data/content/ .issue.swpx DELETE
2014-08-31 06:51:03 /data/ OPEN,ISDIR
2014-08-31 06:51:03 /data/ CLOSE_NOWRITE,CLOSE,ISDIR
2014-08-31 06:51:03 /data/ OPEN,ISDIR
2014-08-31 06:51:03 /data/ CLOSE_NOWRITE,CLOSE,ISDIR
2014-08-31 06:51:03 /data/content/ issue CLOSE_NOWRITE,CLOSE
2014-08-31 06:51:03 /data/content/ issue OPEN
2014-08-31 06:51:03 /data/content/ issue ACCESS
2014-08-31 06:51:03 /data/content/ issue CLOSE_NOWRITE,CLOSE
2014-08-31 06:51:03 /data/ OPEN,ISDIR
2014-08-31 06:51:03 /data/ CLOSE_NOWRITE,CLOSE,ISDIR
2014-08-31 06:51:12 /data/ OPEN,ISDIR
2014-08-31 06:51:12 /data/ CLOSE_NOWRITE,CLOSE,ISDIR
2014-08-31 06:51:12 /data/content/ 4913 CREATE
2014-08-31 06:51:12 /data/content/ 4913 OPEN
2014-08-31 06:51:12 /data/content/ 4913 ATTRIB
2014-08-31 06:51:12 /data/content/ 4913 CLOSE_WRITE,CLOSE
2014-08-31 06:51:12 /data/content/ 4913 DELETE
2014-08-31 06:51:12 /data/content/ issue MOVED_FROM
2014-08-31 06:51:12 /data/content/ issue~ MOVED_TO
2014-08-31 06:51:12 /data/content/ issue CREATE
2014-08-31 06:51:12 /data/content/ issue OPEN
2014-08-31 06:51:12 /data/content/ issue MODIFY
2014-08-31 06:51:12 /data/content/ issue CLOSE_WRITE,CLOSE
2014-08-31 06:51:12 /data/content/ issue ATTRIB
2014-08-31 06:51:12 /data/content/ issue ATTRIB
2014-08-31 06:51:12 /data/content/ issue~ DELETE
2014-08-31 06:51:12 /data/ OPEN,ISDIR
2014-08-31 06:51:12 /data/ CLOSE_NOWRITE,CLOSE,ISDIR
事件:create open attrib delete modify moved_from moved_to close_write close
下面是使用vim打開,無修改關閉的日誌
2014-08-31 06:46:51 /data/content/ issue CLOSE_NOWRITE,CLOSE
2014-08-31 06:46:51 /data/content/ issue OPEN
2014-08-31 06:46:51 /data/content/ issue ACCESS
2014-08-31 06:46:51 /data/content/ issue CLOSE_NOWRITE,CLOSE
可以看出關鍵的事件是close_nowrite
[root@localhost data]# mv content/issue /tmp/
2014-08-31 06:52:48 /data/content/ issue MOVED_FROM
[root@localhost data]# mv /tmp/issue /data/content/
2014-08-31 06:54:55 /data/content/ issue MOVED_TO
事件:moved_from moved_to
[root@localhost data]# rm /data/content/issue
2014-08-31 06:58:25 /data/content/ issue DELETE
[root@localhost data]# rm -rf /data/content
2014-08-31 06:59:53 /data/ content OPEN,ISDIR
2014-08-31 06:59:53 /data/content/ OPEN,ISDIR
2014-08-31 06:59:53 /data/content/ t1.txt DELETE
2014-08-31 06:59:53 /data/ content DELETE,ISDIR
2014-08-31 06:59:53 /data/ content CLOSE_NOWRITE,CLOSE,ISDIR
2014-08-31 06:59:53 /data/content/ CLOSE_NOWRITE,CLOSE,ISDIR
2014-08-31 06:59:53 /data/content/ DELETE_SELF
2014-08-31 06:59:53 /data/content/ IGNORED
事件:open delete delete_self close_nowrite close
[root@localhost data]# chmod 400 testfile
2014-08-31 08:22:53 /data/ testfile ATTRIB
事件:attrib
由此可見,一個監控中的文件夾內容改變的主要事件有:create delete attrib close_write。
選取create是因爲,目錄的創建,不會發生close_write事件。
文件的創建,create事件產生,保存退出,會產生close_write事件。文件的修改,modify事件產生,保存退出,會產生close_write事件。所以選取close_write。
修改一個文件屬性,只會產生attrib事件,所以要選取它。
文件或目錄的刪除都會產生delete事件。
4、部署inotify監控
創建訪問目標節點的訪問密碼文件
# echo "tomadm" > /etc/rsync.accesspasswd
# cat /etc/rsync.accesspasswd
tomadm
# chmod 400 /etc/rsync.accesspasswd
5、推送測試
# rsync -avzr --delete --progress --password-file=/etc/rsync.accesspasswd /data/* rsync://[email protected]/data/
sending incremental file list
ERROR: module is read only
rsync error: syntax or usage error (code 1) at main.c(866) [receiver=3.0.6]
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(759) [sender=3.0.6]
修改目標節點的配置文件,修改爲可寫,但不可下載。然後再次測試
# sed -i -e 's/read only.*/read only = no/' -e 's/write only.*/write only = yes/' /etc/rsyncd.conf
# rsync -avzr --delete --progress --password-file=/etc/rsync.accesspasswd /data/* rsync://[email protected]/data/
sending incremental file list
apx.txt
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=3/5)
issue
68 100% 0.00kB/s 0:00:00 (xfer#2, to-check=2/5)
test.txt
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=1/5)
ttt/
sent 283 bytes received 69 bytes 33.52 bytes/sec
total size is 68 speedup is 0.19
6、提供後臺運行腳本
提供腳本/usr/local/scripts/inotesync.sh,內容如下
#!/bin/bash
prog=inotesync
dir=/data/
user=tom
romote=192.168.60.144
logfile=/var/log/${prog}.log
{
/usr/local/bin/inotifywait -rmq -e create,attrib,delete,close_write --format "%T %e %w%f" --timefmt "%Y-%m-%d %H:%M:%S" $dir | while read line
do
/usr/bin/rsync -avzr --delete --progress --password-file=/etc/rsync.accesspasswd $dir rsync://$user@$romote$dir
done
} >> $logfile &
# chmod +x /usr/local/scripts/inotesync.sh
# echo "/usr/local/scripts/inotesync.sh" >> /etc/rc.local
重啓測試成功。
腳本說明:
1、在rc.local尾部追加腳本路徑,使其啓動時運行
2、inotifywait、rsync使用絕對路徑,否則腳本將不能正常運行,因爲此時環境變量PATH還沒有起作用
3、爲了排錯或者查看信息,使用一個花括號將語句塊套起來,這樣語句塊內的所有的日誌輸出重定向到日誌文件中
4、使用&符號,讓命令後臺執行
至此,使用inotify+rsync的推模型實驗完成。
四、總結
inotify+rsync的推模型,很好的解決了拉模型中的幾個問題,做到了有變化再做同步。但是使用inotify來監控文件夾,只是在監控的事件發生的時候,觸發了一個事件,而每觸發一個事件,rsync都要遍歷比較所有文件和目錄。當目錄中文件非常多的時候效率也不高。
可以考慮從inotifywait的輸出信息中,提取被創建、改變的文件,推送到目標節點。
參考資料
http://en.wikipedia.org/wiki/Rsync
http://zh.wikipedia.org/zh/Inotify
http://tutorials.jenkov.com/rsync/index.html
http://rsync.samba.org/tech_report/tech_report.html
http://en.wikipedia.org/wiki/Rolling_hash
http://blog.csdn.net/liuaigui/article/details/5793706