高效數據同步工具rsync服務部署詳解(下)

三、rsycn配置文件詳解 rsyncd.conf

1 部分知識補充

1.1 配置文件內容參考資料

man rsyncd.conf

1.2 配置文件內容總結

模塊之上內容爲全局變量信息

模塊之下內容爲局部變量信息

說明:

無論是全局變量發生變化,還是局部變量發生變化,都建議重啓rsync服務使配置生效。

2 利用/etc/init.d/啓動rsync服務方式

2.1 可以實現方式:

a. 編寫rsync啓動腳本(有一定的shell能力 if case)

b. 利用xinetd服務,管理啓動rsync服務

2.2 利用 xinetd服務 管理rsync

第一個里程碑: 安裝xinetd軟件

[root@backup ~]# yum install -y xinetd
[root@backup ~]# rpm -qa |grep xin
xinetd-2.3.14-40.el6.x86_64

第二個里程碑:編輯配置文件

修改disable = yes 改爲disable = no

[root@backup ~]# vim /etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#   allows crc checksumming etc.service rsync
{
disable   = no
flags     = IPv6
socket_type     = stream
wait            = no
user            = root
server          = /usr/bin/rsync
server_args     = --daemon
log_on_failure  += USERID
}

第三個里程碑:重啓xinetd服務

[root@backup ~]# /etc/init.d/xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]

傳輸測試

[root@nfs01 ~]# rsync -avzP /etc/services [email protected]::backup --
password-file=/etc/rsync.password
sending incremental file list
sent 29 bytes  received 8 bytes  74.00 bytes/sec
total size is 641020  speedup is 17324.86

3 定義變量信息實現免祕鑰交互

3.1 通過man手冊獲得方法

Some  modules  on the remote daemon may require authentication. If so, you will receive a password prompt when you connect.  You  can avoid  the  password  prompt  by  setting the environment variable RSYNC_PASSWORD to the password  you  want  to  use  or  using  the --password-file option. This may be useful when scripting rsync.
WARNING:  On some systems environment variables are visible to all users. On those systems using --password-file is recommended.

在遠程進程的一些模塊可能需要認證。如果是這樣的話,你將得到一個密碼提示當您連接。你可以通過設置環境變量rsync_password要使用或使用密碼文件選項密碼避免密碼提示。這可能是有用的腳本文件。

警告:在一些系統環境變量,對所有用戶都是可見的。在這些系統中使用的密碼文件的建議。

3.3.2 使用 RSYNC_PASSWORD 變量實現免交互

未設置變量之前

[root@nfs01 ~]# rsync -avzP /etc/services [email protected]::backup
Password:

添加上環境變量

[root@nfs01 ~]# export RSYNC_PASSWORD=oldboy123

測試

[root@nfs01 ~]# rsync -avzP /etc/services [email protected]::backup
sending incremental file list
sent 29 bytes  received 8 bytes  24.67 bytes/sec
total size is 641020  speedup is 17324.86

4 守護進程多模塊功能配置

第一個里程碑: 編寫配置信息創建多模塊

[root@backup ~]# vim /etc/rsyncd.conf
……
[nfsdata]
comment = "nfsdata dir by oldboy"
path = /backup/nfsdata
[nfsbackup]
comment = "nfsbackup dir by oldboy"
path = /backup/nfsbackup 

第二個里程碑: 創建多模塊指定的目錄

# 創建目錄,並修改目錄的權限
[root@backup ~]# mkdir /backup/nfs{data,backup} -p
[root@backup ~]# chown rsync.rsync  /backup/nfs{data,backup}

#查看:
[root@backup ~]# ll /backup/nfs{data,backup} -d
drwxr-xr-x 2 rsync rsync 4096 Oct 12 10:05 /backup/nfsbackup
drwxr-xr-x 2 rsync rsync 4096 Oct 12 10:05 /backup/nfsdata

第三里程碑: 利用rsync客戶端進行測試

[root@nfs01 ~]# rsync -avz /data[email protected]::nfsdata --password-file=/etc/rsync.passsword
sending incremental file list
./
nfs.data
sent 78 bytes  received 30 bytes  216.00 bytes/sec
total size is 0  speedup is 0.00

說明:

rsyncd.conf配置文件中,添加多模塊信息,可以不用重啓rsync服務,即時生效~

全局變量參數針對所有模塊生效;局部變量參數只針對指定模塊生效

read only參數默認配置爲ture,即爲只讀模式

全局變量發生變化,不用重啓rsync服務;局部變量發生變化,需要重啓rsync服務

注意:修改配置文件就重啓

無論是全局變量發生變化,還是局部變量發生變化,都建議重啓rsync服務使配置生效

5 守護進程的排除功能實踐

5.1 排除的方式

a) --exclude=要配置的目錄或文件名稱

b) --exclude-from=要排除多個目錄或文件彙總文件名稱

c) 在配置文件中進行修改,指定要排除的信息

