日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Linux rsync命令用法詳解
在 linux 系統(tǒng)下,數(shù)據(jù)備份的工具很多,除了前面介紹了 tar、cpio、dd 命令之外,本節(jié)再介紹一個用來備份數(shù)據(jù)的命令,就是
rsync。

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供米脂企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、H5場景定制、小程序制作等業(yè)務(wù)。10年已為米脂眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。

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

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

[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

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

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

[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

通過以上 2 個實例,讀者應(yīng)該能對“rsync既支持本地備份數(shù)據(jù),還支持遠(yuǎn)程備份數(shù)據(jù)”有了直觀的認(rèn)識。那么,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 種不同的工作模式:

  • 第一種用于僅在本地備份數(shù)據(jù);
  • 第二種用于將本地數(shù)據(jù)備份到遠(yuǎn)程機器上;
  • 第三種用于將遠(yuǎn)程機器上的數(shù)據(jù)備份到本地機器上;
  • 第四種和第三種是相對的,同樣第五種和第二種是相對的,它們各自之間的區(qū)別在于登陸認(rèn)證時使用的驗證方式不同。

要知道,使用 rsync 在遠(yuǎn)程傳輸數(shù)據(jù)(備份數(shù)據(jù))前,是需要進(jìn)行登陸認(rèn)證的,這個過程需要借助 ssh 協(xié)議或者 rsync 協(xié)議才能完成。在 rsync 命令中,如果使用單個冒號(:),則默認(rèn)使用 ssh 協(xié)議;反之,如果使用兩個冒號(::),則使用 rsync 協(xié)議。 另外,以上幾種格式中各個參數(shù)的含義如下:

  • SRC:用來表示要備份的目標(biāo)數(shù)據(jù)所在的位置(路徑);
  • DEST:用于表示將數(shù)據(jù)備份到什么位置;
  • USER@:當(dāng)做遠(yuǎn)程同步操作時,需指明系統(tǒng)登錄的用戶名,如果不顯示指定,默認(rèn)為以 root 身份登錄系統(tǒng)并完成同步操作。

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

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

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

為了更好的演示各個選項的功能,需要做一些準(zhǔn)備工作,執(zhí)行如下命令:

#新建rsync目錄
[root@localhost ~]# mkdir rsync
[root@localhost ~]# cd rsync
#在rsync目錄中,創(chuàng)建test1目錄
[root@localhost rsync]# mkdir test1
[root@localhost rsync]# cd test1
#在test1目錄中,分別創(chuàng)建名為 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]#

在此基礎(chǔ)上,下面挑選了幾個常用的 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 目錄中的內(nèi)容直接放到 test2 目錄中,可結(jié)果 rsync 命令卻新建了 test2 目錄,然后把 test1 放到 test2 中。

如果想要實現(xiàn)將 test1 目錄中的內(nèi)容直接備份到 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 選項,系統(tǒng)將不理會軟鏈接文件。

rsync --delete選項

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

#拷貝 test1 目錄下的數(shù)據(jù)
[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

可以看到,當(dāng)對 test1 目錄刪除了 123.txt 文件之后,再次備份并沒有對 test2 目錄中的 123.txt 文件產(chǎn)生任何影響。

下面使用 --delete 選項,再次執(zhí)行拷貝命令,如下所示:

[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 選項進(jìn)行備份數(shù)據(jù)時,test1 目錄一旦做了改變,那么 test2 也會做相應(yīng)改變。

不僅如此,如果在 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


網(wǎng)頁名稱:Linux rsync命令用法詳解
文章位置:http://www.5511xx.com/article/cdcpijh.html