
在 Linux 操作系統(tǒng)上,“rsync”代表遠程同步同步。它是用于將文件和目錄從源 (SRC) 同步(復(fù)制)到目標 (DEST) 的實用程序。
文件和目錄可以在本地主機上同步,也可以在遠程主機上同步。在 SRC 上,文件和目錄將被同步。而在 DEST 上,將發(fā)生同步的文件和目錄。
(資料圖)
主要分享低代碼、微服務(wù)、容器化、SAAS?、系統(tǒng)架構(gòu)方面的的?內(nèi)容??,希望?大家?點贊?,評論,關(guān)注?。
此外,rsync 命令可以遞歸地復(fù)制文件和目錄,復(fù)制符號鏈接,保留(權(quán)限、組、修改時間和所有權(quán))文件身份。
Rsync 可以使用兩種不同的方式來同步/復(fù)制文件或目錄:
使用遠程 shell:ssh 或 rsh直接通過 TCP 使用 rsync 守護進程更重要的是,rsync 使用增量傳輸算法,它只在 SRC 和 DEST 之間復(fù)制/同步不同的數(shù)據(jù)。
本教程將通過動手實踐和示例來介紹使用 rsync 命令的多種方法。我們還將詳細解釋 rsync 命令最常用的選項。
在深入研究 rsync 命令的使用方法之前,讓我們先看看它的語法:
rsync [options]
rsync 命令可用于在本地機器上復(fù)制/同步文件。
假設(shè)您要將名為“/home/linoxide”的用戶“l(fā)inoxide”的主目錄中的文件復(fù)制到文件夾“/opt/test”。
$ sudo rsync -zvh /home/linoxide/computer-networking.pdf /opt/test/computer-networking.pdfsent 1.01K bytes received 35 bytes 2.10K bytes/sectotal size is 3.03K speedup is 2.89
參數(shù)說明:
-z: 壓縮
-v: 詳細輸出
-h: 人類可讀
要將所有文件從 SRC 本地目錄傳輸?shù)?DEST 目錄,請運行帶有選項的 rsync 命令-zavh。
假設(shè)您要將名為“/home/linoxide”的用戶“l(fā)inoxide”的主目錄復(fù)制到文件夾“/opt/backup”。
$ sudo rsync -zavh /home/linoxide/ /opt/backup/sending incremental file list./.bash_history.bash_logout.bashrc.lesshst.profile.sudo_as_admin_successful.viminfocomputer-networking.pdf.ssh/.ssh/known_hostssent 4.85K bytes received 198 bytes 10.10K bytes/sectotal size is 9.59K speedup is 1.90
參數(shù)說明:
-a:在復(fù)制/同步過程中存檔數(shù)據(jù)
您可以驗證 rsync 命令是否成功?運行以下命令:
$ ls /opt/backup/ -latotal 40drwxr-xr-x 3 linoxide linoxide 4096 Thg 6 15 16:02 .drwxr-xr-x 4 root root 4096 Thg 6 15 16:25 ..-rw------- 1 linoxide linoxide 817 Thg 6 15 16:01 .bash_history-rw-r--r-- 1 linoxide linoxide 220 Thg 6 11 15:58 .bash_logout-rw-r--r-- 1 linoxide linoxide 3771 Thg 6 11 15:58 .bashrc-rw-rw-r-- 1 linoxide linoxide 3028 Thg 2 25 2017 computer-networking.pdf-rw------- 1 linoxide linoxide 40 Thg 6 15 15:51 .lesshst-rw-r--r-- 1 linoxide linoxide 807 Thg 6 11 15:58 .profiledrwx------ 2 linoxide linoxide 4096 Thg 6 13 17:58 .ssh-rw-r--r-- 1 linoxide linoxide 0 Thg 6 15 16:02 .sudo_as_admin_successful-rw------- 1 linoxide linoxide 680 Thg 6 14 16:50 .viminfo
您可以使用 ssh(安全外殼)復(fù)制/同步數(shù)據(jù),您的數(shù)據(jù)將通過加密的安全連接傳輸。在 Internet 上傳輸數(shù)據(jù)時,沒有人可以讀取您的數(shù)據(jù)。當您使用 rsync 命令時,您需要提供用戶/root 密碼來完成特定任務(wù)。使用 SSH 選項將以加密方式發(fā)送您的登錄信息,以確保您的密碼安全。
使用特定的 ssh 端口將文件從遠程復(fù)制到本地。為了使用 rsync 命令指定協(xié)議,您需要使用-e帶有您要使用的協(xié)議名稱的選項。
例如,以下 rsync 命令將通過遠程 shell 將文件“deploy.yaml”從遠程計算機復(fù)制到用戶“l(fā)inoxide”的主目錄。
$ rsync -avz -e "ssh -p 22" cas@10.9.8.41:/home/cas/deploy.yaml /home/linoxide/cas@10.9.8.41"s password: receiving incremental file listsent 20 bytes received 60 bytes 32.00 bytes/sectotal size is 239 speedup is 2.99
假設(shè)您要將本地主機中用戶“l(fā)inoxide”的主目錄中的文件夾“test-data”復(fù)制/同步到遠程計算機“cas@10.9.8.41”的主目錄,運行命令:
$ rsync -azrvh /home/linoxide/computer-networking.pdf cas@10.9.8.41: cas@10.9.8.41"s password: sending incremental file listcomputer-networking.pdfsent 1.04K bytes received 35 bytes 430.80 bytes/sectotal size is 3.03K speedup is 2.81
參數(shù)說明:
-r: 遞歸復(fù)制
rsync 命令可幫助您傳輸要復(fù)制到本地主機的遠程目錄“文檔”。
假設(shè)您要將遠程主機“cas@10.9.8.41”中的文件/目錄復(fù)制/同步到用戶“l(fā)inoxide”的主目錄。
例如:
$ rsync -zavhr cas@10.9.8.41:document /home/linoxidecas@10.9.8.41"s password: receiving incremental file listdocument/document/kubernetes.txtsent 47 bytes received 139 bytes 74.40 bytes/sectotal size is 0 speedup is 0.00
這兩個選項允許我們通過指定參數(shù)來包含和排除文件,這些選項有助于我們指定您想要包含在同步中的文件或目錄,并 排除 您不想傳輸?shù)奈募臀募A。
在此示例中,rsync 命令將僅包含以“k”開頭的文件和目錄,并排除所有其他文件和目錄。
$ rsync -avhz -e "ssh -p 22" --include "k*" --exclude "*" cas@10.9.8.41: /home/linoxidecas@10.9.8.41"s password: receiving incremental file list./k8s.yamlkubernetes.yamlkuber/sent 88 bytes received 280 bytes 147.20 bytes/sectotal size is 73 speedup is 0.20
您可以指定要傳輸或同步的最大文件大小。您可以使用“--max-size”選項來做到這一點。
在以下示例中,最大文件大小為 200k,因此該命令將僅傳輸那些等于或小于 200k 的文件。
$ rsync -zavhr --max-size="200k" cas@10.9.8.41: /home/linoxidecas@10.9.8.41"s password: receiving incremental file list.bash_history.bash_logout.bashrc.profile.sudo_as_admin_successful.viminfocomputer-networking.pdfsent 2.58K bytes received 51.60K bytes 21.67K bytes/sectotal size is 113.24K speedup is 2.09
現(xiàn)在,假設(shè)您有一個 Web 服務(wù)器和一個數(shù)據(jù)備份服務(wù)器,您創(chuàng)建了一個每日備份并將其與您的備份服務(wù)器同步,現(xiàn)在您不想將備份的本地副本保留在您的 Web 服務(wù)器中。那么,您會等待傳輸完成然后手動刪除該本地備份文件嗎?當然不。這種自動刪除可以使用“--remove-source-files”選項來完成。
$ ls /home/directorycomputer-networking.pdf deploy.yaml document k8s.yaml kuber kubernetes.yaml------$ rsync -azvh --remove-source-files /home/linoxide/deploy.yaml cas@10.9.8.41:cas@10.9.8.41"s password: sending incremental file listsent 70 bytes received 20 bytes 36.00 bytes/sectotal size is 239 speedup is 2.66
再次運行"ls"命令,你可以看到文件"deploy.yaml"在SRC上被刪除了。
$ lscomputer-networking.pdf document k8s.yaml kuber kubernetes.yaml
為了找到 SRC 和 DEST 之間目錄文件的任何差異,您可以使用 "-i" 選項運行 rsync 命令。
$ rsync -avzi /home/linoxide/cloud cas@10.9.8.41:cas@10.9.8.41"s password: sending incremental file listcd+++++++++ cloud/10.限制帶寬
您可以在運行 rsync 命令時使用 "--bwlimit=
" 選項設(shè)置帶寬限制。 假設(shè)您要將數(shù)據(jù)傳輸速率限制為 300KB/s,請運行以下命令:
$ rsync -avzh --bwlimit=300 /home/linoxide/coursera.png cas@10.9.8.41:cas@10.9.8.41"s password: sending incremental file listcoursera.pngsent 2.93M bytes received 35 bytes 234.48K bytes/sectotal size is 3.00M speedup is 1.0211. scp失敗時恢復(fù)傳輸
有時,您必須通過 scp 命令傳輸一個非常大的文件,但在復(fù)制時出錯,并且由于文件大小和時間消耗過大而無法再次使用 scp 命令開始傳輸。
在這種情況下,您可以使用 rsync 命令從出現(xiàn)錯誤的位置開始復(fù)制文件。例如,將主目錄中的大型 ISO 文件復(fù)制到遠程主機 "cas@10.9.8.41"
$ scp /home/linoxide/ubuntu-18.04.4-live-server-amd64.iso cas@10.9.8.41:cas@10.9.8.41"s password: ubuntu-18.04.4-live-server-amd64.iso 32% 278MB 86.7MB/s 00:06 ETA該進程已被用戶殺死。當時,32% 的文件被傳輸。現(xiàn)在,您可以通過運行 rsync 繼續(xù)當前的工作:
$ rsync -P -avzh ubuntu-18.04.4-live-server-amd64.iso cas@10.9.8.41:cas@10.9.8.41"s password: sending incremental file listubuntu-18.04.4-live-server-amd64.iso 912.26M 100% 15.89MB/s 0:00:54 (xfr#1, to-chk=0/1)sent 541.25M bytes received 132.44K bytes 9.25M bytes/sectotal size is 912.26M speedup is 1.69rsync 命令恢復(fù)傳輸文件
12.傳輸數(shù)據(jù)時顯示進度
如果您想在運行 rsync 命令時查看傳輸進度,可以使用“--progress”選項。
它將顯示文件和完成復(fù)制的剩余時間。
例如:
$ rsync -avh --progress /home/linoxide/cloud/ cas@10.9.8.41:cas@10.9.8.41"s password: sending incremental file list./computer-networking.pdf 3.03K 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=8/10)coursera.png 3.00M 100% 114.38MB/s 0:00:00 (xfr#2, to-chk=7/10)k8s.yaml 39 100% 1.52kB/s 0:00:00 (xfr#3, to-chk=6/10)kubernetes.yaml 34 100% 1.33kB/s 0:00:00 (xfr#4, to-chk=5/10)ubuntu-18.04.4-live-server-amd64.iso 912.26M 100% 89.60MB/s 0:00:09 (xfr#5, to-chk=4/10)document/document/kubernetes.txt 0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=1/10)kuber/kuber/test.txt 0 100% 0.00kB/s 0:00:00 (xfr#7, to-chk=0/10)sent 915.49M bytes received 164 bytes 79.61M bytes/sectotal size is 915.26M speedup is 1.00帶有進度的 rsync 命令
結(jié)論
在本教程中,我們通過一些示例學(xué)習(xí)了如何使用 rsync 命令來復(fù)制或同步文件/目錄。
主要分享低代碼、微服務(wù)、容器化、SAAS?、系統(tǒng)架構(gòu)方面的的?內(nèi)容??,希望?大家?點贊?,評論,關(guān)注?。