十八、Rsync 遠程同步數據

在linux系統下數據備份的工具。Rsync不僅可以遠程同步數據(類似於scp [1]),當然還可以本地同步數據(類似於cp),但不同於cp或scp的一點是,rsync不像cp/scp一樣會覆蓋以前的數據(如果數據已經存在),它會先判斷已經存在的數據和新數據有什麼不同,只有不同時纔會把不同的部分覆蓋掉。如果你的linux沒有rsync命令請使用 yum install -y rsync 安裝。

下面阿銘先舉一個例子,然後再詳細講解rsync的用法:

[root@localhost ~]# rsync -av 123.txt /tmp/ 

sending incremental file list 123.txt sent 71 bytes  received 31 bytes  204.00 bytes/sec total size is 0  speedup is 0.00

上面例子表示把當前目錄下的123.txt同步到/tmp/目錄下,也可以更改目標文件的名字,

 rsync -av 123.txt/tmp/234.txt 

如果是遠程拷貝的話就是這樣的形式了: IP:path (如:10.0.2.34:/root/)

[root@localhost ~]# rsync -av 123.txt 192.168.0.101:/data/The authenticity of host '192.168.0.101 (192.168.0.101)' can't be established.RSA key fingerprint is b4:54:5f:73:ec:c2:60:5f:c3:79:c0:f9:51:e9:ac:e5.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.0.101' (RSA) to the list of known [email protected]'s password:

首次連接會提示是否要繼續連接,我們輸入yes繼續,當建立連接後,需要輸入密碼。如果手動去執行這些操作還好,但若是寫在腳本中怎麼辦?這就涉及到添加信任關係了,該部分內容稍後會詳細介紹。

  1. rsync的命令格式

rsync [OPTION]... SRC DEST

rsync [OPTION]... SRC [USER@]HOST:DEST

rsync [OPTION]... [USER@]HOST:SRC DEST

rsync [OPTION]... [USER@]HOST::SRC DEST

rsync [OPTION]... SRC [USER@]HOST::DEST

在一開始舉的兩個例子,第一個例子即爲第一種格式,第二個例子即爲第二種格式,但不同的是,並沒有加user@host 如果不加默認指的是root. 第三種格式是從遠程目錄同步數據到本地。第四種以及第五種格式使用了兩個冒號,這種方式和前面的方式的不同在於驗證方式不同,稍後詳細介紹。

2. rsync常用選項

-a 歸檔模式,表示以遞歸方式傳輸文件,並保持所有屬性,等同於-rlptgoD, -a選項後面可以跟一個 --no-OPTION 這個表示關閉-rlptgoD中的某一個例如 -a--no-l 等同於-rptgoD

-r 對子目錄以遞歸模式處理,主要是針對目錄來說的,如果單獨傳一個文件不需要加-r,但是傳輸的是目錄必須加-r選項

-v 打印一些信息出來,比如速率,文件數量等

-l 保留軟鏈結

-L 向對待常規文件一樣處理軟鏈結,如果是SRC中有軟連接文件,則加上該選項後將會把軟連接指向的目標文件拷貝到DST

-p 保持文件權限

-o 保持文件屬主信息

-g 保持文件屬組信息

-D 保持設備文件信息

-t 保持文件時間信息

--delete 刪除那些DST中SRC沒有的文件

--exclude=PATTERN 指定排除不需要傳輸的文件,等號後面跟文件名,可以是萬用字符模式(如.txt)

--progress 在同步的過程中可以看到同步的過程狀態,比如統計要同步的文件數量、同步的文件傳輸速度等等

-u 加上這個選項後將會把DST中比SRC還新的文件排除掉,不會覆蓋

常用的選項: (-a -v --delete --exclude)

實驗:

1) 建立目錄以及文件:

[root@localhost ~]# mkdir rsync

[root@localhost ~]# cd rsync

[root@localhost rsync]# mkdir test1

[root@localhost rsync]# cd test1

[root@localhost test1]# touch 1 2 3

[root@localhost test1]# ln -s /root/123.txt ./123.txt

