rsync工具介紹、rsync常用選項、rsync通過ssh同步

目錄

一、rsync工具介紹
二、rsync常用選項
三、rsync通過ssh同步

一、rsync工具介紹

rsync命令是一個遠程數據同步工具,可通過LAN/WAN快速同步多臺主機間的文件。

rsync不僅可以遠程同步數據(類似于scp),而且可以本地同步數據(類似于cp),但不同于cp或scp的一點是,它不會覆蓋以前的數據(如果數據已經存在),而是先判斷已經存在的數據和新數據的差異,只有數據不同時才會把不相同的部分覆蓋。

系統中沒有rsync,執行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遠程數據備份,形式為:用戶名[@IP]:路徑,比如root@192.168.162.132:/root/,對方機器也需要安裝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常用選項

rsync命令常用選項:

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

選項使用練習:

  1. 建立練習用的目錄和文件
[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選項

使用rsync備份目錄時要在源目錄和目標目錄后面加上斜杠/,否則達不到復制效果。

示例命令如下:

[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選項等同于-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選項
    加上-L選項就可以把SRC中軟連接的目標文件復制到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選項

首先查看test1/1和test2/1的創建時間是一樣的:

[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的創建時間,然后使用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的創建時間還是和test1/1一樣。下面加上-u選項:

[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選項后,不會再把test1/1同步為test2/1了。

  1. 使用--delete選項

首先刪除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/目錄并沒有刪除test1目錄中沒有的123.txt。下面加上--delete選項,示例如下:

[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當中沒有這些文件,同步時加上
--delete選項后同樣會刪除新增的文件。如下所示:

[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選項
[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通過ssh同步

前面介紹過的rsync的5種命令格式中如下兩種就屬于通過ssh的方式備份數據。這種方式其實就是讓用戶登錄到遠程機器,然后執行rsync的任務。

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

第一種方式已經實現過了,下面試試第二種方式:

[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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容