如何在Linux下使用rsync

Bobbin Zachariah發表於2014-12-22

對於各種組織和公司,資料對他們是最重要的,即使對於電子商務,資料也是同樣重要的。Rsync是一款通過網路備份重要資料的工具/軟體。它同樣是一個在類Unix和Window系統上通過網路在系統間同步資料夾和檔案的網路協議。Rsync可以複製或者顯示目錄並複製檔案。Rsync預設監聽TCP 873埠,通過遠端shell如rsh和ssh複製檔案。Rsync必須在遠端和本地系統上都安裝。

rsync的主要好處是:

速度:最初會在本地和遠端之間拷貝所有內容。下次,只會傳輸發生改變的塊或者位元組。

安全:傳輸可以通過ssh協議加密資料。

低頻寬:rsync可以在兩端壓縮和解壓資料塊。

語法:

#rsysnc [options] source path destination path

示例: 1 - 啟用壓縮

[root@localhost /]# rsync -zvr /home/aloft/ /backuphomedir
building file list ... done
.bash_logout
.bash_profile
.bashrc
sent 472 bytes received 86 bytes 1116.00 bytes/sec
total size is 324 speedup is 0.58

上面的rsync命令使用了-z來啟用壓縮,-v是視覺化,-r是遞迴。上面在本地的/home/aloft/和/backuphomedir之間同步。

示例: 2 - 保留檔案和資料夾的屬性

[root@localhost /]# rsync -azvr /home/aloft/ /backuphomedir
building file list ... done
./
.bash_logout
.bash_profile
.bashrc

sent 514 bytes received 92 bytes 1212.00 bytes/sec
total size is 324 speedup is 0.53

上面我們使用了-a選項,它保留了所有人和所屬組、時間戳、軟連結、許可權,並以遞迴模式執行。

示例: 3 - 同步本地到遠端主機

root@localhost /]# rsync -avz /home/aloft/ azmath@192.168.1.4:192.168.1.4:/share/rsysnctest/
Password:

building file list ... done
./
.bash_logout
.bash_profile
.bashrc
sent 514 bytes received 92 bytes 1212.00 bytes/sec
total size is 324 speedup is 0.53

上面的命令允許你在本地和遠端機器之間同步。你可以看到,在同步檔案到另一個系統時提示你輸入密碼。在做遠端同步時,你需要指定遠端系統的使用者名稱和IP或者主機名。

示例: 4 - 遠端同步到本地

[root@localhost /]# rsync -avz azmath@192.168.1.4:192.168.1.4:/share/rsysnctest/ /home/aloft/
Password:
building file list ... done
./
.bash_logout
.bash_profile
.bashrc
sent 514 bytes received 92 bytes 1212.00 bytes/sec
total size is 324 speedup is 0.53

上面的命令同步遠端檔案到本地。

示例: 5 - 找出檔案間的不同

[root@localhost backuphomedir]# rsync -avzi /backuphomedir /home/aloft/
building file list ... done
cd+++++++ backuphomedir/
>f+++++++ backuphomedir/.bash_logout
>f+++++++ backuphomedir/.bash_profile
>f+++++++ backuphomedir/.bashrc
>f+++++++ backuphomedir/abc
>f+++++++ backuphomedir/xyz

sent 650 bytes received 136 bytes 1572.00 bytes/sec
total size is 324 speedup is 0.41

上面的命令幫助你找出源地址和目標地址之間檔案或者目錄的不同。

示例: 6 - 備份

rsync命令可以用來備份linux。

你可以在cron中使用rsync安排備份。

0 0 * * * /usr/local/sbin/bkpscript &> /dev/null

vi /usr/local/sbin/bkpscript

rsync -avz -e ‘ssh -p2093′ /home/test/ root@192.168.1.150:/oracle/data/

via: http://linoxide.com/how-tos/rsync-copy/

作者:Bobbin Zachariah 譯者:geekpi 校對:wxy

本文由 LCTT 原創翻譯,Linux中國 榮譽推出

相關文章