[root@localhost test1]# ls -l

總用量 0

-rw-r--r-- 1 root root  0  6月 10 12:58 1l

rwxrwxrwx 1 root root 13  6月 10 12:59 123.txt -> /root/123.txt

-rw-r--r-- 1 root root  0  6月 10 12:58 2

-rw-r--r-- 1 root root  0  6月 10 12:58 3

[root@localhost test1]# cd ..

建立這些文件的目的就是爲做試驗做一些準備工作。

2)使用 -a 選項

[root@localhost rsync]# rsync -a test1 test2

[root@localhost rsync]# ls test2 

test1

[root@localhost rsync]# ls test2/test1/

1 123.txt  2  3

這裏有一個問題,就是本來想把test1目錄直接拷貝成test2目錄,可結果rsync卻新建了test2目錄然後把test1放到test2當中。爲了避免這樣的情況發生,可以這樣做:

[root@localhost rsync]# rm -rf test2

[root@localhost rsync]# rsync -a test1/ test2/

[root@localhost rsync]# ls -l test2/

總用量 0-rw-r--r-- 1 root root  0  6月 10 12:58 1l

rwxrwxrwx 1 root root 13  6月 10 12:59 123.txt -> /root/123.txt

-rw-r--r-- 1 root root  0  6月 10 12:58 2

-rw-r--r-- 1 root root  0  6月 10 12:58 3

加一個斜槓就好了,在使用rsync備份目錄時要養成加斜槓的習慣。

在上面講了-a選項等同於-rlptgoD,而且 -a 還可以和 --no-OPTIN 一併使用。下面看看-l選項的作用:

[root@localhost rsync]# rsync -av --no-l test1/ test2/

sending incremental file listcreated directory test2./1skipping non-regular file "123.txt"23sent 200 bytes  received 72 bytes  544.00 bytes/sectotal size is 13  speedup is 0.05

使用-v選項看來就是方便呀,上例告訴我們跳過了非普通文件123.txt,其實123.txt是一個軟連接文件,如果不使用-l選項則不理會軟連接文件的。雖然加上-l選項會把軟連接文件給拷貝過去,但是軟連接的目標文件卻沒有拷貝過去,有時候咱們指向拷貝軟連接文件所指向的目標文件,那這時候該怎麼辦呢?

3)使用-L選項

[root@localhost rsync]# rsync -avL test1/ test2/sending incremental file listcreated directory test2./1123.txt23sent 231 bytes  received 91 bytes  644.00 bytes/sectotal size is 0  speedup is 0.00

[root@localhost rsync]# ls -l test2/

總用量 0

-rw-r--r-- 1 root root 0  6月 10 12:58 1

-rw-r--r-- 1 root root 0  6月 10 12:39 123.txt

-rw-r--r-- 1 root root 0  6月 10 12:58 2

-rw-r--r-- 1 root root 0  6月 10 12:58 3

加上 -L 選項就可以把SRC中軟連接的目標文件給拷貝到DST.

4) 使用-u選項

首先查看一下test1/1 和test2/1的創建時間(肯定是一樣的),然後使用touch修改一下test2/1的創建時間(此時test2/1要比test1/1的創建時間晚了一些),如果不加-u選項的話,會把test2/1的創建時間變成和test1/1的創建時間一樣。這樣講也許你會迷糊,不妨看一看:

[root@localhost rsync]# ll test1/1 test2/1

-rw-r--r-- 1 root root 0  6月 10 12:58 test1/1

-rw-r--r-- 1 root root 0  6月 10 12:58 test2/1

兩者之間的創建時間是一樣的,下面修改test2/1 的創建時間,然後不加-u同步:

[root@localhost rsync]# touch test2/1

[root@localhost rsync]# ll test2/1

-rw-r--r-- 1 root root 0  6月 10 13:20 test2/1

[root@localhost rsync]# rsync -a test1/1 test2/

[root@localhost rsync]# ll test2/1

-rw-r--r-- 1 root root 0  6月 10 12:58 test2/1

test2/1 的創建時間又變成和test1/1的創建時間一樣了。下面加上 -u 再看看結果是怎麼樣的:

