1. 程式人生 > >Linux基礎學習-資料備份工具rsync

Linux基礎學習-資料備份工具rsync

資料備份工具rsync

作為一個系統管理員,資料備份是非常重要的,如果沒有做好備份策略,磁碟損壞了,那麼你的資料將全部丟失,所以在日常的維護工作中,一定要時刻牢記給資料做備份.

rsync不僅可以可以遠端同步資料(類似於scp),而且可以本地同步資料(類似於cp),但不同於scp cp的一點是,它不會覆蓋以前的資料(如果資料已經存在),而是先判斷已經存在的資料和新資料的差異,只有資料不同時才會把不相同的部分覆蓋.如果Linux沒有rsync命令可以通過yum install rsync -y安裝.

常用選項 解釋
-a 這是歸檔模式,表示以遞迴方式傳輸檔案,並保持所有屬性,它等同於-rlptgoD.他後面可以跟一個–no-OPTION,表示關閉-rlptgoD中的某一個,比如-a–no-l等同於-rptgoD.
-r 表示以遞迴模式處理子目錄,它主要是針對目錄來說的.
-v 表示列印一些資訊,比如檔案列表,檔案數量
-l 表示保留軟連結
-L 表示保持軟連結,加上該引數後軟連結指向的目標檔案一起復制到目標中.
-p 表示標尺檔案許可權
-o 表示保持檔案的屬主資訊
-g 表示保持檔案的屬組資訊
-D 表示保持裝置檔案資訊
-t 表示保持穩健時間資訊
–delete 表示刪除DST中SRC沒有的檔案
–excluede=PATTERN 表示指定排除不需要傳輸的檔案
-u 表示把DST中比SRC還新的檔案排除掉,不會覆蓋
-z 加上此引數,將會在傳輸過程中壓縮
//直接拷貝

[root@backup ~]# rsync /etc/hosts /mnt

[root@backup ~]# rsync -vzrtopg /etc/hosts /mnt/
sending incremental file list
hosts

sent 184 bytes  received 31 bytes  430.00 bytes/sec
total size is 294  speedup is 1.37

//本地有的遠端就有(即使遠端沒有我給你),本地沒有遠端有的也要沒有

[root@backup
~]# rsync -avz --delete /test /mnt/ //遠端拷貝 [root@backup ~]# rsync -avz /etc/hosts -e 'ssh -p 22' [email protected]:/mnt/ [email protected]192.168.56.31's password: sending incremental file list hosts sent 184 bytes received 31 bytes 47.78 bytes/sec total size is 294 speedup is 1.37
uid = rsync //使用者 遠端的命令使用rsync訪問共享目錄
gid = rsync //使用者組
use chroot = no //安全相關
max connections = 200 //最大連線數
timeout = 300 //超時時間
pid file = /var/run/rsyncd.pid //程序對應的程序號檔案
lock file = /var/run/rsync.lock //鎖檔案
log file = /var/log/rsyncd.log //日誌檔案
[backup] //模組名稱
path = /backup //伺服器端提供訪問的目錄
ignore errors //忽略錯誤
read only = false //可寫
list = false //不能列表
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password

vim /etc/rsync.password
rsync_backup:123456
chmod 600 /etc/rsync.password

[root@backup ~]# lsof -i :873