rsync備份實戰
阿新 • • 發佈:2021-01-21
目錄
reync備份服務實戰
1.reync基礎概述
角色 外網IP(NAT) 內網IP(LAN) 主機名
NFS eth0:10.0.0.11 eth1:172.16.1.11 nfs01
Rsync eth0:10.0.0.12 eth1:172.16.1.12 backup
1.什麼是備份
備份就是把檔案複製一份存起來(增加一個副本)
2.為什麼備份
資料非常重要
保證資料不丟失
便於快速的恢復
3.能不能不做備份
可以,對於不是特別重要的資料可以不考慮
完全備份 佔用空間 效率不高
增量備份 節省空間 效率高
備份工具: 本地備份:cp 將什麼資料拷貝到那個目錄下 (不可以離開伺服器本身) 遠端備份:scp 基於ssh協議基礎實現的更安全的cp ,可以實現跨主機 rsync 遠端同步工具,實現 “ 不同作業系統 ” 之間資料同步 Linux ---》 Linux Linux ---Windows Linux --- MacOS
4.rsync模式
1.基於命令使用,就好像在使用cp命令
2.基於服務的方式啟動,就需要暴露埠,讓程式持續執行
C/S client/Server 結構
5.rsync資料傳輸方式
本地傳輸: 和cp差不多 Local: rsync [OPTION...] SRC... [DEST] nginrsync: #命令 [OPTION...] #選項 SRC... #源 [DEST] #目標 ==總結==:使用rsync命令將什麼檔案拷貝到什麼路徑下 rsync OPTION SRC DEST rsync -avz /etc/hosts/tmp cp - a /etc/hosts /tmp
rsync -avz /etc/hosts /tmp # 第一次全量
rsync -avz /etc/hosts /tmp # 第二次增量,由於沒有變化,所以沒有更新操作
echo "##" >> /etc/hosts # 將檔案產生變化
rsync -avz /etc/hosts /tmp # 再次執行,會同步發生變化的內容
push #推送
Pull #拉取
遠端傳輸:A-B 可以在機器之間互相傳輸資料(推送和拉取) --》 依賴ssh協議,系統root密碼。
推送:
rsync [OPTION...] SRC... [USER@]HOST:DEST
檔案: 將11伺服器的/etc/hosts檔案,推送到12伺服器的/tmp,使用root使用者
[root@rui ~]# touch /opt/1.txt
[root@rui ~]# rsync -avz /opt/1.txt [email protected]:/tmp
[email protected]'s password:
sending incremental file list
1.txt
sent 84 bytes received 35 bytes 11.33 bytes/sec
total size is 0 speedup is 0.00
目錄: 將11伺服器的/opt目錄以及目錄下的所有檔案都推送到12伺服器的/tmp目錄,使用的是12伺服器的root使用者身份
[root@rui ~]# rsync -avz /tmp [email protected]:/opt
[email protected]'s password:
sending incremental file list
tmp/
tmp/hosts
tmp/ks-script-AShK6w
tmp/yum.log
tmp/.ICE-unix/
tmp/.Test-unix/
tmp/.X11-unix/
tmp/.XIM-unix/
tmp/.font-unix/
sent 1,385 bytes received 133 bytes 433.71 bytes/sec
total size is 997 speedup is 0.66
拉取:
rsync OPTION...HOST:SRC... [DEST]
檔案:拉取31伺服器/tmp/yum.log檔案,至41本地的/opt目錄
[root@rui ~]# rsync -avz [email protected]:/tmp/yum.log /opt/
[email protected]'s password:
receiving incremental file list
yum.log
sent 43 bytes received 85 bytes 36.57 bytes/sec
total size is 0 speedup is0.00
目錄: 目錄:拉取31伺服器/tmp目錄以及目錄下的所有內容,到41伺服器的/opt目錄下
rsync -avz [email protected]:/tmp /opt/
[email protected]'s password:
receiving incremental file list
tmp/
tmp/1.txt
tmp/ks-script-AShK6w
tmp/yum.log
tmp/.ICE-unix/
tmp/.Test-unix/
tmp/.X11-unix/
tmp/.XIM-unix/
tmp/.font-unix/
tmp/opt/
tmp/systemd-private-2ba621ac312148a991cd97e2a142d6f9-chronyd.service-MwYxU5/
tmp/systemd-private-2ba621ac312148a991cd97e2a142d6f9-chronyd.service-MwYxU5/tmp/
tmp/systemd-private-fa9eb8f36c154618ba1a6cabea720e3f-chronyd.service-w8wcz7/
tmp/systemd-private-fa9eb8f36c154618ba1a6cabea720e3f-chronyd.service-w8wcz7/tmp/
tmp/vmware-root_1077-4256545038/
tmp/vmware-root_951-4013330126/
tmp/vmware-root_955-3988228421/
tmp/vmware-root_967-4248221830/
sent 145 bytes received 1,346 bytes 596.40 bytes/sec
total size is 836 speedup is 0.56
守護程序:(不使用系統的真實使用者,可以自行diy一個使用者。)
Access via rsync daemon:
Pull: rsync OPTION...HOST::SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
服務端:10.0.0.11 客戶端:10.0.0.12
安裝配置服務
安裝
[root@rui ~]# yum -y install rsync #安裝rsync
配置(讓軟體按照我們所設定預期去執行)
[root@rui ~]# rpm -qc rsync #檢視rsync配置檔案的位置
/etc/rsyncd.conf
/etc/sysconfig/rsyncd
[root@rui ~]# cat /etc/rsyncd.conf #看一下他的配置檔案內容
[root@rui ~]# > /etc/rsyncd.conf # 清除rsync配置檔案原有內容
[root@backup ~]# vim /etc/rsyncd.conf #重新編寫配置檔案
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = welcome to backup serve
path = /backup
2.配置檔案註釋:
uid = rsync # rsync程序是使用rsync使用者身份執行
gid = rsync #
port = 873 # 預設監聽的埠是873
fake super = yes # 不用root身份也可以儲存完整的資料以及資料屬性
use chroot = no # 固定在某一個目錄中
max connections = 200 # 最大的連線數
timeout = 600 # 連線超時時間
ignore errors # 忽略錯誤
read only = false # 可以進行讀和寫操作
list = false #
auth users = rsync_backup # 自行定義假的使用者(不存在於系統中)
secrets file = /etc/rsync.passwd # 儲存假使用者的密碼 username:password
log file = /var/log/rsyncd.log # 日誌
#####################################
[backup] # 模組名稱
comment = welcome to backup server # 描述的意思
path = /backup # 模組名稱對應的具體目錄
3.根據配置檔案,初始化操作:
3.1 建立rsync使用者(主要是rsync程序執行時需要使用的身份)
[root@rui ~]# useradd rsync -r -M
3.2 建立密碼檔案/etc/rsync.passwd 密碼檔案中要填寫username:password 使用者要與配置檔案中定義的一致
[root@rui ~]# echo "rsync_backup:1" > /etc/rsync.passwd #自定義使用者並設定密碼“1”。
[root@rui ~]# chmod 600 /etc/rsync.passwd #給檔案600許可權 預設644許可權,其他使用者對它有執行許可權
[root@rui ~]# cat /etc/rsync.passwd #檢視檔案內容
rsync_backup:1
3.3建立儲存資料目錄:/backup
[root@rui ~]# mkdir /backup #建立/backup
[root@rui ~]# chown -R rsync.rsync /backup/ #給他屬主屬組到rsync
啟動
[root@rui ~]# systemctl restart rsyncd #啟動rsyncfuwu
[root@rui ~]# systemctl enable rsyncd #開機自動啟動rsync服務
[root@rui ~]# netstat -lntp #列出所有tcp和udp埠 (還有別的,我只複製了rsync的)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Addresstcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 19399/rsync
tcp6 0 0 :::873 :::* LISTEN 19399/rsync
測試 (客戶端使用推送或拉取你的方式)
10.0.0.12模擬客戶端進行推送測試:
[root@rui tmp]# rsync -avz [email protected]::backup /mnt/ #把客戶端的/tmp目錄推送到服務端
rsync選項引數:
-a #歸檔模式傳輸, 等於-tropgDl
-v #詳細模式輸出, 列印速率, 檔案數量等
-z #傳輸時進行壓縮以提高效率
-r #遞迴傳輸目錄及子目錄,即目錄下得所有目錄都同樣傳輸。
-t #保持檔案時間資訊
-o #保持檔案屬主資訊
-p #保持檔案許可權
-g #保持檔案屬組資訊
-l #保留軟連線
-P #顯示同步的過程及傳輸時的進度等資訊
-D #保持裝置檔案資訊
-L #保留軟連線指向的目標檔案
-e #使用的通道協議,指定替代rsh的shell程式
--exclude=PATTERN #指定排除不需要傳輸的檔案模式
--exclude-from=file #檔名所在的目錄檔案
--bwlimit=100 #限速傳輸
--delete #讓目標目錄和源目錄資料保持一致
1.--delete
推送:客戶端有100檔案,服務端有200檔案,服務端最後就剩下客戶端的100個檔案,高度與客戶端一致
[root@nfs ~]# touch /mnt/file-{1..10}
[root@nfs ~]# rsync -avz /mnt/ [email protected]::backup
[root@nfs ~]# rsync -avz /etc/ [email protected]::backup
[root@nfs ~]# rsync -avz --delete /mnt/ [email protected]::backup
拉取: 客戶端有 100檔案,服務端遊200檔案, 客戶端最終剩下200個服務端的檔案,與服務端保持一致、
[root@nfs ~]# cp /etc/* /mnt/
[root@nfs ~]# rsync -avz --delete [email protected]::backup /mnt/
2.--bwlimit限速 按照MB
[root@nfs ~]# dd if=/dev/zero of=/opt/bigdata bs=1M count=500
[root@nfs ~]# du -sh /opt/bigdata
[root@nfs ~]# rsync -avzP /opt/bigdata [email protected]::backup
開啟限速:
[root@backup ~]# rm -f /backup/bigdata
[root@nfs ~]# rsync -avzP --bwlimit 10 /opt/bigdata [email protected]::backup
Password:
sending incremental file list
bigdata
262,078,464 49% 9.89MB/s 0:00:25
2.客戶端非互動傳輸資料至服務端。
2.1) RSYNC_PASSWORD
[root@nfs ~]# export RSYNC_PASSWORD=1
[root@nfs ~]# rsync -avzP --bwlimit 10 /opt/bigdata [email protected]::backup
2.2) --passwd-file=/path/password ( 檔案中僅可以儲存客戶端密碼。多一點都不行。) 只需要在客戶端建立該檔案即可
[root@nfs ~]# echo "1" > /etc/rsync.pass
[root@nfs ~]# chmod 600 /etc/rsync.pass
[root@nfs ~]# rsync -avzP --bwlimit 10 /opt/bigdata [email protected]::backup --password-file=/etc/rsync.pass
@ERROR: auth failed on module backup # 密碼檔案許可權為600
rsync: mkstemp ".hosts.K9azMA" (in backup) failed: Permission denied (13)