Rsync經常使用選項:git
-v, --verbose 詳細模式輸出
github
-q, --quiet 精簡輸出模式
web
-c, --checksum 打開校驗開關,強制對文件傳輸進行校驗
shell
-a, --archive 歸檔模式,表示以遞歸方式傳輸文件,並保持全部文件屬性,等於-rlptgoD
vim
-r, --recursive 對子目錄以遞歸模式處理
bash
-l, --links 保留符號鏈結
服務器
-p, --perms 保留文件權限
ide
-t, --times 保留文件時間戳
測試
-g, --group 保留文件屬組信息
ui
-o, --owner 保留文件屬主信息
-D, --devices 保留設備文件即特殊文件信息
-e, --rsh=COMMAND 指定替代rsh的shell程序
-z, --compress 對備份的文件在傳輸時進行壓縮處理
目標主機配置
# yum -y install rsync xinetd # vim /etc/rsyncd.conf # Section 1: Global settings uid = nobody gid = nobody use chroot = no max connections = 3 strict modes = yes pid file = /var/run/rsyncd.pid log file = /var/log/rsyncd.log # Section 2:Directory to be synced [htdocs] path = /www/htdocs //同步過來的數據保存的位置,同步的時候有兩種寫法,後邊會說到。 ignore errors = yes read only = no write only = no hosts allow = 172.16.0.0/16 //容許訪問的主機範圍 hosts deny = * list = false uid = root gid = root auth users = testuser //遠程同步的時候指定的客戶端用戶 secrets file = /etc/rsync.passwd //加密訪問用到的用到的認證密碼文件,目標主機和源主機都得配置
# vim /etc/rsync.passwd testuser:P@ssw0rd # chkconfig rsync on # chkconfig xinetd on # service xinetd start
配置源
# yum -y install rsync xinetd # vim /etc/rsync.passwd P@ssw0rd
注意:這裏密碼文件必定不要寫爲testuser:P@ssw0rd,只寫密碼就行,不然,同步會報錯,即,服務器端和客戶端的此文件寫法不一樣。
# chmod 600 /etc/rsync.passwd # chkconfig rsync on # chkconfig xinetd on # service xinetd start
同步方式:
rsync有四種工做模式:
第一個是shell模式,也稱爲本地模式;
第二個是遠程shell模式,其利用SSH執行底層鏈接和傳輸;
第三個是列表模式,其工做方式與ls類似,即列出源的內容;-nv
第四個模式是服務器模式。rsync以守護進程方式運行,接收文件傳輸請求。在使用時,能夠使用rsync命令把文件發送給守護進程,也能夠向它請求文件。服務器模式很是適合建立中心備份服務器或項目存儲庫。
shell模式:
# rsync -lr /tmp/rsync 172.16.5.11:/www/ # rsync -lr /tmp/rsync root@172.16.5.11:/www/
服務器模式
# rsync -avz --password-file=/etc/rsync.passwd /tmp/rsync testuser@172.16.5.11::htdocs
注意手動同步的時候,shell模式可能更簡單一些,但如果用rsync+inotify來同步,服務器模式就方便多了。
利用inotify自動同步
1、安裝inotify-tools
軟件下載地址:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
# tar xf inotify-tools-3.14.tar.gz # cd inotify-tools-3.14 # ./configure # make # make install # echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf # ldconfig
2、建立測試目錄
# mkdir -pv /www/htdocs
服務器端建立腳本
# vim /root/bin/htdocsync.sh #!/bin/bash # DESTHOST=172.16.3.4 DESTHOSTDIR=/www/htdocs/ SRCDIR=/www/htdocs/ inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e close_write,modify,delete,create,attrib $SRCDIR | while read DATE TIME DIR FILE; do FILECHANGE=${DIR}${FILE} rsync -avz --password-file=/etc/rsync.passwd $SRCDIR htdocsuser@${DESTHOST}::htdocs &>/dev/null && \ echo "At ${TIME} on ${DATE}, file $FILECHANGE was backed up via rsync" >> /var/log/websync.log done # chmod +x /root/bin/htdocsync.sh # /root/bin/htdocsync.sh