rsync工具介紹、rsync常用選項(xiàng)、rsync通過(guò)ssh同步

目錄

一、rsync工具介紹
二、rsync常用選項(xiàng)
三、rsync通過(guò)ssh同步

一、rsync工具介紹

rsync命令是一個(gè)遠(yuǎn)程數(shù)據(jù)同步工具,可通過(guò)LAN/WAN快速同步多臺(tái)主機(jī)間的文件。

rsync不僅可以遠(yuǎn)程同步數(shù)據(jù)(類(lèi)似于scp),而且可以本地同步數(shù)據(jù)(類(lèi)似于cp),但不同于cp或scp的一點(diǎn)是,它不會(huì)覆蓋以前的數(shù)據(jù)(如果數(shù)據(jù)已經(jīng)存在),而是先判斷已經(jīng)存在的數(shù)據(jù)和新數(shù)據(jù)的差異,只有數(shù)據(jù)不同時(shí)才會(huì)把不相同的部分覆蓋。

系統(tǒng)中沒(méi)有rsync,執(zhí)行yum install -y rsync命令安裝rsync。

  • 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

舉例:
把/etc/passwd同步到本地/tmp/目錄下,并改名為a.txt。

[root@minglinux-01 ~]# rsync -av /etc/passwd /tmp/a.txt
sending incremental file list
passwd

sent 1,184 bytes  received 35 bytes  2,438.00 bytes/sec
total size is 1,092  speedup is 0.90
[root@minglinux-01 ~]# ll !$
ll /tmp/a.txt
-rw-r--r-- 1 root root 1092 10月 24 23:26 /tmp/a.txt

把/etc/passwd遠(yuǎn)程數(shù)據(jù)備份,形式為:用戶(hù)名[@IP]:路徑,比如root@192.168.162.132:/root/,對(duì)方機(jī)器也需要安裝rsync,示例命令如下:

[root@minglinux-01 ~]# rsync -av /etc/passwd root@192.168.162.132:/root/a.txt
root@192.168.162.132's password: 
sending incremental file list
passwd

sent 1,184 bytes  received 35 bytes  221.64 bytes/sec
total size is 1,092  speedup is 0.90

[root@minglinux-02 ~]# ls    //minglinux-02IP地址即192.168.162.132
anaconda-ks.cfg  a.txt
[root@minglinux-02 ~]# ll a.txt 
-rw-r--r--. 1 root root 1092 10月 24 23:26 a.txt

二、rsync常用選項(xiàng)

rsync命令常用選項(xiàng):

-a:這是歸檔模式,表示以遞歸方式傳輸文件,并保持所有屬性,它等同于-rlptgoD。-a選項(xiàng)后面可以跟一個(gè)--no-OPTION,表示關(guān)閉-rlptgoD中的某一個(gè),比如-a--no-l等同于-rptgoD。
-r:表示以遞歸模式處理子目錄。它主要是針對(duì)目錄來(lái)說(shuō)的,如果單獨(dú)傳一個(gè)文件不需要加-r選項(xiàng),但是傳輸目錄時(shí)必須加。
-v:表示打印一些信息,比如文件列表、文件數(shù)量等。
-l:表示保留軟連接。
-L:表示像對(duì)待常規(guī)文件一樣處理軟連接。如果是SRC中有軟連接文件,則加上該選項(xiàng)后,將會(huì)把軟連接指向的目標(biāo)文件復(fù)制到DST。
-p:表示保持文件權(quán)限。
-o:表示保持文件屬主信息。
-g:表示保持文件屬組信息。
-D:表示保持設(shè)備文件信息。
-t:表示保持文件時(shí)間信息。
--delete:表示刪除DST中SRC沒(méi)有的文件。
--exclude=PATTERN:表示指定排除不需要傳輸?shù)奈募忍?hào)后面跟文件名,可以是萬(wàn)用字符模式(如*.txt)。
--progress:表示在同步的過(guò)程中可以看到同步的過(guò)程狀態(tài),比如統(tǒng)計(jì)要同步的文件數(shù)量、同步的文件傳輸速度等。
-u:表示把DST中比SRC還新的文件排除掉,不會(huì)覆蓋。
-z:加上該選項(xiàng),將會(huì)在傳輸過(guò)程中壓縮

