一、概念簡介
-
rsync
一款開源的快速增量備份工具,可以在不同主機之間鏡像同步整個目錄樹。第一次同步時, rsync 會複制全部内容,但在下一次同步時隻傳輸修改過的檔案;此外rsync 在傳輸資料的過程中可以實行壓縮及解壓縮操作,可以使用更少的帶寬。作為一款最常用的開源檔案備份工具,是現在linux和Unix系統預設基本元件。
-
inotify
一款強大的、細粒度的、異步的檔案系統事件監控機制,從linux 核心2.6.13起加入了 inotify 支援,為監視檔案系統的變化提供了強大的支援,實作檔案系統中打開、關閉、移動/重命名、删除、建立或者改變屬性等各種事件持續監控,而inotify-tools 正是實施監控的一套C庫和指令行工具,可為三方軟體提供一套開發接口庫函數。
rsync 僅為一款增量備份工具,需與其他工具配合使用實作自動備份功能,目前主流兩種實作方式:
觸發式 - 通過 crontab 守護程序方式進行觸發,實作定時觸發 rsync 同步(定時備份)。
監聽式 - 通過 inotify 監控檔案系統的各種變化,當檔案有任何變動時,實作實時觸發 rsync 同步(實時備份)。
本文以兩台Centos7.X系統采用rsync-3.1.2.10版本 + inotify-tools-3.14配套方案為例,搭建檔案實時同步機制(單向同步)。根據誰主動誰是用戶端,誰被動誰是服務端的思想,伺服器細分如下:
伺服器名稱 | 性質 | IP位址 | 工具安裝 | 系統版本 | 操作目錄 |
備份伺服器 | 服務端 | 172.16.42.65 | rsync | centos7.8 | /server/file/ |
源資料伺服器 | 用戶端 | 172.16.42.53 | rsync、inotify-tools | centos7.8 | /client/file/ |
二、配置操作
1)防火牆配置:(源資料伺服器與備份伺服器)
-
關閉selinux
vi /etc/selinux/config #編輯防火牆配置檔案,關閉selinux:
#SELINUX=enforcing #注釋掉
#SELINUXTYPE=targeted #注釋掉
SELINUX=disabled #添加配置為禁用狀态
:wq! #儲存,退出
setenforce 0 #立即生效
-
開啟rsync預設tcp 873端口
firewall-cmd --zone=public --add-port=873/tcp --permanent #開啟tcp873端口
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZwpmL3gDO0QzNzETM5IzMwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
systemctl restart firewalld.service #重新開機防火牆,使其配置生效
firewall-cmd --list-ports #不放心可以檢查開放的端口
2)備份伺服器配置(服務端172.16.42.65):
-
安裝rsync
rsync往往是現在Linux和Unix系統預設安裝的基本元件之一,可以通過運作指令 rpm -aq rsync 進行檢視,如下資訊說明已經安裝
如果沒有預設安裝,建議運作如下指令
yum install rsync
-
建立服務端rsync的使用者密碼認證檔案(含使用者名與密碼)
① 建立 /etc/rsyncd/ 目錄
[[email protected] ~]# mkdir /etc/rsyncd
② 在建立目錄下建立rsync.password密碼認證檔案,并寫入【使用者名:密碼】
echo "root:root123"> /etc/rsyncd/rsync.password
#其中 `root:1234qwer`使用者密碼與rsync.password 檔案名可自定義
③ 設定讀寫權限
chmod 600 /etc/rsyncd/rsync.password
-
編輯rsyncd.conf配置檔案,變動内容請參考<更改位置>,配置内容如下:
vi /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
#使用者
uid = root
#組
gid = root
#開啟預設監聽端口
port = 873
#不使用root切換模式
use chroot = no
#最大連結數
max connections = 10
strict modes = yes
#允許限定用戶端ip通路(用戶端-源資料伺服器ip)--- <更改位置>
hosts allow = 172.16.42.53
#開啟日志
transfer logging = yes
#同步資料時排除lost+found這個目錄
exclude = lost+found/
#逾時時間
#忽略沒有權限的使用者
ignore nonreadable = yes
#遇到這類檔案不進行壓縮
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
#pid檔案存放路徑
pid file = /var/run/rsyncd.pid
#lock檔案存放路徑
lock file = /var/run/rsync.lock
#log檔案存放路徑
log file = /var/log/rsyncd.log
#同步子產品名 --- <更改位置>
[inotify]
#監控目錄(保證必須存在)--- <更改位置>
path = /server/file/
#備注内容
comment = test!
ignore errors
#是否隻讀
read only = no
#是否隻寫
write only = no
hosts deny = *
list = false
uid = root
gid = root
#賦予權限的使用者,此處我用的是root使用者 --- <更改位置>
auth users = root (linux中真實存在的賬号) --- <更改位置>
#密碼檔案 --- <更改位置>
secrets file = /etc/rsyncd/rsync.password --- <更改位置>
注:配置參數與注釋請勿寫在同一行,不然同步時會報錯rsync error(1503)。
-
重新開機rsync服務,依次執行 (使其rsyncd.conf配置生效)
lsof -i :873 #可檢視是否已啟動
kill [pid]
rsync --daemon
3)源資料伺服器配置(用戶端172.16.42.53):
-
安裝rsync(隻安裝、不配置、不啟用)
請參考上文,此處不再贅述
-
安裝inotify
① 檢視目前系統是否支援inotify,如下3個條目代表支援
② 依次執行如下安裝指令
[[email protected] rsyncd]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[[email protected] rsyncd]# tar zxvf inotify-tools-3.14.tar.gz
[[email protected] rsyncd]# cd inotify-tools-3.14
[[email protected] inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[[email protected] inotify-tools-3.14]# make
[[email protected] inotify-tools-3.14]# make install
如果下載下傳位址不行,請網上另行查找更換。
-
建立通路備份伺服器的密碼認證檔案(僅含密碼)
① 建立 /etc/rsyncd/ 目錄
[[email protected] ~]# mkdir /etc/rsyncd
② 在建立目錄下建立密碼認證檔案,并配置備份伺服器的通路密碼
echo "1234qwer" >/etc/rsyncd/rsync.password
#其中 `1234qwer`密碼與rsync.password 檔案名可自定義
③ 設定讀寫權限
chmod 600 /etc/rsyncd/rsync.password
-
編寫執行腳本
① /etc/rsyncd/ 目錄下,建立 inotify.sh 腳本(腳本核心代碼為inotify的監控指令,可自行百度檢索學習),變動内容請參考<更改位置>,腳本代碼如下,:
#!/bin/bash
host=172.16.42.65 #服務端-備份伺服器ip --- <更改位置>
src=/client/file/ #所要監控的備份目錄(此處可以自定義,但是要保證存在)--- <更改位置>
des=inotify #服務端-備份伺服器的同步子產品名(需同備份伺服器的rsyncd.conf中的子產品名保持一緻)--- <更改位置>
user=root #服務端-備份伺服器的通路使用者名(linux中真實存在的賬号)--- <更改位置>
password=/etc/rsyncd/rsync.password #通路服務端-備份伺服器的密碼認證檔案 --- <更改位置>
inotify=/usr/local/inotify #inotify的安裝目錄(該路徑為inotify-tool的預設安裝位址)
${inotify}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files
do
rsync -avzP --delete --timeout=100 --password-file=${password} $src [email protected]$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
該腳本主要通過inotify監控/client/file/ 目錄内容,如果發生更改(添加、删除或修改)則通過rsync同步給備份伺服器的/server/file/目錄。
② 為腳本檔案增加 764 權限、為/etc/rc.local添加-x權限
chmod 764 /etc/rsyncd/inotify.sh
chmod -x /etc/rc.local
③ 背景運作腳本
sh /etc/rsyncd/inotify.sh &
④ 将腳本設定為開機自啟
echo "setsid /etc/rsyncd/inotify.sh &" >> /etc/rc.local
4)測試同步效果
在用戶端-源資料伺服器/client/file/ 目錄下建立檔案,服務端-備份伺服器檢視同步效果