[root@localhost rsync]# touch test2/1

[root@localhost rsync]# ll test2/1

-rw-r--r-- 1 root root 0  6月 10 13:31 test2/1

[root@localhost rsync]# rsync -avu test1/ test2/

sending incremental file list./123.txt -> /root/123.txt

sent 100 bytes  received 18 bytes  236.00 bytes/sectotal size is 13  speedup is 0.11

[root@localhost rsync]# ll test2/1

-rw-r--r-- 1 root root 0  6月 10 13:31 test2/1

[root@localhost rsync]# ll test1/1

-rw-r--r-- 1 root root 0  6月 10 12:58 test1/1

加上-u 選項後,不會再把 test1/1 同步爲 test2/1 了,現在你明白 -u 選項的妙用了吧。

5)使用 --delete 選項

首先刪除test1/123.txt:

[root@localhost rsync]# rm -f test1/123.txt

[root@localhost rsync]# ls test1/1  2  3

然後把test1/ 目錄 同步到 test2/ 目錄下:

[root@localhost rsync]# rsync -av test1/ test2/

sending incremental file list./1sent 94 bytes  

received 34 bytes  256.00 bytes/sectotal 

size is 0  speedup is 0.00

[root@localhost rsync]# ls test2/1  

123.txt  2  3

test2/目錄並沒有刪除掉123.txt, 下面加上 --delete 選項:

[root@localhost rsync]# rsync -av --delete test1/ test2/

sending incremental file listdeleting 123.txtsent 52 bytes  received 12 bytes  128.00 bytes/sectotal size is 0  speedup is 0.00

[root@localhost rsync]# ls 

test2/1  2  3

test2/ 目錄裏的123.txt也被刪除了,這就是 --delete 選項的用處。還有一種情況就是如果在DST增加文件了,而SRC當中沒有這些文件,同步時加上 --delete 選項後同樣會刪除新增的文件:

[root@localhost rsync]# touch test2/4

[root@localhost rsync]# ls test1/1  2  3

[root@localhost rsync]# ls test2/1  2  3  4

[root@localhost rsync]# rsync -a --delete test1/ test2/

[root@localhost rsync]# ls test1/1  2  3

[root@localhost rsync]# ls test2/1  2  3

6)使用 --exclude 選項

[root@localhost rsync]# touch test1/4

[root@localhost rsync]# rsync -a --exclude="4" test1/ test2/

[root@localhost rsync]# ls test1/1

  2  3  4

[root@localhost rsync]# ls test2/1

  2  3

另外還可以使用匹配字符 

[root@localhost rsync]# touch test1/1.txt test1/2.txt

[root@localhost rsync]# ls test1/1 

 1.txt  2  2.txt  3  4

[root@localhost rsync]# rsync -a --progress --exclude=".txt" test1/ test2/

sending incremental file list./4          

 0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/5)sent 104 bytes  

received 34 bytes  276.00 bytes/sectotal size is 0  speedup is 0.00

[root@localhost rsync]# ls test2/1  2  3  4

上例中,阿銘也連帶着使用了 --progress 選項,這個主要是用來觀察rsync同步過程的狀態的。最後簡單總結一下,平時你使用rsync同步數據的時候,使用-a選項基本上就可以達到我們想要的效果了,只是有時候會有個別的需求,會用到 -a --no-OPTION, -u, -L, --delete, --exclude 以及 progress 這些選項,還有些選項阿銘都沒有介紹,如果在以後的工作中遇到特殊需求了,就去查一下rsync的man文檔吧。

3. rsync 應用實例

1)通過ssh的方式

最上面介紹的5種方式當中,第二、第三(1個冒號)就屬於通過ssh的方式,這種方式其實就是讓用戶去登錄到遠程機器,然後執行rsync的任務。

[root@localhost rsync]# rsync -avL test1/ [email protected]:/tmp/test2/

[email protected]'s password:

sending incremental file listcreated directory /tmp/test2./11.txt22.txt34sent 327 bytes  

