rsync是利用 rsync算法同步文件的一種機制。
rsync有四種工作模式:
第一個是shell模式,也稱爲本地模式;
例如:
/etc目錄下的所有文件都會被複制到/test/etc下
注意在shell中,/etc 和 /etc/有所不同, /etc表示 /etc目錄, 而 /etc/隱含等於 /etc/*
所以 cp-a /etc/ /tmp 和 cp-a /etc /tmp 結果是完全不同的,對於rsync同樣需要注意這一點。
第二個是遠程shell模式,其利用SSH執行底層連接和傳輸;
echo"password" | rsync -azv -e 'ssh -p 1998' /rsync/httpd/ vps:/etc/httpd/
當然如果設置了使用密鑰認證,就不需要密碼了,我的因爲SELinux導致ssh必須使用密碼才能登陸,後來才發現,實驗環境下就無所謂啦
第三個是列表模式,其工作方式與ls相似,即列出源的內容;-nv
例如
可以指定 -v-r 等參數
第四個模式是服務器模式。rsync以守護進程方式運行,接收文件傳輸請求。在使用時,可以使用rsync命令把文件發送給守護進程,也可以向它請求文件。服務器模式非常適合創建中心備份服務器或項目存儲庫。
遠程shell模式和服務器模式的差異在於,後者在源和目標名中使用兩個冒號(:)
rsync-rl /www/htdocs HOST:/www
rsync-rl /www/htdocs HOST::htdocs
rsync命令常用選項
--daemon以 daemon模式啓動
daemon模式的其它選項見 manpage DAEMON OPTIONS 段
-v,--verbose 詳細模式輸出
-q,--quiet 精簡輸出模式
-c,--checksum 打開校驗開關,強制對文件傳輸進行校驗
-a,--archive 歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性,等於-rlptgoD
--delete delete extraneous files from dest dirs,從目標目錄中刪除源目錄(文件)中沒有的
-r,--recursive 對子目錄以遞歸模式處理
-l,--links 保留符號鏈結
--list-onlylist the files instead of copying them(好像列表模式不需要指定這個選項)
-n,--dry-run 測試運行,不進行任何修改
-p,--perms 保留文件權限
-t,--times 保留文件時間戳
-g,--group 保留文件屬組信息
-o,--owner 保留文件屬主信息
-D,--devices 保留設備文件即特殊文件信息
-exclude=PATTERN exclude files matching PATTERN
--exclude-from=FILE read exclude patterns from FILE
--include=PATTERN don’t exclude files matching PATTERN
--include-from=FILE read include patterns from FILE
--files-from=FILE read list of source-file names from FILE
-e,--rsh=COMMAND 指定替代rsh的shell程序
-z,--compress 對備份的文件在傳輸時進行壓縮處理
--password-file=filename在連接遠程rsync服務器時指定密碼文件所在
還可以使用兩個選項監視傳輸的狀態:--progress和--stats。
--progress顯示進度條
--stats顯示如何執行壓縮和傳輸
rsync服務器模式配置
rsync的安裝非常簡單,而且系統一般有自帶。
rsync的配置文件是 /etc/rsyncd.conf需要手工創建。
配置文件範例
#Section1: Global settings
port= 873
address= 173.254.227.40
#當rsyncd由xinetd運行時,port和address被忽略
uid= nobody#傳輸文件時,rsync進程使用的用戶身份
gid= nobody
usechroot = no
maxconnections = 3
timeout= 300
strictmodes = yes
#是否檢查口令文件的權限。如果設置爲yes,密碼文件的權限必須只有root可讀,其它用戶無權限
pidfile = /var/run/rsyncd.pid
#lockfile=/tmp/rsyncd.lock
logfile = /var/log/rsyncd.log
#Section 2:Directoryto be synced
[htdocs]
path= /web/htdocs
comment= backup the web document
ignoreerrors = yes
readonly = no
#hostsallow = 172.16.0.0/16
#hostsdeny = *
list= no
uid= root
gid= root
authusers = www#客戶端連接使用的賬號,類似於samba的賬號,跟系統賬號無關
secretsfile = /etc/.rsync.passwd
[htconfig]
path= /etc/httpd
ignoreerrors = yes
readonly = no
list= yes
uid= root
gid=root
authusers = www
secretsfile = /etc/.rsync.passwd
配置分爲全局段和模塊段,全局段指定全局的和缺省的設置。
密碼文件格式
用戶名:密碼
echo"www:q3zbZ" > /etc/.rsync.passwd
chmod600 /etc/.rsync.passwd
客戶端設置
echo"q3zbZ" > /etc/.rsync.pass
chmod600 /etc/.rsync.pass
使用如下命令即可同步遠程服務器上的數據
rsync-avz --progress --password-file=/etc/.rsync.pass www@vps::htdocs /backup/htdocs
rsyncd可以獨立運行,也可以掛在 xinetd下運行,後者需要修改/etc/xinetd.d/rsync文件,設置 disable= no
注意防火牆放行相應端口的數據通過
Rsync+ inotify 合作備份數據
單靠 rsync很難實現實時同步數據,而且rsync雖然是比較源、目標文件的差異之後才只針對差異的文件進行必要的同步,這樣雖然較之簡單的複製大大提高的效率。但如果需要掃描的文件過多的話,也會產生大量的磁盤I/O。如果有一種機制,實時監控需要同步的文件,當文件發生改變時,就通知rsync進行數據同步,豈不美哉!
Inotify正是提供這一機制的。
inotify是Linux內核2.6.13版本之後新增的一個子系統(API),通過inotify可以實時監控文件系統中添加、刪除、修改、移動等各種細微事件。
inotify可以監視的文件系統常見事件包括:
IN_ACCESS:文件被訪問
IN_MODIFY:文件被修改
IN_ATTRIB,文件屬性被修改
IN_CLOSE_WRITE,以可寫方式打開的文件被關閉
IN_CLOSE_NOWRITE,以不可寫方式打開的文件被關閉
IN_OPEN,文件被打開
IN_MOVED_FROM,文件被移出監控的目錄
IN_MOVED_TO,文件被移入監控着的目錄
IN_CREATE,在監控的目錄中新建文件或子目錄
IN_DELETE,文件或目錄被刪除
IN_DELETE_SELF,自刪除,即一個可執行文件在執行時刪除自己
IN_MOVE_SELF,自移動,即一個可執行文件在執行時移動自己
通過/proc接口中的如下參數設定inotify能夠使用的內存大小:
1、/proc/sys/fs/inotify/max_queue_events
應用程序調用inotify時需要初始化inotify實例,並時會爲其設定一個事件隊列,此文件中的值則是用於設定此隊列長度的上限;超出此上限的事件將會被丟棄;
2、/proc/sys/fs/inotify/max_user_instances
此文件中的數值用於設定每個用戶ID(以ID標識的用戶)可以創建的inotify實例數目的上限;
3、/proc/sys/fs/inotify/max_user_watches
此文件中的數值用於設定每個用戶ID可以監控的文件或目錄數目上限;
inotify-tools則是利用這一API開發的套件,它包括一個C庫和幾個命令行工具,這些命令行工具可用於通過命令行或腳本對某文件系統的事件進行監控。inotify-tools提供了兩個重要工具
inotifywait和 inotifyswatch
inotifywait的使用簡介:
inotifywait [-hcmrq] [-e <event> ] [-t <seconds> ] [--format <fmt>] [--timefmt <fmt> ] <file> [ ...
]
常用選項:
-m,--monitor:inotifywait的默認動作是在監控至指定文件的特定事件發生一次後就退出了,而使用此選項則可實現持續性的監控;
-r,--recursive:遞歸監控指定目錄下的所有文件,包括新建的文件或子目錄;如果要監控的目錄中文件數量巨大,則通常需要修改/proc/sys/fs/inotify/max_users_watchs內核參數,因爲其默認值爲8192。
-e <event>, --event <event>
Listenfor specific event(s) only. The events which can be listened for are listed in the
EVENTSsection. This option can be specified more than once. If omitted,all events are lis-
tenedfor.
此處<event>包括access,modify, attrib, close_write, close_nowirte, close, open, moved_to,moved_from, move, create, delete, delete_selt等;
--exclude<pattern>
Do not process any events whose filename matches the specified POSIXextended regular expres-
sion,case sensitive.
--excludei<pattern>
Donot process any events whose filename matches the specified POSIXextended regular expres-
sion,case insensitive.
-t<seconds>, --timeout <seconds>
Exit if an appropriate event has not occurred within <seconds>seconds. If <seconds> is zero
(thedefault), wait indefinitely for an event.
--timefmt<fmt>:當在--format選項中使用%T時,--timefrt選項則可以用來指定自定義的符合strftime規範的時間格式,此時間格式可用的格式符可以通過strftime的手冊頁獲取;--timefrt後常用的參數是'%d/%m/%y%H:%M';
--format<fmt>:自定義inotifywait的輸出格式,如--format'%T %w %f';常用的格式符如下:
%w This will be replaced with the name of the Watched file on whichan event occurred.
%f When an event occurs within a directory, this will be replacedwith the name of the File which
causedthe event to occur. Otherwise, this will be replaced with an emptystring.
如果被監控的目錄發生事件,用引起事件的文件名來替換%f.
%e Replaced with the Event(s) which occurred, comma-separated.
%Xe Replaced with the Event(s) which occurred, separated by whichevercharacter is in the place of
‘X’.
%T Replaced with the current Time in the format specified by the--timefmt option, which should
bea format string suitable for passing to strftime(3).
使用範例如下:
編寫腳本監控本地需要同步的目錄,當目錄發生特定改動時,執行rsync連接遠程服務器上的 rsyncd守護進程,然後針對改變的文件進行數據同步。腳本範例如下:
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin RHOST=172.16.9.1 DISTINATION=htdocs SRCDIR=/web/htdocs/ AUTH_USER=www PASSWDFILE=/etc/.rsyncpass LOGFILE=/tmp/rsync.log inotifywait--monitor --recursive --eventsclose_write,move_from,move_to,delete,attrib --timefmt '%d/%m/%y%H:%M' --format '%T %w %f ' \ $SRCDIR| while read DATE TIME DIR CHANGED_FILE do rsyncaqz --delete --password-file="$PASSWDFILE" $AUTH_USER@RHOST::$DISTINATION &> /dev/null echo-e "At $DATE $TIME,\t${DIR}$CHANGED_FILE was backuped via rsync" done> $LOGFILE
其它:
經過我的測試,覺得 create事件發生時不宜進行同步,使用 close_write比較好
move A file or directory was moved from or to a watched directory. Note that this is actuallyimplemented simply by listening for both moved_to and moved_from, hence all close events eceived will beoutput as one or both of these, not MOVE.即監控move事件 包括move_to和 move_from,但輸出時不會顯示 move,只會顯示 move_to和 move_from
上面的腳本有參考書上和老師的,不過我沒有做測試執行。