結合nfs服務器很實用的rsync+inotify實時同步

簡述

在服務器中,通常會結合計劃任務shell腳本來執行本地備份,而rsync可用於異地備份,它同步的是目錄和文件,結合inotify,根據目錄或文件的變動及時同步。

言歸正傳,看一下rsync的概述

rsync( Remote Sync,遠程同步)是一個開源的快速備份工具,可以在不同主機之間鏡像同步整個目錄樹,支持增量備份,保持鏈接和權限,且採用優化的同步算法,傳輸前執行壓縮,因此非常適用於異地備份,鏡像服務器等應用。

rsync的官方網址是:https://rsync.samba.org/
目前最新的版本是3.1.3(還是18年更新的),不過真的很實用,由 Wayne Davison進行維護,作爲一種最常用的文件備份工具,rsync往往是 Linux和UNIX系統默認安裝的基本組件之一
在這裏插入圖片描述
可以使用rpm -q rsync查看版本

[root@bogon ~]# rpm -q rsync
rsync-3.0.9-17.el7.x86_64

不想要它的,也可以自己yum下載,也超級簡單,我的是阿里雲的Centos源和epel源,直接 yum -y install rsync

[root@bogon ~]# yum -y install rsync
[root@bogon ~]# rpm -q rsync
rsync-3.1.2-6.el7_6.1.x86_64
[root@bogon ~]# 

在遠程同步任務中,負責發起rsync同步操作的客戶機稱爲發起端,而負責響應來自客戶機的rsync同步操作的服務器稱爲同步源。在同步過程中,同步源負責提供文檔的原始位置,發起端應對該位置具有讀取權限

在這裏插入圖片描述
rsync作爲同步源時以守護進程運行,爲其他客戶機提供備份源,它的配置文件在/etc/rsyncd.conf,在做同步目錄的時候,可以通過修改配置文件,也可以直接通過命令實現,我們主要說第二種簡單的方式
配置文件例子:

 uid = nobody
 gid = nobody
 use chroot = yes	//禁錮在源目錄
 max connections = 4	//允許同時連接數:4
 address= 192.168.1.103	//監聽地址:192.168.1.103
 port 873	//監聽端口號:873
 pid file = /var/run/rsyncd.pid	//存放進程ID的文件位置
 log file = /var/log/rsyncd.log	//日誌文件存放位置
 hosts allow = 192.168.1.0/24	//允許訪問的客戶機地址
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress  = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

[wwwroot]	//共享模塊名稱

path = /var/www/html	//源目錄的實際路徑
read only = yes                         //是否爲只讀
dont compress  = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2  //同步時不再壓縮的文件類型
auth users = backuper                  //授權賬戶
secrets file = /etc/rsyncd_users.db	//存放賬戶信息的數據文件

執行rsync --daemon命令就可以啓動 rsync服務,以獨立監聽服務的方式運行,若要關閉 rsync服務,可以採取kill進程的方式,如 kill $(cat/var/run/ rsyncd.pid)

rsync的命令

備份的基本格式爲“ rsync[選項]原始位置 目標位置”,其中常用的一些命令選項如下所示,具體應根據實際需求選擇(如-avz)

-r :遞歸模式,包含目錄及子目錄中的所有文件。
-l :對於符號鏈接文件仍然複製爲符號鏈接文件。
-v :顯示同步過程的詳細( verbose)信息。
-a :歸檔模式,保留文件的權限、屬性等信息,等同於組合選項 “-rlptgoD”
-z :在傳輸文件時進行壓縮( compress)。
-p :保留文件的權限標記。
-t :保留文件的時間標記。
-g :保留文件的屬組標記(僅超級用戶使用)。
-o :保留文件的屬主標記(僅超級用戶使用)。
-H :保留硬連接文件。
-A :保留ACL屬性信息。
-D :保留設備文件及其他特殊文件。
--delete:刪除目標位置有而原始位置沒有的文件。
--checksum:根據校驗和(而不是文件大小、修改時間)來決定是否跳過文件。

配置源的表示方法