received 129 bytes  182.40 bytes/sectotal size is 0  

speedup is 0.00

這種方式就是前面介紹的第二種方式了,是通過ssh拷貝的數據,需要輸入192.168.0.101 那臺機器www 賬戶的密碼。當然也可以使用第三種方式拷貝:

[root@localhost rsync]# rsync -avL [email protected]:/tmp/test2/ ./test3/

[email protected]'s password:

receiving incremental file listcreated directory ./test3./11.txt22.txt34sent 128 bytes  

received 351 bytes  38.32 bytes/sectotal size is 0  

speedup is 0.00

以上兩種方式如果寫到腳本里,備份起來就有麻煩了,因爲要輸入密碼,腳本本來就是自動的,不可能做到的。但是不代表沒有解決辦法。那就是通過密鑰驗證,密鑰不設立密碼就ok了。

在操作之前我們先講明主機信息: 192.168.0.10 (主機名Aming-1)和 192.168.0.101 (主機名Aming)需要從Aming-1上拷貝數據到Aming上。

首先確認一下Aming-1上是否有這個文件 /root/.ssh/id_rsa.pub:

[root@Aming-1 ~]# ssh-keygen

Generating public/private rsa key pair.

阿銘之前生成過密鑰對,所以這個文件已經存在了,如果你的Linux不存在這個文件,請按照如下方法生成:

[root@Aming-1 ~]# ssh-keygen

Generating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa):Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /root/.ssh/id_rsa.Your public key has been saved in /root/.ssh/id_rsa.pub.The key fingerprint is:3b:74:af:e8:08:ac:99:30:3f:ef:84:7a:a0:a6:3d:89 root@Aming-1

在這個過程中會有一些交互的過程,它首先提示要輸入這個密鑰的密碼,出於安全考慮應該定義個密碼,但是我們的目的就是爲了自動化同步數據,所以這裏不輸入任何密碼,直接按回車,即密碼爲空。最後則生成了私鑰(/root/.ssh/id_rsa)和公鑰文件(/root/.ssh/id_rsa.pub)

把公鑰文件的內容拷貝到目標機器上:

[root@Aming-1 ~]# cat .ssh/id_rsa.pubssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA5SPyJ/kliGTAMUan/GCN325VS8jMxvOn4uQoLU/NqBpCI3MrmvSucv6EAzxx1J2uOssW08el06LG+cUwXmm5mkqDRBV6C9qNnR/bVV5vr3QsUwbKPr7fdyJvruQWWR7cSL+mjP0SYmG2Qy2JcM3hl1IZArzC6yeUnq2Gwbax8LgbZE3XfRfOYdimwyh5Tfft7yLYipWc37k+oRUWkI3mW7PalsOlfQhxrLD/lS891y6RdSbGxMJWPoV0KMFbVh+uJgyAXpeuWl+F+/iuQPzb6w3h4pWI31bvbsE9BU82jSzHYEjpq3SN2MJN2vaLs5a0mVpm9zka/h4ITFB8Uy1iSQ== root@Aming-1

複製主機Aming-1的/root/.ssh/id_rsa.pub文件內容,並粘貼到主機Aming的/home/www/.ssh/authorized_keys中:

[root@Aming ~]# vim /home/www/.ssh/authorized_keys

在這一步也許你會遇到/home/www/.ssh目錄不存在的問題,可以手動創建,並修改目錄權限爲700也可以執行ssh-keygen命令生成這個目錄。保存/home/www/.ssh/authorized_keys文件後,再到主機Aming-1上執行:

[root@Aming-1 ~]# ssh [email protected] login: Wed Jun 12 12:24:34 2013 from 192.168.0.10[www@Aming ~]$

現在不用輸入密碼也可以登錄主機Aming了。下面先從Aming主機退出來,再從主機Aming-1上執行一下rsync命令試試吧。

[root@Aming-1 ~]# rsync -av rsync/test1/ [email protected]:/tmp/test4/sending incremental file listcreated directory /tmp/test4./11.txt22.txt34sent 327 bytes  received 129 bytes  912.00 bytes/sectotal size is 0  speedup is 0.00

