Linux命令——rsync
阿新 • • 發佈:2018-12-08
參考:Rsync (Remote Sync): 10 Practical Examples of Rsync Command in Linux
How to Sync Files/Directories Using Rsync with Non-standard SSH Port
How to Use Rsync to Sync New or Changed/Modified Files in Linux
簡介
rsync是遠端(或本地)複製和同步檔案最常用的命令。 藉助rsync命令,你可以跨目錄,跨磁碟和跨網路遠端與本地資料進行復制和同步。舉例來說:在兩臺Linux主機之間進行資料備份和映象。本文介紹在Linux主機上進行遠端和本地傳輸檔案的常見用法,不需要root賬戶也可以允許rsync。
rsync特性
- 高效地複製同步資料到對端,或者對端到本地
- 支援複製連結、裝置、屬主、屬組、許可權
- 比scp(Secure Copy)更快。rsync使用遠端更新協議( remote-update protocol ),這允許僅僅傳輸兩組檔案之間的差異。對於首次傳輸,它將檔案或目錄的全部內容從源複製到目標,但是從下次起,它僅將變化部分複製到目標。
- Rsync消耗較少的頻寬,因為它使用壓縮和解壓縮方法,同時傳送和接收資料兩端。HTTP壓縮技術
基本語法
rsync options source destination
-v : 詳細模式輸出
-r : 遞迴拷貝資料,但是傳輸資料時不保留時間戳和許可權copies data recursively (but don’t preserve timestamps and permission while transferring data
-a : 歸檔模式, 歸檔模式總是遞迴拷貝,而且保留符號連結、許可權、屬主、屬組時間戳
-z : 壓縮傳輸
-h : human-readable
--progress: 顯示傳輸過程
使用場景
本地拷貝同步檔案、目錄
同步一個檔案從本地一個目錄到另一個目錄,如果目標目錄不純在,會自動建立
[[email protected]]# rsync -zvh backup.tar /tmp/backups/ created directory /tmp/backups backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18M speedup is 1.10View Code
再演示同步目錄
[[email protected]View Code]# rsync -avzh /root/rpmpkgs /tmp/backups/ sending incremental file list rpmpkgs/ rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm rpmpkgs/nagios-3.5.0.tar.gz rpmpkgs/nagios-plugins-1.4.16.tar.gz sent 4.99M bytes received 92 bytes 3.33M bytes/sec total size is 4.99M speedup is 1.00
遠端拷貝同步檔案、目錄
本地到遠端
[[email protected]]$ rsync -avz rpmpkgs/ [email protected]192.168.0.101:/home/ [email protected]192.168.0.101's password: sending incremental file list ./ httpd-2.2.3-82.el5.centos.i386.rpm mod_ssl-2.2.3-82.el5.centos.i386.rpm nagios-3.5.0.tar.gz nagios-plugins-1.4.16.tar.gz sent 4993369 bytes received 91 bytes 399476.80 bytes/sec total size is 4991313 speedup is 1.00View Code
遠端到本地
[[email protected]]# rsync -avzh [email protected]192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms [email protected]192.168.0.100's password: receiving incremental file list created directory /tmp/myrpms rpmpkgs/ rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm rpmpkgs/nagios-3.5.0.tar.gz rpmpkgs/nagios-plugins-1.4.16.tar.gz sent 91 bytes received 4.99M bytes 322.16K bytes/sec total size is 4.99M speedup is 1.00View Code
通過ssh使用rsync