rsync -avz --delete 用戶名@主機地址:共享目錄 本地目錄

現在模擬一下遠程同步實驗

建立發佈的共享目錄和文件

[root@bogon ~]# mkdir /rsync
[root@bogon ~]# touch /rsync/file{1..10}
[root@bogon ~]# ll /rsync/
total 0
-rw-r--r--. 1 root root 0 Apr 28 12:12 file1
-rw-r--r--. 1 root root 0 Apr 28 12:12 file10
-rw-r--r--. 1 root root 0 Apr 28 12:12 file2
-rw-r--r--. 1 root root 0 Apr 28 12:12 file3
-rw-r--r--. 1 root root 0 Apr 28 12:12 file4
-rw-r--r--. 1 root root 0 Apr 28 12:12 file5
-rw-r--r--. 1 root root 0 Apr 28 12:12 file6
-rw-r--r--. 1 root root 0 Apr 28 12:12 file7
-rw-r--r--. 1 root root 0 Apr 28 12:12 file8
-rw-r--r--. 1 root root 0 Apr 28 12:12 file9
[root@bogon ~]# 

客戶端同步操作
要注意:如果寫的是192.168.1.103:/rsync 表示的是整個目錄同步過去
換成192.168.1.103:/rsync/ 多加一個/,就會表示成這個目錄裏的所有東西

[root@slave ~]# rsync -avz --delete [email protected]:/rsync /var/www/html/
The authenticity of host '192.168.1.103 (192.168.1.103)' can't be established.
ECDSA key fingerprint is 4f:58:1b:4f:30:a5:40:89:34:b3:b7:ab:72:40:df:4d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.103' (ECDSA) to the list of known hosts.
[email protected]'s password:                  
receiving incremental file list
rsync/
rsync/file1
rsync/file10
rsync/file2
rsync/file3
rsync/file4
rsync/file5
rsync/file6
rsync/file7
rsync/file8
rsync/file9

sent 218 bytes  received 548 bytes  139.27 bytes/sec
total size is 0  speedup is 0.00
[root@slave ~]# 
[root@slave ~]# ll /var/www/html/
總用量 0
drwxr-xr-x. 2 root root 137 4月  28 2020 rsync
[root@slave html]# cd rsync/
[root@slave rsync]# ll
總用量 0
-rw-r--r--. 1 root root 0 4月  28 2020 file1
-rw-r--r--. 1 root root 0 4月  28 2020 file10
-rw-r--r--. 1 root root 0 4月  28 2020 file2
-rw-r--r--. 1 root root 0 4月  28 2020 file3
-rw-r--r--. 1 root root 0 4月  28 2020 file4
-rw-r--r--. 1 root root 0 4月  28 2020 file5
-rw-r--r--. 1 root root 0 4月  28 2020 file6
-rw-r--r--. 1 root root 0 4月  28 2020 file7
-rw-r--r--. 1 root root 0 4月  28 2020 file8
-rw-r--r--. 1 root root 0 4月  28 2020 file9
[root@bogon rsync]# 

由於rsync基於底層ssh實現,所以在傳輸時可以提前生成密鑰對,實現免密傳輸

在這裏插入圖片描述

[root@slave html]# rsync -avz --delete [email protected]:/rsync2/ /var/www/html/ 
receiving incremental file list
deleting rsync2/linux5
deleting rsync2/linux4
deleting rsync2/linux3
deleting rsync2/linux2
deleting rsync2/linux1
deleting rsync2/
deleting rsync/file9
deleting rsync/file8
deleting rsync/file7
deleting rsync/file6
deleting rsync/file5
deleting rsync/file4
deleting rsync/file3
deleting rsync/file2
deleting rsync/file10
deleting rsync/file1
deleting rsync/
./
linux1
linux2
linux3
linux4
linux5

sent 122 bytes  received 300 bytes  844.00 bytes/sec
total size is 0  speedup is 0.00
[root@slave html]# cd /var/www/html/
[root@slave html]# ls
linux1  linux2  linux3  linux4  linux5

配置 inotify+ rsync實時同步

