日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

rsync推拉模型及结合inotify实现推模型自动同步

發布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rsync推拉模型及结合inotify实现推模型自动同步 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

一、前言

無論使用什么操作系統下,都經常有同步文件的需求,不管發生在本地,還是發生在本地和遠程主機之間。那么應該怎么做呢?

使用拷貝類的命令,本地使用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://tom@192.168.60.144/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://tom@192.168.60.144/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://tom@192.168.60.144/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

http://www.ibm.com/developerworks/cn/linux/l-inotify/

http://www.cnblogs.com/jingzhishen/p/3738637.html

轉載于:https://blog.51cto.com/me2xp/1549624

總結

以上是生活随笔為你收集整理的rsync推拉模型及结合inotify实现推模型自动同步的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。