5.2 排除測試

第一個里程碑: 創建模擬測試環境

[root@nfs01 data]# mkdir {a..d}
[root@nfs01 data]# touch {a..d}/{1..3}.txt
[root@nfs01 data]# tree.
├── a
│   ├── 1.txt
│   ├── 2.txt
│   └── 3.txt
├── b
│   ├── 1.txt
│   ├── 2.txt
│   └── 3.txt
├── c
│   ├── 1.txt
│   ├── 2.txt
│   └── 3.txt
└── d
    ├── 1.txt
    ├── 2.txt
    └── 3.txt4 directories, 12 files

第二個里程碑 利用 --exclude參數測試排除功能

# 需求:不要a目錄中3.txt 不要b、c目錄
[root@nfs01 data]# rsync -avz /data/ --exclude=a/3.txt --exclude=b --exclude=c [email protected]::nfsdata
sending incremental file list
./
a/
a/1.txt
a/2.txt
d/
d/1.txt
d/2.txt
d/3.txt
sent 300 bytes  received 114 bytes  828.00 bytes/sec
total size is 0  speedup is 0.00

精簡方式排除

[root@nfs01 data]# rsync -avz /data/ --exclude=a/3.txt --exclude={b,c} 
[email protected]::nfsdata
sending incremental file list
./
a/
a/1.txt
a/2.txt
d/
d/1.txt
d/2.txt
d/3.txt
sent 300 bytes  received 114 bytes  828.00 bytes/sec
total size is 0  speedup is 0.00

5.3 利用--exclude-from 方式進行排除

第一個里程碑: 創建模擬測試環境

[root@nfs01 data]# mkdir {a..d}
[root@nfs01 data]# touch {a..d}/{1..3}.txt

第二個里程碑:利用--exlude-from參數,測試排除功能

[root@nfs01 data]# vim /tmp/paichu.txt
a/3.txt
b
c

第三個里程碑:進行排除

[root@nfs01 data]# rsync -avz /data/ --exclude-from=/tmp/paichu.txt 
[email protected]::nfsdata
sending incremental file list
./
a/
a/1.txt
a/2.txt
d/
d/1.txt
d/2.txt
d/3.txt
sent 300 bytes  received 114 bytes  828.00 bytes/sec
total size is 0  speedup is 0.00

說明:

01. 排除文件中,需要利用相對路徑指定排除信息(不能利用絕對路徑)

02. 相對路徑指的是相對同步的目錄信息而言,是對rsync -avz /data/ 後面的data目錄進行相對

5.4 在配置文件中修改要排除的文件

第一個里程碑: 編寫修改服務端配置文件

vim /etc/rsyncd.conf
[nfsdata]
comment = "nfsdata dir by oldboy"
path = /backup/nfsdata
exclude=a/3.txt b c

第二個里程碑:重啓rsync服務

killall rsync && sleep 1 && rsync --daemon

第三里程碑: 進行測試

[root@nfs01 data]# rsync -avz /data/  [email protected]::nfsdata
sending incremental file list
./
a/
a/1.txt
a/2.txt
skipping daemon-excluded file "a/3.txt"
skipping daemon-excluded directory "b"
*** Skipping any contents from this failed directory ***
skipping daemon-excluded directory "c"
*** Skipping any contents from this failed directory ***
d/
d/1.txt
d/2.txt
d/3.txt
sent 407 bytes  received 116 bytes  1046.00 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]

6 守護進程來創建備份目錄

通過客戶端命令創建服務端備份目錄中子目錄

# 推送/etc/services文件到 服務器/backup/sda/目錄

