如何在Linux下使用rsync進行數據備份

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

rsync的主要好處是:

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

安全:傳輸可以通過ssh協議加密數據。

低帶寬:rsync可以在兩端壓縮和解壓數據塊。

語法:

  1. #rsysnc [options] source path destination path

示例: 1 - 啓用壓縮

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

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

示例: 2 - 保留文件和文件夾的屬性

  1. [root@localhost /]# rsync -azvr /home/aloft/ /backuphomedir
  2. building file list ... done
  3. ./
  4. .bash_logout
  5. .bash_profile
  6. .bashrc
  7.  
  8. sent 514 bytes received 92 bytes 1212.00 bytes/sec
  9. total size is 324 speedup is 0.53

上面我們使用了-a選項,它保留了所有人和所屬組、時間戳、軟鏈接、權限,並以遞歸模式運行。

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

  1. root@localhost /]# rsync -avz /home/aloft/ azmath@192.168.1.4:192.168.1.4:/share/rsysnctest/
  2. Password:
  3.  
  4. building file list ... done
  5. ./
  6. .bash_logout
  7. .bash_profile
  8. .bashrc
  9. sent 514 bytes received 92 bytes 1212.00 bytes/sec
  10. total size is 324 speedup is 0.53

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

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

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

上面的命令同步遠程文件到本地。

示例: 5 - 找出文件間的不同

  1. [root@localhost backuphomedir]# rsync -avzi /backuphomedir /home/aloft/
  2. building file list ... done
  3. cd+++++++ backuphomedir/
  4. >f+++++++ backuphomedir/.bash_logout
  5. >f+++++++ backuphomedir/.bash_profile
  6. >f+++++++ backuphomedir/.bashrc
  7. >f+++++++ backuphomedir/abc
  8. >f+++++++ backuphomedir/xyz
  9.  
  10. sent 650 bytes received 136 bytes 1572.00 bytes/sec
  11. total size is 324 speedup is 0.41

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

示例: 6 - 備份

rsync命令可以用來備份linux。

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

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

  1. vi /usr/local/sbin/bkpscript
  2.  
  3. rsync -avz -e ssh -p2093 /home/test/ root@192.168.1.150:/oracle/data/

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章