數據同步工具rsync

rsync應用實例

一. 經過ssh的方式linux

 

前面介紹的rsync 5種方式當中,第2、第三(1個冒號)就屬於經過ssh的方式,這種方式其實就是讓用戶去登陸到遠程機器,而後執行rsync的任務。vim

 

[root@localhost rsync]# rsync -avL test1/ www@192.168.0.101:/tmp/test2/安全

www@192.168.0.101's password:服務器

sending incremental file listssh

created directory /tmp/test2tcp

./ide

1工具

1.txt測試

2ui

2.txt

3

4

 

sent 327 bytes  received 129 bytes  182.40 bytes/sec

total size is 0  speedup is 0.00

 

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

 

[root@localhost rsync]# rsync -avL www@192.168.0.101:/tmp/test2/ ./test3/

www@192.168.0.101's password:

receiving incremental file list

created directory ./test3

./

1

1.txt

2

2.txt

3

4

 

sent 128 bytes  received 351 bytes  38.32 bytes/sec

total 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.pub

ssh-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 www@192.168.0.101

Last 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/ www@192.168.0.101:/tmp/test4/

sending incremental file list

created directory /tmp/test4

./

1

1.txt

2

2.txt

3

4

 

sent 327 bytes  received 129 bytes  912.00 bytes/sec

total size is 0  speedup is 0.00

 

二. 經過後臺服務的方式

 

這種方式能夠理解成這樣,在遠程主機上創建一個rsync的服務器,在服務器上配置好rsync的各類應用,而後本機做爲rsync的一個客戶端去鏈接遠程的rsync服務器。下面阿銘就介紹一下,如何去配置一臺rsync服務器。

 

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

 

[root@Aming-1 ~]# vim /etc/rsyncd.conf

#port=873

log file=/var/log/rsync.log

pid file=/var/run/rsyncd.pid

#address=192.168.0.10

 

[test]

path=/root/rsync

use chroot=true

max connections=4

read only=no

list=true

uid=root

gid=root

auth users=test

secrets file=/etc/rsyncd.passwd

hosts 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.passwd

test: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 873

tcp     0    0 0.0.0.0:873        0.0.0.0:*  LISTEN      12066/rsync

tcp     0    0 :::873             :::*       LISTEN      12066/rsync

 

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

 

到另外一臺機器上測試

 

[root@Aming ~]# rsync -avL test@192.168.0.10::test/test1/ /tmp/test5/

Password:

receiving incremental file list

created directory /tmp/test5

./

1

1.txt

2

2.txt

3

4

 

sent 143 bytes  received 354 bytes  994.00 bytes/sec

total 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.txt

lrwxrwxrwx 1 root root 14  6月 12 13:24 rsync/test1/test.txt -> /root/test.txt

 

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

 

[root@Aming ~]# rsync -avL test@192.168.0.10::test/test1/ /tmp/test6/

Password:

receiving incremental file list

symlink has no referent: "/test1/test.txt" (in test)

created directory /tmp/test6

./

1

1.txt

2

2.txt

3

4

 

sent 143 bytes  received 419 bytes  1124.00 bytes/sec

total size is 0  speedup is 0.00

rsync error: some files/attrs were not transferred (see previous errors) (code

23) 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.conf

use chroot=false

 

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

 

[root@Aming ~]# rsync -avL test@192.168.0.10::test/test1/ /tmp/test7/

Password:

receiving incremental file list

created directory /tmp/test7

./

1

1.txt

2

2.txt

3

4

test.txt

 

sent 162 bytes  received 410 bytes  1144.00 bytes/sec

total size is 0  speedup is 0.00

 

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

 

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

 

第一種,指定密碼文件

 

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

 

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

 

加入test用戶的密碼:

 

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

test123

 

修改密碼文件的權限:

 

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

 

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

 

[root@Aming ~]# rsync -avL test@192.168.0.10::test/test1/ /tmp/test8/ --password-file=/etc/pass

receiving incremental file list

created directory /tmp/test8

./

1

1.txt

2

2.txt

3

4

test.txt

 

sent 190 bytes  received 451 bytes  1282.00 bytes/sec

total 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 list

created directory /tmp/test9

./

1

1.txt

2

2.txt

3

4

test.txt

 

sent 162 bytes  received 410 bytes  1144.00 bytes/sec

total size is 0  speedup is 0.00

 

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

 

數據同步工具rsync介紹(1)

數據備份,毫無疑問很重要。阿銘就曾經有過一次很是痛苦的經歷,備份策略沒有作好,結果磁盤壞掉數據丟失,簡直是撕心裂肺的痛呀。還好數據重要性不是特別高,即便是不高也是丟失了數據,這是做爲系統管理員最不該該出現的事故。

 

因此,在你之後的系統維護工做中,必定要把數據備份當回事,認真對待。在linux系統下數據備份的工具不少,但阿銘就只用一種那就是rsync. 從字面上的意思你能夠理解爲remote sync (遠程同步)這樣可讓你理解的更深入一些。

 

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)? yes

Warning: Permanently added '192.168.0.101' (RSA) to the list of known hosts.

root@192.168.0.101'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 ), 請熟記他們吧。

 

數據同步工具rsync介紹(2)

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 1

lrwxrwxrwx 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 1

lrwxrwxrwx 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 list

created directory test2

./

1

skipping non-regular file "123.txt"

2

3

 

sent 200 bytes  received 72 bytes  544.00 bytes/sec

total 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 list

created directory test2

./

1

123.txt

2

3

 

sent 231 bytes  received 91 bytes  644.00 bytes/sec

total 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/sec

total 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

./

1

 

sent 94 bytes  received 34 bytes  256.00 bytes/sec

total 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 list

deleting 123.txt

 

sent 52 bytes  received 12 bytes  128.00 bytes/sec

total 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/sec

total 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 這些選項。

 

常見問題  : 

1. rsync 同步命令中,下面兩種方式有什麼不一樣呢?

答 : (1) rsync -av /dira/ ip:/dirb/

(2) rsync -av /dira/ ip::dirb

(1)前者是經過ssh方式同步的

(2)後者是經過rsync服務的方式同步的

2.rsync 同步時,若是要同步的源中有軟鏈接,如何把軟鏈接的目標文件或者目錄同步?

答 : 同步源文件須要加-L選項

3.rsync 同步數據時,如何過濾出全部.txt的文件不一樣步?

加上--exclude選項:--exclude=「*.txt」

 

4.rsync同步數據時,若是目標文件比源文件還新,則忽略該文件,如何作?

保留更新使用-u或者--update選項

 

5.使用rsync同步數據時,假如咱們採用的是ssh方式,而且目標機器的sshd端口並非默認的22端口,那咱們如何作?

rsync "--rsh=ssh -p 10022"或者rsync -e "ssh -p 10022"

6.rsync同步時,如何刪除目標數據多出來的數據,即源上不存在,但目標卻存在的文件或者目錄?

加上--delete選項

 

7.rsync使用服務模式時,若是咱們指定了一個密碼文件,那麼這個密碼文件的權限應該設置成多少才能夠?

600或400

8.

 

 

參考連接:

https://mp.weixin.qq.com/s/-aMDm-HGMfsaqF9Mr62yrQ

https://mp.weixin.qq.com/s/zVAPWTWrPuuquPwVmDl8mQ

https://mp.weixin.qq.com/s/2g5at0hjep9xZ8pUbmQ-Ug

相關文章
相關標籤/搜索