[root@nfs01 ~]# rsync -avzP /etc/services [email protected]::backup/dba/
sending incremental file list
created directory dba
services
      641020 100%   19.34MB/s    0:00:00 (xfer#1, to-check=0/1)

# 推送/etc/services文件到 服務器/backup/sa/目錄

sent 127417 bytes  received 27 bytes  254888.00 bytes/sec
total size is 641020  speedup is 5.03
[root@nfs01 ~]# rsync -avzP /etc/services [email protected]::backup/sa/
sending incremental file list
created directory sa
services
      641020 100%   19.34MB/s    0:00:00 (xfer#1, to-check=0/1)

# 推送/etc/services文件到 服務器/backup/dev/目錄

sent 127417 bytes  received 27 bytes  254888.00 bytes/sec
total size is 641020  speedup is 5.03
[root@nfs01 ~]# rsync -avzP /etc/services [email protected]::backup/dev/
sending incremental file list
created directory dev
services
      641020 100%   18.71MB/s    0:00:00 (xfer#1, to-check=0/1) 
sent 127417 bytes  received 27 bytes  254888.00 bytes/sec
total size is 641020  speedup is 5.03

檢查結果:

[root@backup backup]# tree.
├── dba
│   └── services
├── dev
│   └── services
└── sa
    └── services

說明:

a 目標目錄名稱後要加上 "/", 表示創建目錄,否則變爲修改傳輸文件名稱了

b 利用客戶端創建服務備份子目錄時,只能創建一級子目錄。

7 守護進程的訪問控制配置

第一個里程碑:在服務端配置文件,編寫白名單策略或黑名單策略(只能取其一)

    vim /etc/rsyncd.conf
    hosts allow = 172.16.1.0/24
    #hosts deny = 0.0.0.0/32

關於訪問控制的說明:

01. 白名單和黑名單同時存在時,默認控制策略爲不匹配的傳輸數據信息全部放行

02. 白名單單一存在時,默認控制策略爲不匹配的傳輸數據信息全部禁止

03. 黑名單單一存在時,默認控制策略爲不匹配的傳輸數據信息全部放行

全局變量修改控制策略信息,rsync服務必須重啓

第二個里程碑:客戶端進行測試

[root@nfs01 backup]# rsync -avz /etc/services [email protected]::data
@ERRORUnknown module 'data'
rsync errorerror starting client-server protocol (code 5at main.c(1503) [sender=3.0.6]--------------------------------------------------------------------------------
[root@nfs01 backup]# rsync -avz /etc/services [email protected]::data    
sending incremental file list
sent 29 bytes  received 8 bytes  74.00 bytes/sec
total size is 641020  speedup is 17324.86

8 守護進程無差異同步配置

8.1 什麼是無差異:

推模式:我有什麼,你就有什麼;我沒有,你也不能有

拉模式:你有什麼,我就有什麼;你沒有,我也不能有

總結:服務端客戶端數據完全一致(一模一樣)

8.2 實現無差異同步方法

第一個里程碑: 創建實驗環境

[root@nfs01 ~]# ll /data/
total 16
drwxr-xr-x 2 root root 4096 Oct 12 10:29 a
drwxr-xr-x 2 root root 4096 Oct 12 10:40 b
drwxr-xr-x 2 root root 4096 Oct 12 10:29 c
drwxr-xr-x 2 root root 4096 Oct 12 10:29 d

第二個里程:進行第一次數據同步

[root@nfs01 ~]# rsync -avz --delete  /data[email protected]::backup/
sending incremental file list
./
a/
a/1.txt
a/2.txt
a/3.txt
b/
b/1.txt
b/2.txt
b/3.txt
c/
c/1.txt
c/2.txt
c/3.txt
d/
d/1.txt
d/2.txt
d/3.txt
sent 669 bytes  received 255 bytes  1848.00 bytes/sec
total size is 0  speedup is 0.00

第三個里程:刪除指定目錄,並添加指定文件,測試無差異功能

# 刪除客戶端中的 a/ 目錄,再進行無差異傳輸
[root@nfs01 data]# rm a/ -rf
[root@nfs01 data]# rsync -avz --delete  /data/ [email protected]::backup/
sending incremental file list
./
deleting a/3.txt
deleting a/2.txt
deleting a/1.txt
deleting a/
sent 181 bytes  received 14 bytes  390.00 bytes/sec
total size is 0  speedup is 0.00

8.3 【注意】無差異同步方法的應用

01. 實現儲存數據與備份數據完全一致(慎用

rsync -avz --delete  /data/ rsync_backup@172.16.1.41::backup /

02. 快速刪除大文件數據

1. mkdir /null      --創建出一個空目錄。
2. rsync -avz --delete /null/ /bigdata/
# 刪除效率高於 rm -rf /bigdata

9 守護進程的列表功能配置

第一個里程碑: 在服務端配置文件中開啓list列表功能

[root@backup ~]# vim /etc/rsyncd.conf
list = true

第二個里程碑:重啓rsync服務

[root@backup ~]# killall rsync && sleep 1 && rsync --daemon

第三個里程碑: 客戶端查看服務端模塊信息

[root@nfs01 data]# rsync [email protected]::
backup         "backup dir by oldboy"
nfsdata        "nfsdata dir by oldboy"
nfsbackup      "nfsbackup dir by oldboy"

說明:

爲了提升備份服務器安全性,建議關閉list列表功能


四、常見問題

[root@nfs01 tmp]# rsync -avz /etc/hosts  [email protected]::backup
Password:
sending incremental file list
hosts
rsync: mkstemp ".hosts.U5OCyR" (in backupfailed: Permission denied (13)
sent 200 bytes  received 27 bytes  13.76 bytes/sec
total size is 371  speedup is 1.63
rsync errorsome files/attrs were not transferred (see previous errors) (code 23at main.c(1039) [sender=3.0.6]

說明:備份目錄權限設置不正確

解決辦法:

將服務端的備份存放目錄(path值),屬主和屬組修改爲rsync。

 [root@backup ~]# chown -R rsync.rsync /backup/ 

本文內容來自 老男孩Linux雲計算運維優秀學員課後筆記整理

上一篇:

Linux雲計算運維學習之rsync服務部署詳解(上)


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