2) 通過後臺服務的方式

這種方式可以理解成這樣,在遠程主機上建立一個rsync的服務器,在服務器上配置好rsync的各種應用,然後本機作爲rsync的一個客戶端去連接遠程的rsync服務器。下面就介紹一下,如何去配置一臺rsync服務器。

建立並配置rsync的配置文件 /etc/rsyncd.conf

[root@Aming-1 ~]# vim /etc/rsyncd.conf#port=873log file=/var/log/rsync.logpid file=/var/run/rsyncd.pid#address=192.168.0.10[test]path=/root/rsyncuse chroot=truemax connections=4read only=nolist=trueuid=rootgid=rootauth users=testsecrets file=/etc/rsyncd.passwdhosts allow=192.168.0.101

其中配置文件分爲兩部分:全部配置部分和模塊配置部分,全局部分就是幾個參數而已,就像阿銘的rsyncd.conf中port, log file, pid file, address這些都屬於全局配置,而[test]以下部分就是模塊配置部分了。一個配置文件中可以有多個模塊,模塊名自定義,格式就像阿銘的rsyncd.conf中的這樣。其實模塊中的一些參數例如use chroot, max connections, udi, gid, auth users, secrets file以及hosts allow都可以配置成全局的參數。當然阿銘給出的參數並不是所有的,你可以通過man rsyncd.conf 獲得更多信息。下面就簡單解釋一下這些參數的意義:

port 指定在哪個端口啓動rsyncd服務,默認是873

log file 指定日誌文件

pid file 指定pid文件,這個文件的作用涉及到服務的啓動以及停止等進程管理操作

address 指定啓動rsyncd服務的IP,假如你的機器有多個IP,就可以指定其中一個啓動rsyncd服務,默認是在全部IP上啓動

[test] 指定模塊名,自定義

path 指定數據存放的路徑

use chroot true|false 默認是true,意思是在傳輸文件以前首先chroot到path參數所指定的目錄下。這樣做的原因是實現額外的安全防護,但是缺點是需要以roots權限,並且不能備份指向外部的符號連接所指向的目錄文件。默認情況下chroot值爲true,如果你的數據當中有軟連接文件的話建議設置成false。

max connections 指定最大的連接數,默認是0即沒有限制

read only ture|false 如果爲true則不能上傳到該模塊指定的路徑下

list 指定當用戶查詢該服務器上的可用模塊時,該模塊是否被列出,設定爲true則列出,false則隱藏

uid/gid 指定傳輸文件時,以哪個用戶/組的身份傳輸

auth users 指定傳輸時要使用的用戶名

secrets file 指定密碼文件,該參數連同上面的參數如果不指定則不使用密碼驗證,注意該密碼文件的權限一定要是600

hosts allow 指定被允許連接該模塊的主機,可以是IP或者網段,如果是多個,之間用空格隔開

編輯secrets file,保存後要賦予600權限,如果權限不對,不能完成同步

[root@Aming-1 ~]# cat /etc/rsyncd.passwdtest:test123[root@Aming-1 ~]# chmod 600 /etc/rsyncd.passwd

啓動rsyncd服務

[root@Aming-1 ~]# rsync --daemon --config=/etc/rsyncd.conf

啓動後,可以查看一下日誌,並查看端口是否啓動:

[root@Aming-1 ~]# cat /var/log/rsync.log[root@Aming-1 ~]# netstat -lnp |grep 873tcp     0    0 0.0.0.0:873        0.0.0.0:  LISTEN      12066/rsynctcp     0    0 :::873             :::*       LISTEN      12066/rsync

如果想開機啓動,請把 rsync --daemon --confg=/etc/rsyncd.conf 寫入到/etc/rc.d/rc.local文件。

到另一臺機器上測試

[root@Aming ~]# rsync -avL [email protected]::test/test1/ /tmp/test5/Password:receiving incremental file listcreated directory /tmp/test5./11.txt22.txt34sent 143 bytes  received 354 bytes  994.00 bytes/sectotal size is 0  speedup is 0.00