將 inotify機制與 rsync工具相結合,可以實現觸發式備份(實時同步)——只要原始位置的文檔發生變化,則立即啓動增量備份操作,否則處於靜默等待狀態

在這裏插入圖片描述
使用 inotify機制還需要安裝 inotify-tools(yum安裝爲例),以便提供 inotifywait、inotifywatch輔助工具程序,用來監控、彙總改動情況
yum -y install inotify-tools

以監控網站目錄/var/www/html爲例,可以先執行 inotifywait'命令,然後在另一個終端向/var/www/html目錄下添加文件、移動文件,跟蹤屏幕輸出結果

在這裏插入圖片描述
刪除後,立馬會檢測到

[root@bogon ~]# inotifywait -mrq -e modify,create,move,delete /var/www/html   #監控變動情況
/var/www/html/ DELETE haha5

參數解釋:

-e:指定要監控那些事件
-m:持續監控狀態
-r:遞歸目錄
-q:簡潔輸出信息
這樣做雖然是監控到了,但是還需要手動做好計劃任務才能定時更新數據,不能追蹤實時進行同步數據,所以纔會引用到inotify觸發式同步。

inotifywait可監控 modify(修改)、create(創建)、move(移動)、delete(刪除) , attrib(屬性更改)等各種事件,一有變動立即輸出結果,而 inotifywatch可用來收集文件系統變動情況,並在運行結束後輸出彙總的變化情況。

編寫觸發式同步腳本

腳本含義:一直檢測/var/www/html/,只要此目錄發生變化,就會推送數據給rsync服務器

#!/bin/bash
#2020年4月27日15:28:39
inotifywait -mrq -e create,delete,modify,move /var/www/html/ | while read aaa  #將檢測到的結果賦值給變量aaa,當成功後開始循環do...done內容
do
        rsync -avz --delete /var/www/html [email protected]:/rsync3
done

賦權:chmod +x rsync.sh

[root@slave ~]# sh rsync.sh &   #這裏&表示放到後臺執行
[1] 50129
[root@slave ~]# sending incremental file list
deleting html/file5
deleting html/file4
html/

在這期間在另一個slave終端刪除操作

[root@slave ~]# cd /var/www/html/
[root@slave html]# ls
file1  file2  file3  file4  file5  haha1  haha2  haha3  haha4
[root@slave html]# rm -rf file5 file4
[root@slave html]#

在服務端查看是否同步過去
在這裏插入圖片描述
下面內容爲後期添加的,測試了一下rsync的增量備份功能,但是,inotify同步時還是需要把整個目錄先同步過去,若同步數據非常大的時候,延時很慢,如果頻繁更改同步,rsync服務器有宕機的風險呦

[root@rsync html]# vim 5.txt 
[root@rsync html]# rsync -avz --checksum /var/www/html/ [email protected]:/nfs
[email protected]'s password: 
sending incremental file list
./
5.txt
sent 152 bytes  received 41 bytes  55.14 bytes/sec
total size is 18  speedup is 0.09

########################################################
[root@rsync html]# vim 5.txt 
[root@rsync html]# rsync -avz --checksum /var/www/html/ [email protected]:/nfs
[email protected]'s password: 
sending incremental file list
./
5.txt
sent 157 bytes  received 47 bytes  37.09 bytes/sec
total size is 34  speedup is 0.17

#########################################################
[root@rsync html]# vim 5.txt 
[root@rsync html]# rsync -avz --delete /var/www/html/ [email protected]:/nfs
[email protected]'s password: 
sending incremental file list
./
5.txt
sent 133 bytes  received 47 bytes  51.43 bytes/sec
total size is 44  speedup is 0.24

在這裏插入圖片描述

你們學廢了嗎???
後面會更新rsync+sersync數據同步,彌補inotify的缺點,同步的時候不用在同步整個目錄,而是針對變化的目錄

最後,先關注一下吧,怕你們以後找不到我啦
rsync+sersync會陸續更新在主頁呦,記得查收!!!
在這裏插入圖片描述

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