環境
centos 5.8(64) 192.168.23.130 (service)
centos 5.8(64) 192.168.23.131 (client)
軟件
rsync-3.1.1.tar.gz(http://rsync.samba.org/)
安裝步驟
1.前期的準備條件
a.關閉selinux、防火牆(個人習慣不管什麼情況下,自己玩都是必須關閉)
b.yum install -y make gcc
2.安裝rsync(service、client)
a.tar -zxf rsync-3.1.1.tar.gz
b.cd rsync-3.1.1
c. ./configure --prefix=/usr/local/rsync
d.make
e.make install
3.配置rsync
a.vim /etc/rsyncd.conf(service)
uid = root
gid = root
use chroot = no
[apps]
auth users = root
secrets file = /etc/rsyncd.passwd
munge symlinks = no
read only = no
path=/data/test
b.vim /etc/rsyncd.passwd(service)
root:111111
c.vim /etc/rsync.passwd(client)
111111
4.啓動rsync(service)
/usr/local/rsync/bin/rsync --daemon --config=/etc/rsyncd.conf
5.同步文件(client)
/usr/local/rsync/bin/rsync -vzrtopg --progress --delete rsync://[email protected]/apps /tmp/tmp/ --password-file=/etc/rsync.pas (service->client)
/usr/local/rsync/bin/rsync -vzrtopg --progress --delete /tmp/tmp/ rsync://[email protected]/apps --password-file=/etc/rsync.pas (client->service)
6.驗證
到相應的目錄查看是否存在同步的文件
以後是手動運行的,對於比較懶的人肯定不適合,下邊就介紹Inotify-tools工具,可以實現實時自動同步。Inotify 是一個 Linux特性,它監控文件系統操作,比如讀取、寫入和創建。Inotify 反應靈敏,用法非常簡單,並且比 cron 任務的繁忙輪詢高效得多。學習如何將 inotify 集成到您的應用程序中,並發現一組可用來進一步自動化系統治理的命令行工具。
7.安裝Inotify-tools
a.wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
b.tar -zxf inotify-tools-3.14.tar.gz
c.cd inotify-tools-3.14
d. ./configure --prefix=/usr/local/inotify
e.make
f.make install
8.編寫shell腳本(rsync.sh)
#!/bin/sh
#local dir
dstdir="/tmp/tmp/"
#sync user
rsyncuser="root"
#sync password
rsyncpassword="/etc/rsync.pas"
#remote ip
remoteip="192.168.23.131"
#remote module
module="apps"
#sync remote server module to local dir
for ip in $remoteip
do
/usr/local/rsync/bin/rsync -vzrtopg --progress --delete $rsyncuser@$ip::$module $dstdir --password-file=$rsyncpassword
done
#monitor local dir, then rsync remote server module
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $dstdir | while read file
do
for ip in $remoteip
do
/usr/local/rsync/bin/rsync -vzrtopg --progress --delete $dstdir $rsyncuser@$ip::$module --password-file=$rsyncpassword
echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
done
done
9.chmod 755 rsync.sh
10.運行shell腳本,併到相應的目錄操作看是否操作成功
11.設置開機自啓動
echo "sh /usr/local/inotify/rsync.sh &" >> /etc/rc.d/rc.local
發散一下:如果線上有多臺web服務器,我們可以通過rsync+Inotify實現批量上傳文件。其具體做法是:確定一臺server作爲內容分發機,所有線上的服務器都通過rsync+Inotify的方式監控內容分發機,一旦有更新就實時的同步到線上,這也算一個小小的自動化運維吧。
1.rsyncd.conf詳解
全局參數
uid = root //運行RSYNC守護進程的用戶
gid = root //運行RSYNC守護進程的組
use chroot = no //不使用chroot
max connections = 4 // 最大連接數爲4
strict modes =yes //是否檢查口令文件的權限
port = 873 //默認端口873
模塊參數
[backup] //這裏是認證的模塊名,在client端需要指定
path = /home/backup/ //需要做鏡像的目錄,不可缺少!
comment = This is a test //這個模塊的註釋信息
ignore errors //可以忽略一些無關的IO錯誤
read only = yes // 只讀
list = no //不允許列文件
auth users = hening //認證的用戶名,如果沒有這行則表明是匿名,此用戶與系統無關
secrets file = /etc/rsync.pas //密碼和用戶名對比表,密碼文件自己生成
hosts allow = 192.168.1.1,10.10.10.10 //允許主機
hosts deny = 0.0.0.0/0 //禁止主機
#transfer logging = yes
註釋:下面這些綠色文件是安裝完RSYNC服務後自動生成的文件
pid file = /var/run/rsyncd.pid //pid文件的存放位置
lock file = /var/run/rsync.lock //鎖文件的存放位置
log file = /var/log/rsyncd.log //日誌記錄文件的存放位置
2.rsync命令參數
-v, --verbose 詳細模式輸出
-q, --quiet 精簡輸出模式
-c, --checksum 打開校驗開關,強制對文件傳輸進行校驗
-a, --archive 歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性,等於-rlptgoD
-r, --recursive 對子目錄以遞歸模式處理
-R, --relative 使用相對路徑信息
-b, --backup 創建備份,也就是對於目的已經存在有同樣的文件名時,將老的文件重新命名爲~filename。可以使用--suffix選項來指定不同的備份文件前綴。
--backup-dir 將備份文件(如~filename)存放在在目錄下。
-suffix=SUFFIX 定義備份文件前綴
-u, --update 僅僅進行更新,也就是跳過所有已經存在於DST,並且文件時間晚於要備份的文件。(不覆蓋更新的文件)
-l, --links 保留軟鏈結
-L, --copy-links 想對待常規文件一樣處理軟鏈結
--copy-unsafe-links 僅僅拷貝指向SRC路徑目錄樹以外的鏈結
--safe-links 忽略指向SRC路徑目錄樹以外的鏈結
-H, --hard-links 保留硬鏈結
-p, --perms 保持文件權限
-o, --owner 保持文件屬主信息
-g, --group 保持文件屬組信息
-D, --devices 保持設備文件信息
-t, --times 保持文件時間信息
-S, --sparse 對稀疏文件進行特殊處理以節省DST的空間
-n, --dry-run現實哪些文件將被傳輸
-W, --whole-file 拷貝文件,不進行增量檢測
-x, --one-file-system 不要跨越文件系統邊界
-B, --block-size=SIZE 檢驗算法使用的塊尺寸,默認是700字節
-e, --rsh=COMMAND 指定使用rsh、ssh方式進行數據同步
--rsync-path=PATH 指定遠程服務器上的rsync命令所在路徑信息
-C, --cvs-exclude 使用和CVS一樣的方法自動忽略文件,用來排除那些不希望傳輸的文件
--existing 僅僅更新那些已經存在於DST的文件,而不備份那些新創建的文件
--delete 刪除那些DST中SRC沒有的文件
--delete-excluded 同樣刪除接收端那些被該選項指定排除的文件
--delete-after 傳輸結束以後再刪除
--ignore-errors 及時出現IO錯誤也進行刪除
--max-delete=NUM 最多刪除NUM個文件
--partial 保留那些因故沒有完全傳輸的文件,以是加快隨後的再次傳輸
--force 強制刪除目錄,即使不爲空
--numeric-ids 不將數字的用戶和組ID匹配爲用戶名和組名
--timeout=TIME IP超時時間,單位爲秒
-I, --ignore-times 不跳過那些有同樣的時間和長度的文件
--size-only 當決定是否要備份文件時,僅僅察看文件大小而不考慮文件時間
--modify-window=NUM 決定文件是否時間相同時使用的時間戳窗口,默認爲0
-T --temp-dir=DIR 在DIR中創建臨時文件
--compare-dest=DIR 同樣比較DIR中的文件來決定是否需要備份
-P 等同於 --partial
--progress 顯示備份過程
-z, --compress 對備份的文件在傳輸時進行壓縮處理
--exclude=PATTERN 指定排除不需要傳輸的文件模式
--include=PATTERN 指定不排除而需要傳輸的文件模式
--exclude-from=FILE 排除FILE中指定模式的文件
--include-from=FILE 不排除FILE指定模式匹配的文件
--version 打印版本信息
--address 綁定到特定的地址
--config=FILE 指定其他的配置文件,不使用默認的rsyncd.conf文件
--port=PORT 指定其他的rsync服務端口
--blocking-io 對遠程shell使用阻塞IO
-stats 給出某些文件的傳輸狀態
--progress 在傳輸時現實傳輸過程
--log-format=formAT 指定日誌文件格式
--password-file=FILE 從FILE中得到密碼 權限600
--bwlimit=KBPS 限制I/O帶寬,KBytes per second
inotifywait參數
-m 是保持一直監聽
-r 是遞歸查看目錄
-q 是打印出事件
-e create,move,delete,modify,attrib 是指 “監聽 創建 移動 刪除 寫入 權限” 事件