阿銘剛剛提到有一個選項叫做 “use chroot” 默認爲true,如果是true,同步的文件中如果有軟連接,則會有問題,首先在主機Aming-1的/root/rsync/test1/ 目錄下創建一個軟連接文件:

[root@Aming-1 ~]# ln -s /root/test.txt rsync/test1/test.txt[root@Aming-1 ~]# ls -l rsync/test1/test.txtlrwxrwxrwx 1 root root 14  6月 12 13:24 rsync/test1/test.txt -> /root/test.txt

然後再到主機Aming上,同步:

[root@Aming ~]# rsync -avL [email protected]::test/test1/ /tmp/test6/Password:receiving incremental file listsymlink has no referent: "/test1/test.txt" (in test)created directory /tmp/test6./11.txt22.txt34sent 143 bytes  received 419 bytes  1124.00 bytes/sectotal size is 0  speedup is 0.00rsync error: some files/attrs were not transferred (see previous errors) (code23) at main.c(1532) [generator=3.0.6]

可以看到,如果設置 “use chroot” 爲true則同步軟連接文件會有問題,下面阿銘把主機Aming-1的rsync配置文件修改一下,把true改爲false:

[root@Aming-1 ~]# sed -i 's/use chroot=true/use chroot=false/'  /etc/rsyncd.conf[root@Aming-1 ~]# grep 'use chroot' /etc/rsyncd.confuse chroot=false

然後再到主機Aming上再次執行同步:

[root@Aming ~]# rsync -avL [email protected]::test/test1/ /tmp/test7/Password:receiving incremental file listcreated directory /tmp/test7./11.txt22.txt34test.txtsent 162 bytes  received 410 bytes  1144.00 bytes/sectotal size is 0  speedup is 0.00

這樣就沒有任何問題啦,你也許會奇怪,爲什麼阿銘修改完rsyncd.conf配置文件後,沒有重啓rsyncd服務呢?其實這是rsync的一個特定機制,配置文件時即時生效的,不用重啓服務。

上面的例子中,阿銘都有輸入密碼,這樣同樣也不能寫入腳本中自動執行,其實這種方式也是可以不用手動輸入密碼的,它有兩種實現方式。

第一種,指定密碼文件

在客戶端上,也就是主機Aming上,編輯一個密碼文件:

[root@Aming ~]# vim /etc/pass

加入test用戶的密碼:

[root@Aming ~]# cat /etc/passtest123

修改密碼文件的權限:

[root@Aming ~]# chmod 600 /etc/pass

在同步的時候,指定一下密碼文件,就可以省去輸入密碼的步驟了:

[root@Aming ~]# rsync -avL [email protected]::test/test1/ /tmp/test8/ --password-file=/etc/pa***eceiving incremental file listcreated directory /tmp/test8./11.txt22.txt34test.txtsent 190 bytes  received 451 bytes  1282.00 bytes/sectotal size is 0  speedup is 0.00

第二種:在rsync服務器端不指定用戶

在服務端也就是主機Aming-1上修改配置文件rsyncd.conf, 去掉關於認證賬戶的配置項(auth user 和 secrets file這兩行):

sed -i 's/auth users/#auth users/;s/secrets file/#secrets file/' /etc/rsyncd.conf

上面的這個命令是把 “auth users” 和 “secrets file” 兩行的最前面加一個 “#”, 這樣就把這兩行註釋掉,使其失去意義。在前面阿銘未曾講過sed的這種用法,其實也不難弄明白,只是用分號把兩個替換的子命令塊給替換了而已。然後我們再到客戶端主機Aming上測試:

[root@Aming ~]# rsync -avL 192.168.0.10::test/test1/ /tmp/test9/receiving incremental file listcreated directory /tmp/test9./11.txt22.txt34test.txtsent 162 bytes  received 410 bytes  1144.00 bytes/sectotal size is 0  speedup is 0.00

注意,這裏不用再加test這個用戶了,默認是以root的身份拷貝的,現在已經不需要輸入密碼了。


http://ask.apelearn.com/question/5440X298X  拓展


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章