選項(xiàng)使用練習(xí):

  1. 建立練習(xí)用的目錄和文件
[root@minglinux-01 ~]# mkdir rsync
[root@minglinux-01 ~]# cd rsync/
[root@minglinux-01 rsync]# mkdir test1
[root@minglinux-01 rsync]# cd test1/
[root@minglinux-01 test1]# touch 1 2 3 /root/123.tzr
[root@minglinux-01 test1]# touch 1 2 3 /root/123.txt
[root@minglinux-01 test1]# ln -s /root/123.txt ./123.txt
[root@minglinux-01 test1]# ll
總用量 0
-rw-r--r-- 1 root root  0 10月 31 22:47 1
lrwxrwxrwx 1 root root 13 10月 31 22:48 123.txt -> /root/123.txt
-rw-r--r-- 1 root root  0 10月 31 22:47 2
-rw-r--r-- 1 root root  0 10月 31 22:47 3
  1. 使用-a選項(xiàng)

使用rsync備份目錄時(shí)要在源目錄和目標(biāo)目錄后面加上斜杠/,否則達(dá)不到復(fù)制效果。

示例命令如下:

[root@minglinux-01 rsync]# rsync -a test1/ test2/
[root@minglinux-01 rsync]# ll test2/
總用量 0
-rw-r--r-- 1 root root  0 10月 31 22:47 1
lrwxrwxrwx 1 root root 13 10月 31 22:48 123.txt -> /root/123.txt
-rw-r--r-- 1 root root  0 10月 31 22:47 2
-rw-r--r-- 1 root root  0 10月 31 22:47 3

-a選項(xiàng)等同于-rlptgoD,且-a還可以和--no-OPTIN一并使用。
使用--no-l不備份鏈接文件,示例命令如下:

[root@minglinux-01 rsync]# rm -rf test2 
[root@minglinux-01 rsync]# ls
test1
[root@minglinux-01 rsync]# rsync -av --no-l test1/ test2/
sending incremental file list
created directory test2
skipping non-regular file "123.txt"
./
1
2
3

sent 229 bytes  received 144 bytes  746.00 bytes/sec
total size is 13  speedup is 0.03
  1. 使用-L選項(xiàng)
    加上-L選項(xiàng)就可以把SRC中軟連接的目標(biāo)文件復(fù)制到DST。示例命令如下:
[root@minglinux-01 rsync]# rm -rf test2 
[root@minglinux-01 rsync]# rsync -avL test1/ test2/
sending incremental file list
created directory test2
./
1
123.txt
2
3

sent 260 bytes  received 123 bytes  766.00 bytes/sec
total size is 0  speedup is 0.00
[root@minglinux-01 rsync]# ll test2/
總用量 0
-rw-r--r-- 1 root root 0 10月 31 22:47 1
-rw-r--r-- 1 root root 0 10月 31 22:47 123.txt
-rw-r--r-- 1 root root 0 10月 31 22:47 2
-rw-r--r-- 1 root root 0 10月 31 22:47 3
  1. 使用-u選項(xiàng)

首先查看test1/1和test2/1的創(chuàng)建時(shí)間是一樣的:

[root@minglinux-01 rsync]#  ll test1/1 test2/1 
-rw-r--r-- 1 root root 0 10月 31 22:47 test1/1
-rw-r--r-- 1 root root 0 10月 31 22:47 test2/1

面修改test2/1的創(chuàng)建時(shí)間,然后使用rsync不加-u同步:

[root@minglinux-01 rsync]# echo "1" > test2/1
[root@minglinux-01 rsync]# ll !$
ll test2/1
-rw-r--r-- 1 root root 2 10月 31 23:09 test2/1
[root@minglinux-01 rsync]# rsync -a test1/1 test2/
[root@minglinux-01 rsync]# ll test2/1
-rw-r--r-- 1 root root 0 10月 31 22:47 test2/1

