Since rsync does not provide any security while transferring data it is recommended that you use rsync over ssh . This allows a secure remote connection. Now let us see some examples of rsync.
rsync command common optionsCopy file from /www/backup.tar.gz to a remote server called openbsd.nixcraft.in
$ rsync -v -e ssh /www/backup.tar.gz jerry@openbsd.nixcraft.in:~
Output:
Password: sent 19099 bytes received 36 bytes 1093.43 bytes/sec total size is 19014 speedup is 0.99Please note that symbol ~ indicate the users home directory (/home/jerry).
Task : Copy file from a remote server to a local computerCopy file /home/jerry/webroot.txt from a remote server openbsd.nixcraft.in to a local computer /tmp directory:
$ rsync -v -e ssh jerry@openbsd.nixcraft.in:~/webroot.txt /tmp
Password
Task: Synchronize a local directory with a remote directory$ rsync -r -a -v -e "ssh -l jerry" --delete openbsd.nixcraft.in:/webroot/ /local/webroot
Task: Synchronize a remote directory with a local directory$ rsync -r -a -v -e "ssh -l jerry" --delete /local/webroot openbsd.nixcraft.in:/webroot
Task: Synchronize a local directory with a remote rsync server$ rsync -r -a -v --delete rsync://rsync.nixcraft.in/cvs /home/cvs
Task: Mirror a directory between my "old" and "new" web server/ftpYou can mirror a directory between my "old" (my.old.server.com) and "new" web server with the command (assuming that ssh keys are set for password less authentication)
$ rsync -zavrR --delete --links --rsh="ssh -l vivek" my.old.server.com:/home/lighttpd /home/lighttpd
===================================================
當需要把服務器上的文件復制到另外的機器上,可用rsync來同步文件。
一、服務器端配置:
# yum -y install xinetd
# 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
}
然后啟動 xinetd
復制代碼 代碼如下:
# /etc/init.d/xinetd start
復制代碼 代碼如下:
# telnet 127.0.0.1 873
# vi /etc/rsyncd.conf
復制代碼 代碼如下:
[backup]
path = /www
auth users = admin
uid = root
gid = root
secrets file = /etc/rsyncd.secrets
read only = no
# vi /etc/rsyncd.secrets
復制代碼 代碼如下:
admin:1234 #用戶名:密碼
二、client 端進行同步
客戶端默認好像已經(jīng)裝了rsync,沒有的話裝下:
# yum -y install rsync
執(zhí)行異步同步操作:
/usr/bin/rsync -avz –progress admin@192.168.1.105::backup /www
同步命令說明:
1 顯示目錄內(nèi)容
命令
——
a) rsync
b) rsync -r
c) rsync jack@192.168.0.1::
d) rsync ssh_user@192.168.0.1:
命令說明
———
a) 顯示目錄內(nèi)容(第一層)
b) 遞歸顯示目錄內(nèi)容
c) 顯示遠程主機目錄內(nèi)容
*注1:端口模式, 基于rsync用戶的身份驗證
*注2:rsync server上的目錄必須具有xx7的權限.
d) 查看遠程主機目錄內(nèi)容
*注1:remote shell模式, 通過ssh連接的基于系統(tǒng)本地用戶的身份驗證
*注2:這里只使用了一個冒號(:),同時用戶名是遠程主機的ssh 用戶,密碼也是ssh用戶對應的密碼。
*注3:使用””,則列出文件夾本身的信息。若要列出文件夾內(nèi)容,應使用”/”。
參數(shù)說明
———
-r 對目錄進行遞歸操作
2 本地目錄之間同步
命令
——
a) rsync -av –progress / *** 注意(/) ***
b) rsync -av –progress
c) rsync -avu –progress –delete /
d) rsync -av –progress –temp-dir=/tmp /
命令說明
———
a) 同步src-dir目錄下所有文件到dst-dir目錄下
b) 同步src-dir目錄下所有文件到dst-dir/src-dir目錄下
c) 對src-dir目錄內(nèi)容向dst-dir目錄下進行差異更新,有增加/更新則添加替換,有減少則對其刪減
d) 比a)多了–temp-dir=/tmp,即指定/tmp為臨時交換區(qū),這樣可以避免因目標目錄空間不夠引起的無法同步文件的錯誤。
參數(shù)說明
———
-a 相當于 -rlptgoD 的集合
-u 等同于 –update,在目標文件比源文件新的情況下不更新
-v 顯示同步的文件
–progress 顯示文件同步時的百分比進度、傳輸速率
–delete 刪除目標目錄中多于源目錄的文件
3 異地主機之間同步
命令
——
a) rsync -avz –progress jack@192.168.0.1::/
b) rsync -avz –progress jack@192.168.0.1::/ –password-file=/home/jack/rsync.jack
c) rsync -avuz –progress –delete jack@192.168.0.1::/ –password-file=/home/jack/rsync.jack
d) rsync -avz –progress jack@192.168.0.1::/
命令說明
———
a) 同步本地目錄的內(nèi)容到遠程主機192.168.0.1的目錄下,jack是rsync數(shù)據(jù)庫用戶(參見3. /etc/rsync.secrets)
b) 通過自動讀取用戶密碼而實現(xiàn)非交互登錄文件同步
c) 較b)多了-u和–delete
d) 同步遠程主機內(nèi)容到本地目錄
新聞熱點
疑難解答
圖片精選