二、備份服務(rsync)
(一)rsync服務介紹
Rsync是一款開源的、快速的、多功能的、可實作全量及增量的本地或遠程資料同步備份的優秀工具,并且可以不進行改變原有資料的屬性資訊,實作資料的備份遷移特性,Rsync軟體適用于unix/linux/windows等多種作業系統平臺,
Rsync是一個快速和非常通用的檔案復制工具,它能本地復制,遠程復制,或者遠程守護行程方式復制,它提供了大量的引數來控制其行為的各個方面,并且允許非常靈活的方式來實作檔案的傳輸復制,它以其delta-transfer演算法聞名,減少通過網路資料發送數量,利用只發送源檔案和目標檔案之間的差異資訊,從而實作資料的增量同步復制,
(二)rsync服務命令簡單應用
1、Rsync復制同步資料原理
在同步備份資料時,默認情況下,Rsync通過其獨特的“quick check”演算法,它僅同步大小或者最后修改時間發生變化的檔案或目錄,當然也可根據權限,屬主等屬性的變化同步,但需要指定相應的引數,甚至可以實作只同步一個檔案里有變化的內容部分,所以可以實作快速的同步備份資料,即采用增量復制方法對資料資訊進行同步,與傳統cp,scp拷貝工具的全量拷貝復制截然不同,增量同步復制資料,在效率上遠遠高于全量復制,
2、Rsync備份軟體7大特性總結:
支持拷貝普通檔案與特殊檔案如鏈接檔案,設備等,
支持排除指定檔案或目錄同步的功能,類似tar命令排除功能,
支持保持原檔案或目錄的所有屬性資訊不變,
支持增量同步,既只同步變化資料,提升資料傳輸效率,
支持使用rcp,rsh,ssh等方式來配合進行隧道加密傳輸檔案,
支持使用通過socket(守護行程方式)傳輸檔案或目錄資料資訊,
支持用戶認證方式傳輸資料,提升資料同步安全性,
3、Rsync服務命令在應用時,屬于一個非常強大的命令,可以通過rsync一個命令,替換下面4個命令的操作:
①. 實作本地資料同步復制(等價命令cp)
[root@backup ~]# # rsync == cp效果
[root@backup ~]# cp -a /etc/hosts /tmp/
[root@backup ~]# ll /tmp/
total 4
-rw-r--r--. 1 root root 352 Jan 27 01:15 hosts
[root@backup ~]# rsync -a /etc/sysconfig/network /tmp/
[root@backup ~]# ll /tmp/
total 8
-rw-r--r--. 1 root root 352 Jan 27 01:15 hosts
-rw-r--r-- 1 root root 31 Jan 26 18:16 network
②. 實作遠程資料同步復制(等價命令scp)
rsync == scp
[root@backup ~]# scp -rp /tmp/ 172.16.1.31:/tmp/
The authenticity of host '172.16.1.31 (172.16.1.31)' can't be established.
RSA key fingerprint is 5b:9b:e6:79:a9:95:4f:be:06:41:e3:bb:7a:12:ee:b4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.31' (RSA) to the list of known hosts.
[email protected]'s password:
network 100% 31 0.0KB/s 00:00
hosts 100% 352 0.3KB/s 00:00
[root@backup ~]# ll /tmp/
total 8
-rw-r--r--. 1 root root 352 Jan 27 01:15 hosts
-rw-r--r-- 1 root root 31 Jan 26 18:16 network
[root@backup ~]# rsync -rp /tmp/ 172.16.1.31:/tmp/
[email protected]'s password:
[root@backup ~]#
說明:同步資料時,/tmp/目錄后有/資訊,表示將目錄下面的資料內容進行備份同步
同步資料時,/tmp目錄后沒有/資訊,表示將目錄及目錄下面的資料內容進行備份同步
③. 實作資料資訊洗掉功能(等價命令rm)
說明:rsync實作洗掉目錄中資料內容程序,就將一個空目錄和一個有資料的目錄進行同步
最終,會將有資料的目錄中的檔案進行清空
[root@backup ~]# mkdir /null
[root@backup ~]# rsync --delete /null/ /tmp/
rsync: --delete does not work without -r or -d.
rsync error: syntax or usage error (code 1) at main.c(1422) [client=3.0.6]
[root@backup ~]#
[root@backup ~]# rsync -r --delete /null/ /tmp/
[root@backup ~]# ll /tmp/
total 0
④. 實作資料資訊查看功能(等價命令ls)
[root@backup ~]# ls /etc/hosts
/etc/hosts
[root@backup ~]# ls -l /etc/hosts
-rw-r--r--. 2 root root 352 Jan 27 01:15 /etc/hosts
[root@backup ~]# rsync /etc/hosts
-rw-r--r-- 352 2018/01/27 01:15:59 hosts
(三)rsync軟體作業方式
- 本地資料備份方式
Local: rsync [OPTION...] SRC... [DEST]
rsync --- 資料備份傳輸命令
option --- 可以輸入一下和rsync傳輸資料有關的引數
src --- 要進行備份的資料(檔案/目錄)
dest --- 將資料資訊備份到什么位置(相應路徑中)
實踐練習:
[root@backup ~]# rsync -a /etc/hosts /tmp/ok.txt
[root@backup ~]# ll /tmp/ok.txt
-rw-r--r-- 1 root root 352 Jan 27 01:15 /tmp/ok.txt
- 遠程資料備份方式
Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
pull方式語法說明:
rsync --- 資料備份傳輸命令
option --- 可以輸入一下和rsync傳輸資料有關的引數
[USER@]HOST: --- 需要指定以什么用戶身份登錄到遠程主機,
如果省略USER資訊,表示以當前用戶身份進行登錄
登錄主機地址或域名資訊
SRC --- 指定遠程主機要傳輸過來到本地的資料資訊
dest --- 將資料保存到本地的什么路徑中
push方式語法說明:
rsync --- 資料備份傳輸命令
option --- 可以輸入一下和rsync傳輸資料有關的引數
[USER@]HOST: --- 需要指定以什么用戶身份登錄到遠程主機,
如果省略USER資訊,表示以當前用戶身份進行登錄
登錄主機地址或域名資訊
SRC --- 指定本地主機要傳輸到遠程主機的資料
dest --- 將本地資料保存到遠端的什么路徑中
- 守護行程傳輸模式
③. 守護行程傳輸模式
Access via rsync daemon:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
pull:rsync [OPTION...] [USER@]HOST::SRC... [DEST]
[USER@]HOST:: --- 指定遠程連接的認證用戶
SRC --- 指定相應的模塊資訊
[DEST] --- 將遠程資料保存到本地的路徑資訊
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
[USER@]HOST:: --- 指定遠程連接的認證用戶
SRC --- 指定本地要進行推送的資料資訊
[DEST] --- 遠程進行保存資料的模塊資訊
(四) rsync守護行程部署流程
1、服務端部署流程
第一里程:檢查軟體是否安裝
[root@backup ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64
? 第二里程:撰寫組態檔
vim /etc/rsyncd.conf
#rsync_config
#created by HQ at 2017
##rsyncd.conf start##
uid = 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
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
[backup]
comment = "backup dir by oldboy"
path = /backup
read only = true
[nfs]
comment = "backup dir by oldboy"
path = /nfs
第三個里程:創建備份目錄管理用戶
useradd rsync -M -s /sbin/nologin
第四個里程:創建備份目錄
mkdir /backup
chown -R rsync.rsync /backup
第五個里程:創建認證檔案
echo "rsync_backup:oldboy123" >>/etc/rsync.password
chmod 600 /etc/rsync.password
第六個里程:啟動rsync服務
rsync --daemon
2、客戶端部署流程
第一個里程:確認軟體是否安裝
[root@backup ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64
第二個里程:創建認證密碼檔案
echo "oldboy123" >>/etc/rsync.password
chmod 600 /etc/rsync.password
第三個里程:進行資料備份測驗
[root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password
sending incremental file list
hosts
sent 189 bytes received 27 bytes 432.00 bytes/sec
total size is 352 speedup is 1.63
(五) rsync服務錯誤排查方法
1)檢查錯誤日志
2)可以在模擬環境下,模擬練習一些錯誤
擴展說明:rsync啟動服務擴展引數
--port 指定rsync服務埠號資訊,默認是873
--config=xxx 指定識別的rsync服務組態檔資訊
(六) rsync服務擴展應用
① 守護行程多模塊功能配置
第一步:修改組態檔
vim /etc/rsyncd.conf
[backup01]
comment = "backup dir by oldboy"
path = /backup
[backup02]
comment = "backup dir by oldboy"
path = /backup02
第二步:創建多模塊目錄
mkdir /backup02
chown -R rsync.rsync /backup02
第三步:重啟服務程式
killall rsync
rsync --daemon
第四步:進行測驗檢查
② 守護行程的排除功能實踐
第一種資料備份排除方式:--exclude
rsync -avz /test_dir/ --exclude=b --exclude=d [email protected]::backup01 --password-file=/etc/rsync.password
rsync -avz /test_dir/ --exclude={b,d} [email protected]::backup01 --password-file=/etc/rsync.password
rsync -avz /test_dir/ --exclude={b..d} [email protected]::backup01 --password-file=/etc/rsync.password
第二種資料備份排除方式:--exclude-from=file
rsync -avz /test_dir/ --exclude-from=./exclude_file.txt [email protected]::backup01 --password-file=/etc/rsync.password
③ 守護行程來創建備份目錄
rsync -avz /etc/hosts --exclude-from=./exclude_file.txt [email protected]::backup01/sa/ --password-file=/etc/rsync.password
rsync -avz /etc/hosts --exclude-from=./exclude_file.txt [email protected]::backup01/dev/ --password-file=/etc/rsync.password
rsync -avz /etc/hosts --exclude-from=./exclude_file.txt [email protected]::backup01/dba/ --password-file=/etc/rsync.password
④ 守護行程的訪問控制配置
三種情況:
1. 只有白名單,白名單網段或主機資訊允許,其余阻止
2. 只有黑名單,黑名單網段或主機資訊阻止,其余允許
3. 有黑名單也要白名單,白名單網段或主機資訊允許,黑名單網段或主機資訊阻止,其余允許
建議只選擇前兩種方式配置
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0./32
?
⑤ 守護行程無差異同步配置(--delete)
我有的,你也有;我沒有的,你也不能有
rsync -avz /test_dir/ --delete [email protected]::backup01 --password-file=/etc/rsync.password
說明:一定要謹慎使用,否則可能會清空備份目錄;
如果要快速清空目錄資料,也可以使用無差異同步清空
?
⑥ 守護行程的串列功能配置
list = false
說明:表示是否串列顯示rsync服務端所有模塊資訊
[root@nfs01 test_dir]# rsync [email protected]::
backup01 "backup dir by oldboy"
backup02 "backup dir by oldboy"
(七)Rsync服務常見問題匯總
-
rsync服務端開啟的iptables防火墻
【客戶端的錯誤】 No route to host 【錯誤演示程序】 [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup rsync: failed to connect to 172.16.1.41: No route to host (113) rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6] 【例外問題解決】 關閉rsync服務端的防火墻服務(iptables) [root@backup mnt]# /etc/init.d/iptables stop iptables: Setting chains to policy ACCEPT: filter [ OK ] iptables: Flushing firewall rules: [ OK ] iptables: Unloading modules: [ OK ] [root@backup mnt]# /etc/init.d/iptables status iptables: Firewall is not running. -
rsync客戶端執行rsync命令錯誤
【客戶端的錯誤】 The remote path must start with a module name not a / 【錯誤演示程序】 [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::/backup ERROR: The remote path must start with a module name not a / rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] 【例外問題解決】 rsync命令語法理解錯誤,::/backup是錯誤的語法,應該為::backup(rsync模塊) -
rsync服務認證用戶失敗*****
【客戶端的錯誤】 auth failed on module oldboy 【錯誤演示程序】 [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup Password: @ERROR: auth failed on module backup rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] 【例外問題解決】 【客戶端的錯誤】 auth failed on module oldboy 【錯誤演示程序】 [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup Password: @ERROR: auth failed on module backup rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] 【例外問題解決】 1. 密碼真的輸入錯誤,用戶名真的錯誤 2. secrets file = /etc/rsync.password指定的密碼檔案和實際密碼檔案名稱不一致 3. /etc/rsync.password檔案權限不是600 4. rsync_backup:123456密碼組態檔后面注意不要有空格 5. rsync客戶端密碼檔案中只輸入密碼資訊即可,不要輸入虛擬認證用戶名稱 -
rsync服務位置模塊錯誤
1. 【客戶端的錯誤】 Unknown module 'backup' 【錯誤演示程序】 [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup @ERROR: Unknown module 'backup' rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] 【例外問題解決】 2. /etc/rsyncd.conf組態檔模塊名稱書寫錯誤 -
rsync服務權限阻止問題
1. 【客戶端的錯誤】 Permission denied 【錯誤演示程序】 [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup Password: sending incremental file list hosts rsync: mkstemp ".hosts.5z3AOA" (in backup) failed: Permission denied (13) sent 196 bytes received 27 bytes 63.71 bytes/sec total size is 349 speedup is 1.57 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6] 【例外問題解決】 2. 備份目錄的屬主和屬組不正確,不是rsync 3. 備份目錄的權限不正確,不是755 -
rsync服務備份目錄例外
1. 【客戶端的錯誤】 chdir failed 【錯誤演示程序】 [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup Password: @ERROR: chdir failed rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] 【例外問題解決】 2. 備份存盤目錄沒有建立 3. 建立的備份存盤目錄和組態檔定義不一致 說明:如果沒有備份存盤目錄 -
rsync服務無效用戶資訊
【客戶端的錯誤】 invalid uid rsync 【錯誤演示程序】 [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup Password: @ERROR: invalid uid rsync rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] 【例外問題解決】 rsync服務對應rsync虛擬用戶不存在了 -
客戶端已經配置了密碼檔案,但免秘鑰登錄方式,依舊需要輸入密碼
【客戶端的錯誤】 password file must not be other-accessible 【錯誤演示程序】 [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password password file must not be other-accessible continuing without password file Password: sending incremental file list sent 26 bytes received 8 bytes 5.23 bytes/sec total size is 349 speedup is 10.26 【例外問題解決】 rsync客戶端的秘鑰檔案也必須是600權限 -
rsync客戶端連接慢問題
IP === 域名 反向DNS決議 【錯誤日志資訊】 錯誤日志輸出 2017/03/08 20:14:43 [3422] params.c:Parameter() - Ignoring badly formed line in configuration file: ignore errors 2017/03/08 20:14:43 [3422] name lookup failed for 172.16.1.31: Name or service not known 2017/03/08 20:14:43 [3422] connect from UNKNOWN (172.16.1.31) 2017/03/08 20:14:43 [3422] rsync to backup/ from rsync_backup@unknown (172.16.1.31) 2017/03/08 20:14:43 [3422] receiving file list 2017/03/08 20:14:43 [3422] sent 76 bytes received 83 bytes total size 349 正確日志輸出 2017/03/08 20:16:45 [3443] params.c:Parameter() - Ignoring badly formed line in configuration file: ignore errors 2017/03/08 20:16:45 [3443] connect from nfs02 (172.16.1.31) 2017/03/08 20:16:45 [3443] rsync to backup/ from rsync_backup@nfs02 (172.16.1.31) 2017/03/08 20:16:45 [3443] receiving file list 2017/03/08 20:16:45 [3443] sent 76 bytes received 83 bytes total size 349 【例外問題解決】 查看日志進行分析,撰寫rsync服務端hosts決議檔案
10 rsync服務沒有正確啟動
【錯誤日志資訊】
Connection refused (111)
【錯誤演示程序】
[root@oldboy-muban ~]# rsync -avz /etc/hosts [email protected]::backup
rsync: failed to connect to 172.16.1.41: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]
【例外問題解決】
[root@oldboy-muban ~]# rsync --daemon
[root@oldboy-muban ~]# ss -lntup |grep rsync
tcp LISTEN 0 5 :::873 :::* users:(("rsync",1434,5))
tcp LISTEN 0 5 *:873 *:* users:(("rsync",1434,4))
[root@oldboy-muban ~]# rsync -avz /etc/hosts [email protected]::backup
Password:
sending incremental file list
hosts
sent 196 bytes received 27 bytes 49.56 bytes/sec
total size is 349 speedup is 1.57
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/89808.html
標籤:Linux