上面test2/1的創(chuàng)建時(shí)間還是和test1/1一樣。下面加上-u選項(xiàng):

[root@minglinux-01 rsync]# echo "123" > test2/1
[root@minglinux-01 rsync]# ll test2/1
-rw-r--r-- 1 root root 4 10月 31 23:12 test2/1
[root@minglinux-01 rsync]# rsy
rsync                  rsyslogd               rsyslog-recover-qi.pl
[root@minglinux-01 rsync]# rsync -avu test1/ test2/
sending incremental file list
./
123.txt -> /root/123.txt

sent 129 bytes  received 22 bytes  302.00 bytes/sec
total size is 13  speedup is 0.09
[root@minglinux-01 rsync]# ll test1/1 test2/1
-rw-r--r-- 1 root root 0 10月 31 22:47 test1/1
-rw-r--r-- 1 root root 4 10月 31 23:12 test2/1

加上-u選項(xiàng)后,不會(huì)再把test1/1同步為test2/1了。

  1. 使用--delete選項(xiàng)

首先刪除test1/123.txt,然后把test1/目錄同步到test2/目錄下:

[root@minglinux-01 rsync]# rm -f test1/123.txt 
[root@minglinux-01 rsync]# ls test1/
1  2  3
[root@minglinux-01 rsync]# rsync -av test1/ test2/
sending incremental file list
./
1

sent 127 bytes  received 38 bytes  330.00 bytes/sec
total size is 0  speedup is 0.00
[root@minglinux-01 rsync]# ls test2/
1  123.txt  2  3

上例中,test2/目錄并沒(méi)有刪除test1目錄中沒(méi)有的123.txt。下面加上--delete選項(xiàng),示例如下:

[root@minglinux-01 rsync]# rsync -av --delete test1/ test2/
sending incremental file list
deleting 123.txt

sent 81 bytes  received 23 bytes  208.00 bytes/sec
total size is 0  speedup is 0.00
[root@minglinux-01 rsync]# ls test2/
1  2  3

這樣test2/目錄下的123.txt也被刪除了。

如果在DST中增加文件了,而SRC當(dāng)中沒(méi)有這些文件,同步時(shí)加上
--delete選項(xiàng)后同樣會(huì)刪除新增的文件。如下所示:

[root@minglinux-01 rsync]# ls test2/
1  2  3
[root@minglinux-01 rsync]# touch test2/4
[root@minglinux-01 rsync]# ls test1/
1  2  3
[root@minglinux-01 rsync]# ls test2/
1  2  3  4
[root@minglinux-01 rsync]# rsync -a --delete test1/ test2/
[root@minglinux-01 rsync]# ls test1/ test2/
test1/:
1  2  3

test2/:
1  2  3
  1. 使用--exclude選項(xiàng)
[root@minglinux-01 rsync]# touch test1/4
[root@minglinux-01 rsync]# rsync -a --exclude="4" test1/ test2/
[root@minglinux-01 rsync]# ls test1/ test2/
test1/:
1  2  3  4

test2/:
1  2  3

三、rsync通過(guò)ssh同步

前面介紹過(guò)的rsync的5種命令格式中如下兩種就屬于通過(guò)ssh的方式備份數(shù)據(jù)。這種方式其實(shí)就是讓用戶(hù)登錄到遠(yuǎn)程機(jī)器,然后執(zhí)行rsync的任務(wù)。

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

第一種方式已經(jīng)實(shí)現(xiàn)過(guò)了,下面試試第二種方式:

[root@minglinux-01 rsync]# rsync -avL 192.168.162.132:/root/a.txt ./test3
root@192.168.162.132's password: 
receiving incremental file list
a.txt

sent 43 bytes  received 1,183 bytes  272.44 bytes/sec
total size is 1,092  speedup is 0.89
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容