文章目錄
- 一、配置 rsync 源服務器
- 1.1 rsync簡介
- 1.2 rsync特點
- 1.3 配置 rsync 同步源
- 1.4 測驗rsync功能
- 下行同步的三種方式
- 二、inotify+rsync 實時同步
- 2.1 配置 inotify+rsync 實時同步的步驟
- 2.2 inotify+rsync 實時同步實驗
- 1. 實驗之前將之前寫在站點的檔案全部洗掉
- 2. 手工編譯安裝inotify
- 3. 對持續性監控的測驗
- 4. 撰寫腳本實作兩臺服務器的實時同步
一、配置 rsync 源服務器
1.1 rsync簡介
rsync(Remote Sync,遠程同步)是一個開源的快速備份工具,可以在不同主機之間鏡像同步整個目錄樹,支持增量備份,并保持鏈接和權限,且采用優化的同步演算法,傳輸前執行壓縮,因此非常適用于異地備份、鏡像服務器等應用,
rsync 的官方站點的網址是 http://rsync.samba.org/,目前最新版本是 3.1.3,由 Wayne Davison 進行維護,作為一種最常用的檔案備份工具,rsync 是 Linux 和 UNIX 系統默認安裝的基本組件之一,
rsync不僅可以遠程同步資料(類似于scp),而且可以本地同步資料(類似于cp),但不同于scp和cp的一點是,它不會覆寫以前的資料(如果資料已經存在),而是先判斷已經存在的資料和新資料的差異,只有資料不同時才會把不同的部分覆寫,支持本地復制,或者與其他SSH、rsync主機同步,
1.2 rsync特點
- 可以鏡像保存整個目錄樹和檔案系統,
- 可以很容易做到保持原來檔案的權限、時間、軟硬鏈接等等,
- 無須特殊權限即可安裝,
- 快速:第一次同步時 rsync 會復制全部內容,但在下一次只傳輸修改過的檔案,rsync 在傳輸資料的程序中可以實行壓縮及解壓縮操作,因此可以使用更少的帶寬,
- 安全:可以使用scp、ssh等方式來傳輸檔案,當然也可以通過直接的socket連接,
- 支持匿名傳輸,以方便進行網站鏡像,
在遠程同步任務中,負責發起 rsync 同步操作的客戶機稱為發起端,而負責回應來自客 戶機的 rsync 同步操作的服務器稱為同步源,在同步程序中,同步源負責提供檔案的原始位置,發起端應對該位置具有讀取權限,

rsync 作為同步源時以守護行程運行,為其他客戶機提供備份源,配置 rsync 同步源需 要建立組態檔 rsyncd.conf,創建備份賬號,然后將 rsync 程式以守護行程(“–daemon”選項)方式運行,
1.3 配置 rsync 同步源
- 建立rsyncd.conf組態檔、獨立的賬號檔案
- 啟用rsync的–daemon模式
[root@localhost ~]# hostnamectl set-hostname source ##將主機名改為source便于區分
[root@localhost ~]# su
[root@localhost ~]# iptables -F ##關閉防火墻
[root@localhost ~]# setenforce 0 ##關閉核心防護
[root@localhost ~]# rpm -q rsync ##查看rsync是否安裝,屬于Linux內核自帶軟體
rsync-3.1.2-4.el7.x86_64
[root@localhost ~]# rpm -qc rsync ##查看rsync的組態檔位置
/etc/rsyncd.conf ##主組態檔位置
/etc/sysconfig/rsyncd
修改組態檔rsyncd.conf
uid = nobody ##設定用戶
gid = nobody ##設定用戶組
use chroot = yes ##禁錮家目錄,就是在訪問的程序中,會將訪問的用戶鎖定在指定的目錄,減少系統的安全風險
port 873 ##埠號
log file = /var/log/rsync.log ##rsync的日志檔案位置
pid file = /var/run/rsyncd.pid ##rsync的行程id位置
hosts allow = 14.0.0.0/24 ##白名單,允許14.0.0.0/24網段的IP進行訪問
[wwwroot] ##自定義的共享模塊,名字自取
path = /var/www/html ##源目錄路徑,自己定義
comment = www.njit.com
read only = yes ##設定為只讀,也就是只允許下載
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 ##同步時對這幾種格式的檔案不進行壓縮
auth users = sx ##授權的賬戶名
secrets file = /etc/rsync_users.db ##授權的賬戶密碼檔案的位置,需要手動建立
- 認證配置auth users、secrets file,不加則為匿名
- rsync賬號檔案
- 采用“用戶名:密碼”的記錄格式,每行一個用戶記錄
- 獨立的賬號資料,不依賴于系統賬號
- 啟用rsync服務
- 通過–daemon獨自提供服務
[root@source ~]# vim /etc/rsync_users.db ##建立一個授權資料檔案
sx:123456
[root@source ~]# cd /etc/
[root@source etc]# ll | grep rsync_users.db
-rw-r--r--. 1 root root 10 9月 11 22:22 rsync_users.db
檔案的權限為644,還是會被別人給讀到,所以降低權限,保證安全性
[root@source etc]# chmod 600 rsync_users.db
[root@source etc]# rsync --daemon ##開啟服務
[root@source etc]# netstat -ntap | grep rsync ##檢查服務開啟狀態
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 16860/rsync
tcp6 0 0 :::873 :::* LISTEN 16860/rsync

