Linux-rsync命令用法詳解


從字面意思上,rsync 可以理解為 remote sync(遠程同步),但它不僅可以遠程同步數據(類似於 scp 命令),還可以本地同步數據(類似於 cp 命令)。不同於 cp 或 scp 的一點是,使用 rsync 命令備份數據時,不會直接覆蓋以前的數據(如果數據已經存在),而是先判斷已經存在的數據和新數據的差異,只有數據不同時才會把不相同的部分覆蓋。

在系統學習 rsync 命令之前,請確認你的 Linux 系統中已經安裝有此命令,如果沒有,可以直接使用 yum install -y rsync 命令安裝。

講解 rsync 用法之前,為了讓大家對此命令有一個整體的認識,這里先舉個例子:

[root@localhost ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list

sent 34 bytes  received 15 bytes  98.00 bytes/sec
total size is 1432  speedup is 29.22

此例中,通過執行 rsync 命令,實現了將 /etc/passwd 文件本地同步到 /tmp/ 目錄下,並改名為 1.txt。

除此之外,rsync 命令還支持遠程同步數據,也就是將本地的數據備份到遠程機器上。比如說,我們知道遠程機器的 IP 地址為 192.168.188.128,則使用 rsync 命令備份 passwd 文件的執行命令為:

[root@localhost ~]# rsync -av /etc/passwd 192.168.188.128:/tmp/1.txt
The authenticity of host '192.168.188.128 (192.168.188.128)' can't be established.
ECDSA key fingerprint is 26:e3:97:e7:bb:ae:17:33:ea:aa:Oc:5f:37:Oe:9e:fa.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.l68.l88.l28' (ECDSA) to the list of known hosts.
root@192.168.188.128's password: <-- 輸入密碼
sending incremental file list

sent 31 bytes received 12 bytes 7.82 bytes/sec
total size is 1432 speedup is 54.91

注意,首次遠程連接時,會提示是否要繼續連接,輸入 yes 即可。另外,當成功建立連接后,需要輸入目標系統的 root 密碼。

通過以上 2 個實例,讀者應該能對“rsync既支持本地備份數據,還支持遠程備份數據”有了直觀的認識。那么,rsync 命令要怎樣使用呢?

rsync 命令的基本格式有多種,分別是:

[root@localhost ~]# rsync [OPTION] SRC DEST
[root@localhost ~]# rsync [OPTION] SRC [USER@]HOST:DEST
[root@localhost ~]# rsync [OPTION] [USER@]HOST:SRC DEST
[root@localhost ~]# rsync [OPTION] [USER@]HOST::SRC DEST
[root@localhost ~]# rsync [OPTION] SRC [USER@]HOST::DEST

針對以上 5 種命令格式,rsync 有 5 種不同的工作模式:

  • 第一種用於僅在本地備份數據;
  • 第二種用於將本地數據備份到遠程機器上;
  • 第三種用於將遠程機器上的數據備份到本地機器上;
  • 第四種和第三種是相對的,同樣第五種和第二種是相對的,它們各自之間的區別在於登陸認證時使用的驗證方式不同。


要知道,使用 rsync 在遠程傳輸數據(備份數據)前,是需要進行登陸認證的,這個過程需要借助 ssh 協議或者 rsync 協議才能完成。在 rsync 命令中,如果使用單個冒號(:),則默認使用 ssh 協議;反之,如果使用兩個冒號(::),則使用 rsync 協議。

ssh 協議和 rsync 協議的區別在於,rsync 協議在使用時需要額外配置,增加了工作量,但優勢是更加安全;反之,ssh 協議使用方便,無需進行配置,但有泄漏服務器密碼的風險。

另外,以上幾種格式中各個參數的含義如下:

  • SRC:用來表示要備份的目標數據所在的位置(路徑);
  • DEST:用於表示將數據備份到什么位置;
  • USER@:當做遠程同步操作時,需指明系統登錄的用戶名,如果不顯示指定,默認為以 root 身份登錄系統並完成同步操作。


rsync 命令提供使用的 OPTION 及功能如表 1 所示。

表 1 rsync 選項及功能

OPTION選項

功能

-a

這是歸檔模式,表示以遞歸方式傳輸文件,並保持所有屬性,它等同於-r、-l、-p、-t、-g、-o、-D 選項。-a 選項后面可以跟一個 --no-OPTION,表示關閉 -r、-l、-p、-t、-g、-o、-D 中的某一個,比如-a --no-l 等同於 -r、-p、-t、-g、-o、-D 選項。

-r

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

-v

表示打印一些信息,比如文件列表、文件數量等。

-l

表示保留軟連接。

-L

表示像對待常規文件一樣處理軟連接。如果是 SRC 中有軟連接文件,則加上該選項后,將會把軟連接指向的目標文件復制到 DEST。

-p

表示保持文件權限。

-o

表示保持文件屬主信息。

-g

表示保持文件屬組信息。

-D

表示保持設備文件信息。

-t

表示保持文件時間信息。

--delete

表示刪除 DEST 中 SRC 沒有的文件。

--exclude=PATTERN

表示指定排除不需要傳輸的文件,等號后面跟文件名,可以是通配符模式(如 *.txt)。

--progress

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

-u

表示把 DEST 中比 SRC 還新的文件排除掉,不會覆蓋。

-z

加上該選項,將會在傳輸過程中壓縮。


以上也僅是列出了 async 命令常用的一些選項,對於初學者來說,記住最常用的幾個即可,比如 -a、-v、-z、--delete 和 --exclude。

如果想查看 async 提供的所有選項,可直接執行 async 命令。

為了更好的演示各個選項的功能,需要做一些准備工作,執行如下命令:

#新建rsync目錄
[root@localhost ~]# mkdir rsync
[root@localhost ~]# cd rsync
#在rsync目錄中,創建test1目錄
[root@localhost rsync]# mkdir test1
[root@localhost rsync]# cd test1
#在test1目錄中,分別創建名為 1、2、3、/root.123.txt 文件
[root@localhost test1]# touch 1 2 3 /root/123.txt
[root@localhost test1]# ln -s /root/123.txt ./123.txt
[root@localhost test1]# ls -l
total 0
-rw-r--r--. 1 root root 0 0ct 23 07:34 1
lrwxrwxrwx. 1 root root 13 0ct 23 08:34 123.txt -> /root/123.txt
-rw-r--r--. 1 root root 0 0ct 23 07:34 2
-rw-r--r--. 1 root root 0 0ct 23 07:34 3
[root@localhost test1]# cd ..
#回到rsync目錄
[root@localhost rsync]#

在此基礎上,下面挑選了幾個常用的 OPTION 選項,給大家舉例說明它們的用法。

rsync -a 選項

首先來看看 -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 中。

如果想要實現將 test1 目錄中的內容直接備份到 test2 目錄中,需修改上面的命令為:

[root@localhost rsync]#rm -rf test2
[root@localhost rsync]# rsync -a test1/ test2/
[root@localhost rsync]# ls test2/
1  123.txt  2  3

可以看到,只需給 test1 和 test2 目錄后添加 / 斜杠即可。

前面講過,使用 -a 選項,等同於同時使用 -r、-l、-p、-t、-g、-o、-D 選項,且 -a 還可以和 --no-OPTION 一並使用。下面再來看看 -l 選項的作用:

[root@localhost rsync]# rm -rf test2
[root@localhost rsync]# rsync -av 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 選項,系統將不理會軟鏈接文件。

rsync --delete選項

通過表 1 可以看到,--delete 選項用來--delete 刪除 DEST 中 SRC 沒有的文件。例如:

#拷貝 test1 目錄下的數據
[root@localhost rsync]# rsync -a test1/ test2
#刪除 test1/123.txt 文件
[root@localhost rsync]# rm -f test1/123.txt
[root@localhost rsync]# ls test1/
1 2 3
[root@localhost rsync]# rsync -av test1/ test2/
sending incremental file list
./

sent 55 bytes  received 15 bytes 140.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost rsync]# ls test2/
1 123.txt 2 3

可以看到,當對 test1 目錄刪除了 123.txt 文件之后,再次備份並沒有對 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

可以看到,使用 --delete 選項進行備份數據時,test1 目錄一旦做了改變,那么 test2 也會做相應改變。

不僅如此,如果在 DEST 中增加文件,而 SRC 中不包含這些文件,那么在使用 --delete 選項做同步備份操作時,DEST 新增的這些文件會被刪除。例如:

[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 test2/
1 2 3

受到篇幅的限制,有關 rsync 命令其他選項的用法,本節不再給出具體實例,有興趣的讀者可自行編寫代碼進行測試。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM