rsync是Linux/unix下一個用於遠程文件(目錄)同步的一個精巧的小工具程序,有不少文章討論了其功能和實現原理,本文主要就不贅述了。html
主要介紹下實踐時使用的一些方法和細枝末節留做工做筆記以便往後參考。bash
大部分的Linux發佈包中基本都默認包含了rsync這個小工具,這裏就不介紹其安裝了。服務器
使用場景併發
用rsync同步程序部署包到單個或多個測試機上。工具
模式測試
rsync支持6種工做模式,整體劃分爲pull和push兩種。具體參看官方文檔 rsync man page,本文的場景適合於push模式。ui
過程unix
下面以 rsync daemon方式的push模式作說明,其命令模式以下code
Push: rsync [OPTION...] SRC... [USER@]HOST::DESTserver
本地機做爲client端,遠程服務器做爲server端,文件從本地push到遠程服務器指定目錄下。首先需在遠程服務器用daemon模式啓動rsync,啓動前需對rsync的配置文件進行配置。
配置文件默認爲 /etc/rsyncd.conf,若以前沒有使用過,/etc目錄下默認沒有該文件,需新建該文件,以下所示:
配置文件是由一個或多個模塊結構組成。一個模塊定義以方括弧中的模塊名開始,直到下一個模塊定義開始或者文件結束,模塊中包含格式爲name = value的參數定義
# 全局參數定義 uid = root gid = root use chroot = no max connections = 1 #指定該模塊的最大併發鏈接數量以保護服務器,超過限制的鏈接請求將被告知隨後再試。默認值是0,也就是沒有限制。 pid file = /tmp/rsyncd.pid lock file = /tmp/rsyncd.lock log file = /tmp/rsyncd.log # tmp 模塊 [tmp] path = /tmp/rsync read only = no #不能只讀,由於要向服務器傳輸文件,必須可寫 list = yes hosts allow = 192.168.1.5 #只容許這個ip訪問 hosts deny = 0.0.0.0/32 auth users = root #受權用戶 secrets file = /etc/rsyncd.pas #受權密碼 格式:root:123456 可如今生成該文件:echo "root:123456" > /etc/rsyncd.pas; chmod 600 /etc/rsyncd.pas(只有全部者能夠讀寫) # vdisk 模塊 [vdisk] path = /backup/vdisk read only = no list = yes hosts allow = 192.168.1.5 hosts deny = 0.0.0.0/32 auth users = root secrets file = /etc/rsyncd.pas
配置完成後,按以下命令啓動rsync daemon服務
rsync --daemon 或 rsync --daemon --config=/etc/rsyncd.conf
若要中止服務,執行以下命令
cat /tmp/rsyncd.pid | xargs kill -9 && rm -rf /tmp/rsyncd.pid
完成了服務端配置,並啓動服務後在client端執行以下命令進行文件同步:
rsync -varz --delete --exclude ".*" --progress --password-file=/etc/rsyncd.pas /tmp root@192.168.1.5::tmp
以上命令是將本地/tmp目錄同步到遠程服務器的tmp模塊指定的目錄,也就是服務端配置文件中path的位置,如上path = /tmp/rsync
具體參數選項參考man page,這裏須要提一點的是client端的密碼文件只須要密碼,不須要用戶名不然會報以下錯誤:
@ERROR: auth failed on module testlink rsync error: error starting client-server protocol (code 5) at main.c(1527) [receiver=3.0.6]
生成客戶端密碼文件可用以下腳本命令
echo "123456" > /etc/rsyncd.pas; chmod 600 /etc/rsyncd.pas