如果我們想要關閉服務,找到rsync服務的pid檔案,查看行程號,直接kill掉就可以,
[root@source etc]# cd /var/run/
[root@source run]# cat rsyncd.pid ##查看rsync服務的行程號
16860
[root@source run]# kill 16860 ##殺死行程,關閉服務
[root@source run]# netstat -ntap | grep rsync ##這時候就查看不到埠了
安裝httpd服務,并在站點目錄寫入一個首頁檔案,為了有apache的站點目錄,便于測驗用
[root@source run]# yum install httpd -y
[root@source run]# cd /var/www/html/
[root@source html]# vim index.html
[root@source html]# echo "<h1>this is test web</h1>" > index.html
1.4 測驗rsync功能
測驗的時候需要開啟另外一臺虛擬機,這臺虛擬機的IP地址應該與之前rsync組態檔中寫的在一個網段,否則不能同步,
rsync的命令
- -r:遞回模式,包含目錄及子目錄中的所有檔案,
- -l:對于符號鏈接檔案仍然復制為符號鏈接檔案,
- -v:顯示同步程序的詳細(verbose)資訊,
- -a:歸檔模式,保留檔案的權限、屬性等資訊,等同于組合選項“-rlptgoD”,
- -z:在傳輸檔案時進行壓縮(compress),
- -p:保留檔案的權限標記,
- -t:保留檔案的時間標記,
- -g:保留檔案的屬組標記(僅超級用戶使用),
- -o:保留檔案的屬主標記(僅超級用戶使用),
- -H:保留硬連接檔案,
- -A:保留 ACL 屬性資訊,
- -D:保留設備檔案及其他特殊檔案,
- –delete:洗掉目標位置有而原始位置沒有的檔案,
- –checksum:根據校驗和(而不是檔案大小、修改時間)來決定是否跳過檔案,
下行同步的三種方式
方式一:
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# rsync -zva sx@14.0.0.110::wwwroot /opt/ ##將wwwroot模塊中寫明的條目同步到/opt下
Password: ##輸入密碼
receiving incremental file list
./
index.html
sent 46 bytes received 133 bytes 51.14 bytes/sec
total size is 26 speedup is 0.15
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
index.html rh
[root@localhost opt]# cat index.html
<h1>this is test web</h1>
方式二:
在源服務器上新建一個檔案
[root@source html]# echo "<h1>this is yy web</h1>" > new.html
[root@source html]# ls
index.html new.html
[root@source html]# cat new.html
<h1>this is yy web</h1>
在另一臺服務器上進行同步
[root@localhost opt]# rsync -avz rsync://sx@14.0.0.110/wwwroot /opt/
Password:
receiving incremental file list
./
new.html
sent 46 bytes received 159 bytes 58.57 bytes/sec
total size is 50 speedup is 0.24
[root@localhost opt]# ls
index.html new.html rh
[root@localhost opt]# cat new.html
<h1>this is yy web</h1>
方式三:
在目標服務器上
[root@localhost opt]# rm -rf *.html
[root@localhost opt]# ls
rh
[root@localhost opt]# vim /etc/server.pass
123456 ##寫入授權用戶的密碼
[root@localhost opt]# chmod 600 /etc/server.pass
[root@localhost opt]# rsync -avz --delete --password-file=/etc/server.pass sx@14.0.0.110::wwwroot /opt/ ##免互動式同步,--delete表示洗掉目標位置有而原始位置沒有的檔案,--password-file指明剛剛寫的密碼檔案的位置
receiving incremental file list
deleting rh/
./
index.html
new.html
sent 65 bytes received 222 bytes 27.33 bytes/sec
total size is 50 speedup is 0.17
[root@localhost opt]# ls ##洗掉了原來的rh目錄,因為源服務器的目錄下沒有這個目錄
index.html new.html
[root@localhost opt]# cat index.html
<h1>this is test web</h1>
[root@localhost opt]# cat new.html
<h1>this is yy web</h1>
二、inotify+rsync 實時同步
Linux 內核從 2.6.13 版本開始提供了 inotify 通知介面,用來監控檔案系統的各種變化情
況,如檔案存取、洗掉、移動、修改等,利用這一機制,可以非常方便地實作檔案異動告警、增量備份,并針對目錄或檔案的變化及時作出回應,
將 inotify 機制與 rsync 工具相結合,可以實作觸發式備份(實時同步)——只要原始位 置的檔案發生變化,則立即啟動增量備份操作;否則處于靜默等待狀態,這樣,就避免了按固定周期備份時存在的延遲性、周期過密等問題,

