rsync features
rsyncis a file transfer program for Unix systems. rsync uses the "rsyncalgorithm" which provides a very fast method for bringing remote filesinto sync. It does this by sending just the differences in the files across thelink, without requiring that both sets of files are present at one of the endsof the link beforehand.
Some features ofrsync include
can update whole directory trees and filesystems
optionally preserves symbolic links, hard links, file ownership, permissions, devices and times
requires no special privileges to install
internal pipelining reduces latency for multiple files
can use rsh, ssh or direct sockets as the transport
supportsanonymous rsyncwhich is ideal for mirroring
基於OpenSSH的模式:
使用基於SSH的服務方式時,服務端無需配置rsync,只需安裝好OpenSSH-server設置並啓動sshd服務即可。這種方式直接以Linux系統用戶進行rsync驗證,非常方便及靈活。
[root@SRV2 ~]#service sshd status
openssh-daemon(pid 3238) is running...
基於rsync的――daemon模式:
1、安裝
[root@SRV1 ~]# yuminstall rsync –y
2、建立rsyncd.conf配置文件
[root@SRV1 ~]# vi/etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
max connections =100
address =192.168.2.109
port 873
log file = /var/log/rsyncd.log
pid file =/var/run/rsyncd.pid
[webroot]
path = /var/www/html
comment = Web Root Directory of192.168.2.109
read only = yes
hosts allow = 192.168.2.104
dont compress = *.gz *.bz2 *.tgz *.zip*.rar *.z
auth users = relearn
secrets file = /etc/rsyncd.secrets
[backup]
path = /backup
comment = MySQL backup Directory of192.168.2.109
read only = yes
hosts allow = 192.168.2.0/255.255.255.0
dont compress = *.gz *.bz2 *.tgz *.zip*.rar *.z
auth users = backup
secrets file = /etc/rsyncd.secrets
[root@SRV1 ~]# vi/etc/rsyncd.secrets
relearn:srv@516
backup:pwd@123
[root@SRV1 ~]#chmod 600 /etc/rsyncd.secrets
3、啓動Rsync服務(兩種方式)
*)獨立啓動的守護進程
[root@SRV1 ~]#rsync --daemon
[root@SRV1 ~]#netstat -anpt | grep rsync
tcp 0 0 192.168.2.109:873 0.0.0.0:* LISTEN 9938/rsync
[root@SRV1 ~]#echo "/usr/bin/rsync --daemon &" >> /etc/rc.local
*)Xinetd管理的客戶進程
[root@SRV1 ~]# vi/etc/xinetd.d/rsync
service rsync
{
disable = yes
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
[root@SRV1 ~]#chkconfig --level 235 rsync on
[root@SRV1 ~]#service xinetd restart
4、使用rsync文件同步工具:
Rsync [選項] 備份源備份目標
***將rsync服務器中webroot備份模塊下的內容同步備份到本地***
(當連接rsync –daemon服務時,使用兩個冒號“:”分隔(或者使用斜槓“/”分隔,同時在主機地址前加“rsync://”前綴)。目錄名對應遠程主機發布的備份模塊名,以遠程主機指定的rsyncd.secrets文件中的用戶進行驗證(允許匿名))
[root@SRV2 ~]# rsync -avz [email protected]::webroot /var/www/html/
Password:
receivingincremental file list
./
anaconda-ks.cfg
install.log
sent 101bytes received 8812 bytes 1371.23 bytes/sec
total size is31856 speedup is 3.57
或者:
[root@SRV2 ~]#rsync -avz rsync://[email protected]/webroot /var/www/html/
***將本地目錄中的內容同步備份到遠程的rsync服務器***
(上傳同步文件時需要用戶對該目錄有可寫權限)
(當連接openssh-server服務時,使用一個冒號“:”分隔。目錄名對應遠程主機的絕對路經,以遠程主機的系統用戶進行驗證。)
[root@SRV2 ~]# rsync -avz /var/www/html/[email protected]:/var/www/html/
[email protected]'spassword:
Error1: (用戶名或密碼認證失敗)
@ERROR: authfailed on module webroot
rsync error: errorstarting client-server protocol (code 5) at main.c(1530) [receiver=3.0.6]
(這個問題是因爲/etc/rsyncd.conf文件內指定的uid、gid,你同步文件要寫的目錄,他的屬主和屬組要是et/rsyncd.conf文件內指定的uid、gid)
sendingincremental file list
./
rsync:failed to set times on "/." (in webroot): Operation not permitted (1)
deletingindex.html
install.log
sent 7982bytes received 30 bytes 1456.73 bytes/sec
total size is30632 speedup is 3.82
rsync error: somefiles/attrs were not transferred (see previous errors) (code 23) atmain.c(1039) [sender=3.0.6]
*查看rsync服務器可用的備份模塊列表:
[root@SRV2 ~]# rsync rsync://192.168.2.109
webroot Web Root Directory of 192.168.2.109
backup MySQL backup Directory of192.168.2.109
*查看rsync服務器中webroot備份模塊下的目錄及文件:
[root@SRV2 ~]# rsync rsync://[email protected]/webroot
Password:
drwxrwxrwx 4096 2013/07/26 03:37:33 .
-rw-r--r-- 1224 2013/07/27 00:52:03anaconda-ks.cfg
-rw-r--r-- 30632 2013/07/27 00:52:03 install.log
-rw-r--r-- 4492 2013/07/26 03:37:33install.log.syslog
Rsync比較常用的命令選項及其含義:
-a使用歸檔(archive)模式,保留文件原有的權限、屬性、屬主等信息,等同於使用“-rlptgoD”等多個選項的組合
-l 符號(軟)連接文件仍然複製爲符號連接
-H保留硬連接文件
-r 遞歸模式,包含目錄及子目錄中所有文件
-v 顯示同步過程的詳細(verbose)信息
-z 在傳輸文件時進行壓縮(compress)
-o 保留文件的屬主標記(僅超級用戶使用)
-g 保留文件的屬組標記(僅超級用戶使用)
-t 保留文件的時間標記
-p 保留文件的權限標記
-D 保留設備文件及其他特殊文件
--delete 刪除目標文件夾有而源文件夾中沒有的文件
--checksum 根據校驗和來決定是否跳過文件(而不是根據文件大小、修改時間)
Rsync+inotify-tools實現數據的實時同步:
實時同步通常適用於有一定實時性要求的場合,如網站鏡像、開發數據備份等。可以結合Linux內核的inotify機制,根據備份源目錄的變化情況,安排觸發更新式的rsync同步任務。
Linux內核自2.6.13版本以後提供了inotify API編程接口,用於監控文件系統事件,如文件存取、刪除、移動、修改等。在RHEL5系統中,默認已經編入了inotify機制(可以檢查是否存在/proc/sys/fs/inotify/目錄).
Inotify-tools是在shell環境中使用inotify功能的一套輔助工具(需要另行下載)。使用inotify-tools提供的inotifywait、inotifywatch工具,編寫合適的shell腳本程序,可以完成許多觸發式的系統管理任務。
通過監控本地源目錄中的文件修改、刪除、新建…… 等變化,可以實時觸發rsync命令,與遠程的目標主機保持同步(基於安全性考慮,上傳同步建議只在內部網絡使用)。
SRV1 | 192.168.2.109 | /var/www/html/ |
SRV2 | 192.168.2.106 | /webdata/ |
1、***安裝inotify工具inotify-tools:
[root@SRV2 ~]# uname -r
2.6.18-164.el5
[root@SRV2 ~]# ll/proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Aug 1 13:59 max_queued_events
-rw-r--r-- 1 root root 0 Aug 1 13:59 max_user_instances
-rw-r--r-- 1 root root 0 Aug 1 13:59 max_user_watches
上面三項的輸出表示系統已經默認支持inotify,接着就可以開始安裝inotify-tools了
[root@SRV2 ~]# wget http://nchc.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
[root@SRV2 ~]# tar zxvfinotify-tools-3.13.tar.gz
[root@SRV2 ~]# cd inotify-tools-3.13
[root@SRV2 inotify-tools-3.13]#./configure && make && make install
[root@SRV2 inotify-tools-3.13]# ls/usr/local/bin/inotifywa*
/usr/local/bin/inotifywait /usr/local/bin/inotifywatch
2、***編寫inotify監控及觸發同步腳本
在腳本中使用inotifywait命令,監控/webdata/目錄下的文件修改、創建、移動、刪除事件,當出現監控的事件時立即觸發rsync命令執行增量更新到192.168.2.109(SRV1)下的/var/www/html/(webroot).
#!/bin/bash
USER=relearn
HOST=192.168.2.109
SRC=/webdata/
DST=webroot
/usr/local/bin/inotifywait -mrq--timefmt '%y/%m/%d %H:%M' --format '%T %w%f%e' -e modify,create,delete,move$SRC | while read files
do
/usr/bin/rsync -avz --delete --password-file=/etc/secrets.txt $SRC$USER@$HOST::$DST
echo "${files}" >> /tmp/rsync.log 2>&1
done
3、測試rsync+inotify實時同步功能
在SRV2中,對/webdata/目錄下進行增加、刪除文件/目錄、修改文件內容等操作,查看SRV1中/var/www/html目錄的相應變化。
[root@SRV2 webdata]# echo "HelloRELEARN" >> index.html
[root@SRV2 webdata]# sendingincremental file list
index.html
sent 105 bytes received 33 bytes 276.00 bytes/sec
total size is 16 speedup is 0.12
[root@SRV1 html]# cat index.html
Hello RELEARN