Linux利用inotify和rsync服務(wù)實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)同步的原理解析
目錄
- 文件定時(shí)同步的實(shí)現(xiàn):
- 文件實(shí)時(shí)同步的實(shí)現(xiàn):
- inotify
- inotify-tools包主要工具:
- inotifywait 命令:
- rsync工具
- rsync有三種工作方式:
- 兩種方式實(shí)現(xiàn)rsync服務(wù)器
- 方式一:通過(guò)rsync守護(hù)進(jìn)程的方式實(shí)現(xiàn)rsync服務(wù)
- 以獨(dú)立服務(wù)方式運(yùn)行rsync并實(shí)現(xiàn)驗(yàn)證功能
- 工作原理:
- inotify+rsync+shell 腳本實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)同步
文件定時(shí)同步的實(shí)現(xiàn):
利用rsync結(jié)合cron計(jì)劃任務(wù)實(shí)現(xiàn):
rsync -av --delete /data/ 10.0.0.12:/back -a:保留文件屬性 -v:顯示過(guò)程 -delete:如果源文件沒(méi)有的,目標(biāo)文件里面有,就把目標(biāo)文件里面的刪除掉
文件實(shí)時(shí)同步的實(shí)現(xiàn):
前提:
文件發(fā)生發(fā)生變化的時(shí)候就觸發(fā)同步,但是觸發(fā)同步需要一個(gè)依賴文件狀態(tài)變化的功能。
inotify
inotify是系統(tǒng)內(nèi)核的一個(gè)監(jiān)控服務(wù),屬于操作系統(tǒng)內(nèi)核的一個(gè)特有機(jī)制,用于監(jiān)控文件的信息變化。
查看內(nèi)核是否支持inotify;
[root@LAP1 data]# ls -l /proc/sys/fs/inotify ls: cannot access " ": No such file or directory /proc/sys/fs/inotify: total 0 -rw-r--r-- 1 root root 0 Oct 24 23:39 max_queued_events -rw-r--r-- 1 root root 0 Oct 24 23:39 max_user_instances -rw-r--r-- 1 root root 0 Oct 24 23:39 max_user_watches
inotify內(nèi)核參數(shù):
max_queued_events:inotify 事件隊(duì)列最大長(zhǎng)度,如值太小會(huì)出現(xiàn) Event Queue Overflow 錯(cuò)誤,默認(rèn)值:16384, 生產(chǎn)環(huán)境建議調(diào)大,比如:327679 max_user_instances:每個(gè)用戶創(chuàng)建inotify實(shí)例最大值,默認(rèn)值:128 max_user_watches:可以監(jiān)視的文件的總數(shù)量(inotifywait 單進(jìn)程),默認(rèn)值:8192,建議調(diào)大
說(shuō)明:
proc里面的參數(shù)可以通過(guò)sysctl工具來(lái)進(jìn)行更改。
inotify-tools包主要工具:
- inotifywait: 在被監(jiān)控的文件或目錄上等待特定文件系統(tǒng)事件(open ,close,delete等)發(fā)生,常用于實(shí)時(shí)同步的目錄監(jiān)控(主要使用的就是這個(gè)工具)
- inotifywatch:收集被監(jiān)控的文件系統(tǒng)使用的統(tǒng)計(jì)數(shù)據(jù),指文件系統(tǒng)事件發(fā)生的次數(shù)統(tǒng)計(jì)
inotifywait 命令:
inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
選項(xiàng):
-m, --monitor 始終保持事件監(jiān)聽(tīng) -d, --daemon 以守護(hù)進(jìn)程方式執(zhí)行,和-m相似,配合-o使用 -r, --recursive 遞歸監(jiān)控目錄數(shù)據(jù)信息變化 -q, --quiet 輸出少量事件信息 --exclude <pattern> 指定排除文件或目錄,使用擴(kuò)展的正則表達(dá)式匹配的模式實(shí)現(xiàn) --excludei <pattern> 和exclude相似,不區(qū)分大小寫(xiě) -o, --outfile <file> 打印事件存到文件中,注意:使用絕對(duì)路徑 -s, --syslogOutput 發(fā)送錯(cuò)誤到syslog相當(dāng)于標(biāo)準(zhǔn)錯(cuò)誤輸出 --timefmt <fmt> 指定時(shí)間輸出格式 --format <fmt> 定義輸出格式;即實(shí)際監(jiān)控輸出內(nèi)容 -e 指定監(jiān)聽(tīng)指定的事件,如果省略,表示所有事件都進(jìn)行監(jiān)聽(tīng)
例如:
# 10.0.0.11 [root@LAP1 data]# cat file1 [root@LAP1 data]# echo hello > file1 [root@LAP1 data]# ll file1 -rw-r--r-- 1 root root 6 Oct 24 23:50 file1 [root@LAP1 data]# chmod 666 # 10.0.0.11 [root@LAP1 data]# inotifywait -m file1 Setting up watches. Watches established. file1 OPEN file1 CLOSE_NOWRITE,CLOSE file1 MODIFY file1 OPEN file1 MODIFY file1 CLOSE_WRITE,CLOSE file1 ATTRIB
inotifywait -e 選項(xiàng)指定的事件類型
create #文件或目錄創(chuàng)建 delete #文件或目錄被刪除 modify #文件或目錄內(nèi)容被寫(xiě)入 attrib #文件或目錄屬性改變 close_write #文件或目錄關(guān)閉,在寫(xiě)入模式打開(kāi)之后關(guān)閉的 close_nowrite #文件或目錄關(guān)閉,在只讀模式打開(kāi)之后關(guān)閉的 close #文件或目錄關(guān)閉,不管讀或是寫(xiě)模式 open #文件或目錄被打開(kāi) lsdir #瀏覽目錄內(nèi)容 moved_to #文件或目錄被移動(dòng)到監(jiān)控的目錄中 moved_from #文件或目錄從監(jiān)控的目錄中被移動(dòng) move #文件或目錄不管移動(dòng)到或是移出監(jiān)控目錄都觸發(fā)事件 access #文件或目錄內(nèi)容被讀取 delete_self #文件或目錄被刪除,目錄本身被刪除 unmount #取消掛載
inotifywait 的--timefmt 時(shí)間格式
%Y #年份信息,包含世紀(jì)信息 %y #年份信息,不包括世紀(jì)信息 %m #顯示月份,范圍 01-12 %d #每月的第幾天,范圍是 01-31 %H #小時(shí)信息,使用 24小時(shí)制,范圍 00-23 %M #分鐘,范圍 00-59 %S #秒,范例 0-60
inotifywait 的 --format 格式定義
%T #輸出時(shí)間格式中定義的時(shí)間格式信息,通過(guò) --timefmt option 語(yǔ)法格式指定時(shí)間信息 %w #事件出現(xiàn)時(shí),監(jiān)控的文件或目錄的名稱信息,相當(dāng)于dirname %f #事件出現(xiàn)時(shí),將顯示監(jiān)控目錄下觸發(fā)事件的文件或目錄信息,否則為空,相當(dāng)于basename %e #顯示發(fā)生的事件信息,不同的事件默認(rèn)用逗號(hào)分隔 %Xe #顯示發(fā)生的事件信息,不同的事件指定用X進(jìn)行分隔
例如:監(jiān)控/data/目錄的變化
[root@CentOS8 data]# inotifywait -m --timefmt "%Y-%m-%d %H:%M:%S" --format="%T %w---%f event: %;e" /data Setting up watches. Watches established. 2022-10-24 17:12:57 /data/--- event: OPEN;ISDIR 2022-10-24 17:12:57 /data/--- event: ACCESS;ISDIR 2022-10-24 17:12:57 /data/--- event: CLOSE_NOWRITE;CLOSE;ISDIR 2022-10-24 17:13:06 /data/---file3 event: CREATE 2022-10-24 17:13:06 /data/---file3 event: OPEN 2022-10-24 17:13:06 /data/---file3 event: ATTRIB 2022-10-24 17:13:06 /data/---file3 event: CLOSE_WRITE;CLOSE
rsync工具
rsync有三種工作方式:
- 本地模式:本地文件系統(tǒng)上實(shí)現(xiàn)同步。命令行語(yǔ)法格式為上述"Local"段的格式
- 基于傳統(tǒng)的ssh協(xié)議,本地主機(jī)使用遠(yuǎn)程shell和遠(yuǎn)程主機(jī)通信
- 作為一個(gè)獨(dú)立服務(wù),本地主機(jī)通過(guò)網(wǎng)絡(luò)套接字連接遠(yuǎn)程主機(jī)上的rsync daemon
區(qū)別:
前兩者的本質(zhì)是通過(guò)本地或遠(yuǎn)程shell,而第3種方式則是讓遠(yuǎn)程主機(jī)上運(yùn)行rsyncd服務(wù),使其監(jiān)聽(tīng)在一個(gè)端口上,等待客戶端的連接。
本地模式:
rsync [OPTION...] SRC... [DEST]
例如:
[root@LAP1 data]# rsync file1 file111 [root@LAP1 data]# ls file1 file11 file111 file2
基于傳統(tǒng)的ssh協(xié)議使用格式:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
例如:
rsync -av --delete /data/ 10.0.0.12:/back #不寫(xiě)用戶名默認(rèn)使用的就是當(dāng)前主機(jī)使用的用戶
作為一個(gè)獨(dú)立服務(wù):
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST] #協(xié)議的形式訪問(wèn),效果等同于上面 Push: rsync [OPTION...] SRC... [USER@]HOST::DEST rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
rsync常用選項(xiàng):
-v:顯示rsync過(guò)程中詳細(xì)信息??梢允褂?-vvvv"獲取更詳細(xì)信息。 -a --archive :歸檔模式,表示遞歸傳輸并保持文件屬性。 -t --times:保持mtime屬性。強(qiáng)烈建議任何時(shí)候都加上"-t",否則目標(biāo)文件mtime會(huì)設(shè)置為系統(tǒng)時(shí)間,導(dǎo)致下次更新,檢查出mtime不同從而導(dǎo)致增量傳輸無(wú)效 --delete :以SRC為主,對(duì)DEST進(jìn)行同步。多則刪之,少則補(bǔ)之
rsync的軟件包:
官方網(wǎng)站: http://rsync.samba.org/
軟件包:rsync,rsync-daemon(CentOS 8)
服務(wù)文件:/usr/lib/systemd/system/rsyncd.service
配置文件:/etc/rsyncd.conf
端口:873/tcp
兩種方式實(shí)現(xiàn)rsync服務(wù)器
rsync即可以作為服務(wù)器端,也可以作為客戶端程序。
方式一:通過(guò)rsync守護(hù)進(jìn)程的方式實(shí)現(xiàn)rsync服務(wù)
#在備份服務(wù)器啟動(dòng) rsync 進(jìn)程 [root@bakup_server ~]# rsync --daemon #--daemon選項(xiàng)表示啟動(dòng)為守護(hù)進(jìn)程 Failed to parse config file: /etc/rsyncd.conf #必須要有這個(gè)配置文件才能啟動(dòng)成功 [root@bakup_server ~]# touch /etc/rsyncd.conf #需要?jiǎng)?chuàng)建這個(gè)配置文件才能正常啟動(dòng) [root@bakup_server ~]# rsync --daemon #啟動(dòng)rsync守護(hù)進(jìn)程 [root@bakup_server ~]# ss -ntl #守護(hù)進(jìn)程啟動(dòng)后會(huì)監(jiān)聽(tīng)873端口 StateRecv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 5 0.0.0.0:873 0.0.0.0:* #設(shè)置rsync服務(wù)器的共享信息 [root@bakup_server ~]# cat /etc/rsyncd.conf #等號(hào)之間可以有空格 [backup] #定義存放數(shù)據(jù)共享的名字 path = /bakup #真實(shí)的路徑,存放共享文件的路徑 (利用rsync將這個(gè)目錄共享出去,共享出去的名字叫做bakup) read only = no #指定可讀寫(xiě),默認(rèn)只讀 [root@bakup_server ~]# rsync --daemon #更改問(wèn)配置文件以后需要重新開(kāi)啟守護(hù)進(jìn)程才會(huì)生效 [root@bakup_server ~]# setfacl -m u:nobody:rwx /bakup/ #指定目錄給nobody權(quán)限,默認(rèn)用戶以nobody訪問(wèn)此目錄 使用客戶端連接rsync備份服務(wù)器: #格式 rsync rsync://host 或者 rsync host:: 客戶端查看服務(wù)器的情況: [root@data_server ~]# rsync rsync://10.0.0.12 #以協(xié)議的形式訪問(wèn) backup #共享出來(lái)的名字 [root@data_server ~]# rsync 10.0.0.12:: #以服務(wù)的形式訪問(wèn) backup #實(shí)現(xiàn)客戶端將文件拷貝到rsync共享的目錄中 注意:傳輸?shù)臅r(shí)候不管以誰(shuí)的身份,都會(huì)映射為nobody,所以不用寫(xiě)用戶名都行 [root@data_server ~]# rsync /root/anaconda-ks.cfg [email protected]::backup [root@bakup_server bakup]# ll total 8 -rw------- 1 nobody nobody 1526 Oct 24 17:48 anaconda-ks.cfg -rw-r--r-- 1 nobody nobody 658 Oct 24 17:49 fstab
以獨(dú)立服務(wù)方式運(yùn)行rsync并實(shí)現(xiàn)驗(yàn)證功能
rsync-daemon:安裝這個(gè)軟件包以后會(huì)提供一個(gè)service服務(wù),它會(huì)監(jiān)聽(tīng)自己的獨(dú)立端口
[root@bakup_server ~]# yum install rsync-daemon Installed: rsync-daemon-3.1.3-9.el8.noarch [root@bakup_server ~]# systemctl enable rsyncd.service Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
說(shuō)明:
rsync默認(rèn)傳輸文件的時(shí)候不需要驗(yàn)證
啟用rsync的驗(yàn)證功能的方法:
[root@backup-centos8 ~]#dnf -y install rsync-daemon #安裝服務(wù)的時(shí)候自動(dòng)創(chuàng)建rsync服務(wù)器的配置文件 [root@centos8 ~]#vi /etc/rsyncd.conf uid = root #遠(yuǎn)程用戶映射到本機(jī)的用戶,默認(rèn)為nobody 指定以哪個(gè)用戶來(lái)訪問(wèn)共享目錄,將之指定為生成的文件所有者,默認(rèn)為nobody gid = root #默認(rèn)為nobody #port = 874 #可指定非標(biāo)準(zhǔn)端口,默認(rèn)873/tcp #use chroot = no max connections = 0 #不限制最大連接數(shù) ignore errors #如果有些錯(cuò)誤,就跳過(guò)這些錯(cuò)誤 exclude = lost+found/ #跳過(guò)指定的目錄,不去復(fù)制 log file = /var/log/rsyncd.log # 日志所在位置 pid file = /var/run/rsyncd.pid # 存放進(jìn)程的pid文件 lock file = /var/run/rsyncd.lock # 存放鎖文件 reverse lookup = no # 拒絕反向解析,不把ip解析為主機(jī)名 #hosts allow = 10.0.0.0/24 # 允許連接的主機(jī) [backup]#每個(gè)模塊名對(duì)應(yīng)一個(gè)不同的path目錄,如果同名后面模塊生效 共享名 path = /data/backup/ #共享的真實(shí)路徑 comment = backup dir #描述信息 read only = no #默認(rèn)是yes,即只讀 auth users = rsyncuser #默認(rèn)anonymous可以訪問(wèn)rsync服務(wù)器 用于驗(yàn)證的賬號(hào),只有這個(gè)賬號(hào)才能去訪問(wèn) secrets file = /etc/rsync.pas #存放密碼的文件 格式: 用戶名: 密碼
例如:實(shí)現(xiàn)密碼驗(yàn)證
[root@CentOS8 ~]# yum install rsync-daemon [root@CentOS8 ~]# systemctl enable rsyncd --now [root@CentOS8 ~]# cat /etc/rsyncd.conf uid = root gid = root max connections = 0 ignore errors exclude = lost+found/ log file = /var/log/rsyncd.log pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock reverse lookup = no [backup] path = /data/backup/ comment = backup dir read only = no auth users = tom secrets file = /etc/rsync.pas [root@CentOS8 ~]# systemctl restart rsyncd.service [root@CentOS8 ~]# mkdir /data/backup/ -p [root@CentOS8 ~]# echo "tom:redhat" > /etc/rsync.pas [root@CentOS8 ~]# chmod 600 /etc/rsync.pas #必須要修改密碼文件權(quán)限,不然客戶端訪問(wèn)的時(shí)候會(huì)提示密碼錯(cuò)誤 客戶端訪問(wèn): [root@CentOS8 ~]# rsync /etc/fstab [email protected]::backup Password: #交互輸入tom的密碼 或者: [root@CentOS8 ~]# rsync /root/anaconda-ks.cfg rsync://[email protected]/backup Password: 注意:不指定用戶名默認(rèn)就是當(dāng)前系統(tǒng)的用戶
可以提前建立一個(gè)文件,將密碼放在文件中
#非交互式查看共享目錄 [root@CentOS8 ~]# echo "redhat" >/etc/rsync.pas #客戶端存放rsync的密碼信息 [root@CentOS8 ~]# chmod 600 /etc/rsync.pas #密碼文件權(quán)限修改(必須,不然會(huì)報(bào)錯(cuò)) 測(cè)試: [root@CentOS8 ~]# rsync --password-file=/etc/rsync.pas /root/file111 rsync://[email protected]/backup
inotify+rsync+shell 腳本實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)同步
工作原理:
- 要利用監(jiān)控服務(wù)(inotify),監(jiān)控同步數(shù)據(jù)服務(wù)器目錄中信息的變化
- 發(fā)現(xiàn)目錄中數(shù)據(jù)產(chǎn)生變化,就利用rsync服務(wù)推送到備份服務(wù)器上
范例:inotify+rsync+shell 腳本實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)同步
數(shù)據(jù)服務(wù)器:存放數(shù)據(jù)信息的服務(wù)器 10.0.0.11 備份服務(wù)器:存放備份信息的服務(wù)器 10.0.0.12
思路:利用inotidy監(jiān)控事件是否發(fā)生變化,
[root@CentOS8 ~]# cat inotify_rsync.sh #!/bin/bash SRC="/data/www/" #需要同步的目錄文件 #注意最后的/ DEST="[email protected]::backup" #同步到備份數(shù)據(jù)器的指定位置 rpm -q rsync &> /dev/null || yum -y install rsync #如果不存在rsync這個(gè)工具就安裝它 inotifywait -mrq --exclude=".*\.swp" --timefmt "%Y-%m-%d %H:%M:%S" --format "%T %w %f" -e create,delete,moved_to,close_write,attrib ${SRC} | while read DATE TIME DIR FILE; do FILEPATH=${DIR}${FILE} #需要同步的文件 rsync -az --delete --password-file=/etc/rsync.pas $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log done #通過(guò)DATE TIME DIR FILE 這四個(gè)變量記錄發(fā)生的變化 日期 事件 目錄 文件
到此這篇關(guān)于Linux利用inotify和rsync服務(wù)實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)同步的文章就介紹到這了,更多相關(guān)Linux數(shù)據(jù)實(shí)時(shí)同步內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