2.1 配置 inotify+rsync 實時同步的步驟
-
調整 inotify 內核引數
在 Linux 內核中,默認的 inotify 機制提供了三個調控引數:max_queue_events、 max_user_instances、max_user_watches,分別表示監控事件佇列(16 384)、最多監控實體數(128)、每個實體最多監控檔案數(8192), -
安裝 inotify-tools
使用 inotify 機制還需要安裝 inotify-tools,以便提供 inotifywait、inotifywatch 輔助工具程式 ,用來監控、匯總改動情況 , inotify-tools 可從網站 https://github.com/rvoicilas/inotify-tools/releases 下載,版本為 3.14, -
撰寫觸發式同步腳本
使用 inotifywait 輸出的監控結果中,每行記錄中依次包括目錄、事件、檔案,據此可以 識別變動情況,為了簡單,只要檢測到變動時執行 rsync 上行同步操作即可,需要注意的是, 當更新較頻繁時,應避免并發執行 rsync 備份——若 rsync 行程已經存在,則忽略本次同步, 或者根據 rsync 行程數量(取決于實際任務)來決定是否同步, -
檢測inotify+rsync 實時同步
在本機運行/opt/inotify_rsync.sh 腳本程式,
切換到本機的/var/www/html 目錄,執行增加、洗掉、修改檔案等操作,
查看服務器中的/var/www/html 目錄下的變化情況,
2.2 inotify+rsync 實時同步實驗
1. 實驗之前將之前寫在站點的檔案全部洗掉
源服務器
[root@source opt]# cd /var/www/html/
[root@source html]# ls
index.html new.html
[root@source html]# rm -rf *
[root@source html]# ls
inotify+rsync發起端
[root@localhost opt]# yum install httpd -y
[root@localhost opt]# cd /var/www/html/
[root@localhost html]# ls ##保證這個目錄下為空,便于實作實時同步
[root@localhost html]# vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384 ##事件大小
fs.inotify.max_user_instances = 1024 ##實體數
fs.inotify.max_user_watches = 1048576 ##檔案數量
[root@localhost html]# sysctl -p ##加載組態檔
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
2. 手工編譯安裝inotify
將軟體包拷貝到當前目錄下
[root@localhost html]# tar zvxf inotify-tools-3.14.tar.gz -C /opt
[root@localhost html]# yum install gcc gcc-c++ make -y ##安裝環境包
[root@localhost html]# cd /opt/inotify-tools-3.14/
[root@localhost inotify-tools-3.14]# ./configure ##configure配置
[root@localhost inotify-tools-3.14]# make && make install ##編譯安裝
[root@localhost inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/ ##讓這個服務器端進入一個持續性監控的狀態,一旦進行了檔案的更改、創建、移動、洗掉,就會觸發實時同步
3. 對持續性監控的測驗
對inotify+rsync發起端服務器重新開啟一個視窗,進行操作
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# touch test.txt
[root@localhost html]# echo "hahaha" > test.txt
[root@localhost html]# rm -rf test.txt
在持續型監控的視窗可以看到以下的資訊
[root@localhost inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/
/var/www/html/ CREATE test.txt
/var/www/html/ MODIFY test.txt
/var/www/html/ DELETE test.txt
4. 撰寫腳本實作兩臺服務器的實時同步
inotify+rsync發起端服務器
[root@localhost html]# cd /opt
[root@localhost opt]# vim inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ sx@14.0.0.110::wwwroot/" ##將自己本地/var/www/html/的檔案同步到14.0.0.110服務器rsync組態檔的指定目錄下
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
[root@localhost opt]# chmod +x inotify.sh
因為腳本中牽扯著檔案的上傳,所以要修改組態檔中的read only記錄,將yes改為no
在源服務器
[root@source html]# vim /etc/rsyncd.conf
read only = no
[root@source html]# chmod 777 /var/www/html/ ##提升目錄權限,防止同步時候目錄權限不夠導致失敗
[root@source html]# ls ##現在這個目錄下為空,沒有檔案,一會執行腳本后,查看檔案有沒有實時同步過來

inotify+rsync發起端服務器
[root@localhost opt]# ls /var/www/html/
inotify-tools-3.14.tar.gz
[root@localhost opt]# ./inotify.sh ##執行腳本,進入持續性監聽狀態
但是這個時候在源服務器端/var/www/html目錄下仍然沒有將檔案同步過來,這是因為我們沒有對這個目錄下的檔案進行操作,沒有觸發實時同步,這時候我們在這個目錄下創建一個檔案,觸發實時同步
重新打開一個終端進行操作
[root@localhost html]# touch home.txt
[root@localhost html]# echo "實驗成功" > home.txt

這時候在源服務器的 /var/www/html/目錄下進行查看
[root@source html]# ls ##檔案都被同步過來
home.txt inotify-tools-3.14.tar.gz
[root@source html]# cat home.txt
實驗成功
查看同步檔案的屬主 、屬組,都為nobody
[root@source html]# ll
總用量 352
-rw-------. 1 nobody nobody 0 9月 12 18:56 home.txt
-rw-------. 1 nobody nobody 358772 9月 12 18:56 inotify-tools-3.14.tar.gz
屬主和屬組為nobody的原因是因為在之前rsync的組態檔中寫的
[root@source html]# vim /etc/rsyncd.conf

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/41650.html
標籤:其他
