我主要用rysnc同步檔案,rsync是一款非常優秀的檔案同步管理軟體,它也支援多種作業系統平台,在Unix環境中,rsync有着卓絕的功績。希望這篇文檔能對一些朋友有所幫助。
1. Install
[url]http://www.samba.org/rsync/[/url]
shell> tar zxvf rsync-x.x.x.tar.gz
shell> cd rsync-x.x.x
shell> ./configure && make && make install
目前大部分 Unix/Linux 預設即安裝了 rsync。
2. /etc/rsyncd.conf
shell> touch /etc/rsyncd.conf
shell> vi /etc/rsyncd.conf
Edit /etc/rsyncd.conf as below:
uid = nobody
gid = nobody
use chroot = no
max connections = 5
pid file = /var/run/rsync.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsync.log
[<dst-dir>]
path = <dst-dir-fullpath>
ignore errors
read only = no
list = yes
auth users = username
secrets file = /etc/rsyncd.secrets
3. /etc/rsync.secrets
shell> echo "jack:password" >> /etc/rsyncd.secrets
shell> chmod 600 /etc/rsyncd.secrets
*注:一定要把rsyncd.secrets的權限設為600,否則不能正常進行身份認證。
包括--password-file指向的密碼檔案,也必須設成600權限。
4. Autorun
>>> Idea 1 - only linux <<<
shell> vi /etc/xinetd.d/rsync
set DISABLE to yes, the result looks like this:
service rsync
{
disable = no <---------- change to yes
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
>>> Idea 2 - on openSUSE <<<
shell> echo "/usr/local/bin/rsync --daemon" >> /etc/init.d/boot.local
>>> Idea 3 <<<
shell> echo "/usr/local/bin/rsync --daemon" >> /etc/rc.local
5. Run daemon
shell> rsync --daemon
*注一:在rsync的man手冊的 CONNECTING TO AN RSYNC SERVER 處有提到:
It is also possible to use rsync without a remote shell as the transport. In this case you will connect to a remote rsync server running on TCP port 873.
*注二:在rsync的man手冊的 --port=PORT 選項解釋中有提到:
This specifies an alternate TCP port number to use rather than the default port 873.
6. rsync 指令執行個體
6.1 顯示目錄内容
指令
------
a) rsync <dst-dir>
b) rsync -r <dst-dir>
c) rsync [email protected]::<dst-dir>
d) rsync [email protected]:<dst-dir>
指令說明
---------
a) 顯示<dst-dir>目錄内容(第一層)
b) 遞歸顯示<dst-dir>目錄内容
c) 顯示遠端主機<dst-dir>目錄内容
*注1:端口模式, 基于rsync使用者的身份驗證
*注2:rsync server上的目錄必須具有xx7的權限.
d) 檢視遠端主機<dst-dir>目錄内容
*注1:remote shell模式, 通過ssh連接配接的基于系統本地使用者的身份驗證
*注2:這裡隻使用了一個冒号(:),同時使用者名是遠端主機的ssh使用者,密碼也是ssh使用者對應的密碼。
*注3:使用"<dst-dir>",則列出<dst-dir>檔案夾本身的資訊。若要列出<dst-dir>檔案夾内容,應使用"<dst-dir>/"。
參數說明
-r 對目錄進行遞歸操作
6.2 本地目錄之間同步
a) rsync -av --progress <src-dir>/ <dst-dir> *** 注意(/) ***
b) rsync -av --progress <src-dir> <dst-dir>
c) rsync -avu --progress --delete <src-dir>/ <dst-dir>
d) rsync -av --progress --temp-dir=/tmp <src-dir>/ <dst-dir>
a) 同步src-dir目錄下所有檔案到dst-dir目錄下
b) 同步src-dir目錄下所有檔案到dst-dir/src-dir目錄下
c) 對src-dir目錄内容向dst-dir目錄下進行差異更新,有增加/更新則添加替換,有減少則對其删減
d) 比a)多了--temp-dir=/tmp,即指定/tmp為臨時交換區,這樣可以避免因目标目錄空間不夠引起的無法同步檔案的錯誤。
-a 相當于 -rlptgoD 的集合
-u 等同于 --update,在目标檔案比源檔案新的情況下不更新
-v 顯示同步的檔案
--progress 顯示檔案同步時的百分比進度、傳輸速率
--delete 删除目标目錄中多于源目錄的檔案
6.3 異地主機之間同步
a) rsync -avz --progress <src-dir> [email protected]::<dst-dir>/
b) rsync -avz --progress <src-dir> [email protected]::<dst-dir>/ --password-file=/home/jack/rsync.jack
c) rsync -avuz --progress --delete <src-dir> [email protected]::<dst-dir>/ --password-file=/home/jack/rsync.jack
d) rsync -avz --progress [email protected]::<dst-dir>/<src-dir> <dst-dir>
a) 同步本地<src-dir>目錄的内容到遠端主機192.168.0.1的<dst-dir>目錄下,jack是rsync資料庫使用者(參見3. /etc/rsync.secrets)
b) 通過自動讀取使用者密碼而實作非互動登入檔案同步
c) 較b)多了-u和--delete
d) 同步遠端主機内容到本地目錄
-z 等同于 --compress,對傳輸的檔案壓縮,這對節約網絡帶寬或在網絡資源緊張的情況下非常有用
--password-file 引用192.168.0.1上rsync使用者jack密碼的本地檔案,建立方法如下
shell> echo "jackpwd" >> /home/jack/rsync.jack
shell> chown jack:wheel /home/jack/rsync.jack
shell> chmod 600 /home/jack/rsync.jack
===============================================
2006/07/18 Created by wandering
2008/10/12 重新對文檔整理,修正了部分錯誤,增加一些内容
2008/10/22 增加了 --temp-dir 參數
本文出自 “Wandering's Blog” 部落格,請務必保留此出處http://wandering.blog.51cto.com/467932/105113