inotify+rsync實(shí)現(xiàn)實(shí)時(shí)同步的方法
1.1 什么是實(shí)時(shí)同步:如何實(shí)現(xiàn)實(shí)時(shí)同步
要利用監(jiān)控服務(wù)(inotify),監(jiān)控同步數(shù)據(jù)服務(wù)器目錄中信息的變化
發(fā)現(xiàn)目錄中數(shù)據(jù)產(chǎn)生變化,就利用rsync服務(wù)推送到備份服務(wù)器上
1.2 實(shí)現(xiàn)實(shí)時(shí)同步的方法
inotify+rsync 方式實(shí)現(xiàn)數(shù)據(jù)同步
sersync 方式實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)同步 詳情參照:sersync實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)同步的方法
1.2.1 實(shí)時(shí)同步原理介紹
1.3 inotify+rsync 方式實(shí)現(xiàn)數(shù)據(jù)同步
1.3.1 Inotify簡(jiǎn)介
Inotify是一種強(qiáng)大的,細(xì)粒度的。異步的文件系統(tǒng)事件監(jiān)控機(jī)制,linux內(nèi)核從2.6.13起,加入了 Inotify支持,通過Inotify可以監(jiān)控文件系統(tǒng)中添加、刪除,修改、移動(dòng)等各種事件,利用這個(gè)內(nèi)核接口,第三方軟件就可以監(jiān)控文件系統(tǒng)下文件的各種變化情況,而 inotify-tools 正是實(shí)施這樣監(jiān)控的軟件。國(guó)人周洋在金山公司也開發(fā)了類似的實(shí)時(shí)同步軟件sersync。
提示信息:
sersync軟件實(shí)際上就是在 inotify軟件基礎(chǔ)上進(jìn)行開發(fā)的,功能要更加強(qiáng)大些 ,多了定時(shí)重傳機(jī)制,過濾機(jī)制了提供接口做 CDN,支持多線程橾作。
Inotify實(shí)際是一種事件驅(qū)動(dòng)機(jī)制,它為應(yīng)用程序監(jiān)控文件系統(tǒng)事件提供了實(shí)時(shí)響應(yīng)事件的機(jī)制,而無(wú)須通過諸如cron等的輪詢機(jī)制來獲取事件。cron等機(jī)制不僅無(wú)法做到實(shí)時(shí)性,而且消耗大量系統(tǒng)資源。相比之下,inotify基于事件驅(qū)動(dòng),可以做到對(duì)事件處理的實(shí)時(shí)響應(yīng),也沒有輪詢?cè)斐傻南到y(tǒng)資源消耗,是非常自然的事件通知接口,也與自然世界事件機(jī)制相符合。
inotify的實(shí)現(xiàn)有幾款軟件:
inotify-tools,sersync,lrsyncd
1.3.2 inotify+rsync使用方式
inotify 對(duì)同步數(shù)據(jù)目錄信息的監(jiān)控
rsync 完成對(duì)數(shù)據(jù)信息的實(shí)時(shí)同步
利用腳本進(jìn)行結(jié)合
1.4 部署inotify軟件的前提
需要2.6.13以后內(nèi)核版本才能支持inotify軟件。2.6.13內(nèi)核之后版本,在沒有安裝inotify軟件之前,應(yīng)該有這三個(gè)文件。
[root@backup ~]# ll /proc/sys/fs/inotify/ total 0 -rw-r--r-- 1 root root 0 Oct 17 10:12 max_queued_events -rw-r--r-- 1 root root 0 Oct 17 10:12 max_user_instances -rw-r--r-- 1 root root 0 Oct 17 10:12 max_user_watches
1.4.1 三個(gè)重要文件的說明
文件
|
默認(rèn)值
|
作用說明
|
max_user_watches
|
8192
|
設(shè)置inotifywait或inotifywatch命令可以監(jiān)視的文件數(shù)量(單進(jìn)程)
|
max_user_instances
|
128
|
設(shè)置每個(gè)用戶可以運(yùn)行的inotifywait或inotifywatch命令的進(jìn)程數(shù)
|
max_queued_events
|
16384
|
設(shè)置inotify實(shí)例事件(event)隊(duì)列可容納的事件數(shù)量
|
1.4.3 【官方說明】三個(gè)重要文件1.4.2 【服務(wù)優(yōu)化】可以將三個(gè)文件的數(shù)值調(diào)大,監(jiān)聽更大的范圍
[root@nfs01 ~]# man proc /proc/sys/fs/inotify (since Linux 2.6.13) This directory contains files max_queued_events, max_user_instances, and max_user_watches, that can be used to limit the amount of kernel memory consumed by the inotify interface. for further details, see inotify(7).
通過man手冊(cè)的第7級(jí)別中查到 inotify的默認(rèn)文件的詳細(xì)說明。
[root@nfs01 ~]# man 7 inotify /proc/sys/fs/inotify/max_queued_events The value in this file is used when an application calls inotify_init(2) to set an upper limit on the number of events that can be queued to the corresponding inotify instance. Events in excess of this limit are dropped, but an IN_Q_OVERFLOW event is always generated. /proc/sys/fs/inotify/max_user_instances This specifies an upper limit on the number of inotify instances that can be created per real user ID. /proc/sys/fs/inotify/max_user_watches This specifies an upper limit on the number of watches that can be created per real user ID.
1.5 inotify軟件介紹及參數(shù)說明
1.5.1 兩種安裝方式
1) yum install -y inotify-tools
2) 手工編譯安裝
注:
YUM 安裝需要有epel源
http://mirrors.aliyun.com
手工編譯安裝方式需要到github上進(jìn)行下載軟件包
inotify軟件的參考資料鏈接:
https://github.com/rvoicilas/inotify-tools/wiki
1.5.2 inotify主要安裝的兩個(gè)軟件
inotifywait: (主要)
在被監(jiān)控的文件或目錄上等待特定文件系統(tǒng)事件(open close delete等)發(fā)生,執(zhí)行后處于阻塞狀態(tài),適合在shell腳本中使用
inotifywatch:
收集被監(jiān)控的文件系統(tǒng)使用的統(tǒng)計(jì)數(shù)據(jù),指文件系統(tǒng)事件發(fā)生的次數(shù)統(tǒng)計(jì)。
說明:在實(shí)時(shí)實(shí)時(shí)同步的時(shí)候,主要是利用inotifywait對(duì)目錄進(jìn)行監(jiān)控
1.5.3 inotifywait命令參數(shù)說明
參數(shù)
|
含義
|
-m, --monitor
(重要參數(shù))
|
Keep listening for events forever. Without this option, inotifywait will exit after one event is received.
始終保持事件監(jiān)聽。
|
-d, --daemon
|
111
|
-r, --recursive
(重要參數(shù))
|
Watch all subdirectories of any directories passed as arguments.
遞歸監(jiān)控目錄數(shù)據(jù)信息變化
|
-o, --outfile <file>
|
Print events to <file> rather than stdout.
打印事件到文件中,相當(dāng)于標(biāo)準(zhǔn)正確輸出
|
-s, --syslog
|
Output errors to syslog(3) system log module rather than stderr.
發(fā)送錯(cuò)誤到syslog相當(dāng)于標(biāo)準(zhǔn)錯(cuò)誤輸出
|
-q, --quiet
(重要參數(shù))
|
If specified once, the program will be less verbose. Specifically, it will not state when it has completed establishing all inotify watches.
輸出信息少(只打印事件信息)
|
--exclude <pattern>
|
Exclude all events on files matching the extended regular expression <pattern>.
排除文件或目錄
|
--excludei <pattern>
|
Like --exclude but case insensitive.
排除文件或目錄時(shí),不區(qū)分大小寫
|
--timefmt <fmt>
(重要參數(shù))
|
Print using a specified printf-like format string; read the man page for more details.
指定時(shí)間輸出格式
|
--format <fmt>
(重要參數(shù))
|
Print using a specified printf-like formatstring; read the man page for more details.
打印使用指定的輸出類似格式字符串;即實(shí)際監(jiān)控輸出內(nèi)容
|
-e
(重要參數(shù))
|
Listen for specific event(s). If omitted, all events are listened for.
指定監(jiān)聽指定的事件,如果省略,表示所有事件都進(jìn)行監(jiān)聽
|
以上的信息可以通過 inotifywait --help 獲得
|
事件名稱
|
事件說明
|
access
|
file or directory contents were read
文件或目錄內(nèi)容被讀取
|
modify
|
file or directory contents were writterv
文件或目錄內(nèi)容被寫入
|
attrib
|
file or directory attributes changed
文件或目錄屬性改變
|
close_write
(重要參數(shù))
|
file or directory closed, after being opened in writeable mode.
文件或目錄關(guān)閉,在寫入模式打開之后關(guān)閉的。
|
close_nowrite
|
file or directory closed, after being opened in read-only mode.
文件或目錄關(guān)閉,在只讀模式打開之后關(guān)閉的
|
close
|
file or directory closed, regardless of read/write mode
文件或目錄關(guān)閉,不管讀或是寫模式
|
open
|
file or directory opened
文件或目錄被打開
|
moved_to
拉
|
file or directory moved to watched directory
文件或目錄被移動(dòng)到監(jiān)控的目錄中
|
moved_from
推
|
file or directory moved from watched directory
文件或目錄被移動(dòng)從監(jiān)控的目錄中
|
move
(重要參數(shù))
|
file or directory moved to or from watched directory
文件或目錄不管移動(dòng)到或是移出監(jiān)控目錄都觸發(fā)事件
|
create
(重要參數(shù))
|
file or directory created within watched directory
文件或目錄創(chuàng)建在監(jiān)控目錄中
|
delete
(重要參數(shù))
|
file or directory deleted within watched directory
文件或目錄被刪除在監(jiān)控目錄中
|
delete_self
|
file or directory was deleted
文件或目錄被刪除,目錄本身被刪除
|
unmount
|
file system containing file or directory unmounted
|
以上的信息可以通過 inotifywait --help 獲得
|
1.5.4.1 【實(shí)例】inotifywait監(jiān)控中的事件測(cè)試
1、創(chuàng)建事件
[root@nfs01 data]# touch test2.txt [root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e create 17-10-17 11:19 /data/test2.txt 事件信息: CREATE
2、刪除事件
[root@nfs01 data]# \rm -f test1.txt [root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e delete 17-10-17 11:28 /data/test1.txt 事件信息: DELETE
3、修改事件
[root@nfs01 data]# echo "132" > test.txt [root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e close_write 17-10-17 11:30 /data/test.txt 事件信息: CLOSE_WRITE,CLOSE
4、移動(dòng)事件 moved_to
[root@nfs01 data]# mv /etc/hosts . [root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e moved_to 17-10-17 11:33 /data/hosts 事件信息: MOVED_TO
5、移動(dòng)事件 moved_from
[root@nfs01 data]# mv ./hosts /tmp/ [root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e moved_from 17-10-17 11:34 /data/hosts 事件信息: MOVED_FROM
1.5.5 inotifywait 參數(shù) --format <fmt>格式定義參數(shù)
命令參數(shù)
|
參數(shù)說明
|
%w(重要參數(shù))
|
事件出現(xiàn)時(shí),監(jiān)控文件或目錄的名稱信息
|
%f(重要參數(shù))
|
事件出現(xiàn)時(shí),將顯示監(jiān)控目錄下觸發(fā)事件的文件或目錄信息,否則為空
|
%e(重要參數(shù))
|
顯示發(fā)生的事件信息,不同的事件信息用逗號(hào)進(jìn)行分隔
|
%Xe
|
顯示發(fā)生的事件信息,不同的事件信息有x進(jìn)行分隔,可以修改X為指定分隔符
|
%T(重要參數(shù))
|
輸出時(shí)間格式中定義的時(shí)間格式信息,通過 --timefmt option 語(yǔ)法格式指定時(shí)間信息
這個(gè)格式是通過strftime函數(shù)進(jìn)行匹配時(shí)間格式信息的
|
以上的信息可以通過 inotifywait --help 獲得
|
1.5.6 inotifywait 參數(shù)--timefmt <fmt>時(shí)間格式參數(shù)
命令參數(shù)
|
參數(shù)說明
|
%d(重要參數(shù))
|
The day of the month as a decimal number(range 01 to 31)
每月的第幾天,顯示倍息為十進(jìn)制數(shù)(范圍是 01-31 )
|
%m(重要參數(shù))
|
The month as a decimal number (range 01 to 12).
顯示月份,顯示信息為十進(jìn)制(范圍 01-12 )
|
%M
|
The minute as a decimal number (range 00 to 59).
顯示分鐘,顯示信息為十進(jìn)制(范圍 00-59 )
|
%y(重要參數(shù))
|
The year as a decimal number without a century (range 00 to 99).
年份信息,顯示信息為十進(jìn)制,并且沒有世紀(jì)信息
|
%Y
|
The year as a decimal number including the century.
年份信息,顯示信息為十進(jìn)制,并且包含世紀(jì)信息
|
%H
|
The hour as a decimal number using a 24-hour clock (range 00 to 23).
小時(shí)信息,顯示信息為十進(jìn)制,使用 24小時(shí)制(范圍 00-23 )
|
說明:以上信息可以通過 man strftime信息獲取
|
1.5.6.1 修改輸出的日期格式
[root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d/%m/%y %H:%M" --format "%T %w%f" 17/10/17 11:12 /data/test1.txt
1.5.7 -e[參數(shù)] 重要監(jiān)控事件參數(shù)匯總表:
重要事件
|
包含事件
|
備注說明
|
close
|
close_write
close_nowrite
|
文件或目錄關(guān)閉,不管讀或是寫模式
即包含寫關(guān)閉與讀關(guān)閉
|
close_write
|
create
|
包含文件創(chuàng)建事件,但不包含目錄創(chuàng)建事件
|
move
|
moved_to
moved_from
|
文件或目錄不管移動(dòng)到或是移動(dòng)出監(jiān)控目錄都觸發(fā)事件
即包含信息移入或移出監(jiān)控目錄事件
|
重要參數(shù)匯總:根據(jù)以上說明,在實(shí)際使用時(shí),只要監(jiān)控以下事件即可
create 創(chuàng)建、 delete 刪除、 movedjto 移入、 close_write 修 改
inotifywait -mrq /data --format "%w%f" -e create,delete,moved_to,close_write
|
1.6 對(duì)inotifywait命令的測(cè)試
對(duì)inotifywait命令測(cè)試的說明:
需要打開兩個(gè)連接窗口
1窗口運(yùn)行inotifywait 2窗口對(duì)文件夾進(jìn)行操作,可在一窗口中查看出inotifywait的監(jiān)控記錄
1.6.1 、創(chuàng)建文件的邏輯↓
[root@nfs01 ~]# inotifywait /data Setting up watches. Watches established. /data/ CREATE test1.txt /data/ OPEN test1.txt /data/ ATTRIB test1.txt /data/ CLOSE_WRITE,CLOSE test1.txt 創(chuàng)建文件,inotifywait顯示創(chuàng)建文件的過程↑ [root@nfs01 data]# touch test1.txt
1.6.2 創(chuàng)建目錄邏輯↓
[root@nfs01 data]# mkdir testdir [root@nfs01 ~]# /data/ CREATE,ISDIR testdir
1.6.3 監(jiān)控子目錄下的文件↓
[root@nfs01 data]# touch testdir/test01.txt [root@nfs01 ~]# inotifywait -mrq /data /data/testdir/ OPEN test01.txt /data/testdir/ ATTRIB test01.txt /data/testdir/ CLOSE_WRITE,CLOSE test01.txt
1.6.4 sed命令修改邏輯
[root@nfs01 data]# sed 's#132#123#g' test.txt -i [root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e moved_from /data/test.txt 事件信息: OPEN /data/sedDh5R8v 事件信息: CREATE /data/sedDh5R8v 事件信息: OPEN /data/test.txt 事件信息: ACCESS /data/sedDh5R8v 事件信息: MODIFY /data/sedDh5R8v 事件信息: ATTRIB /data/sedDh5R8v 事件信息: ATTRIB /data/test.txt 事件信息: CLOSE_NOWRITE,CLOSE /data/sedDh5R8v 事件信息: CLOSE_WRITE,CLOSE /data/sedDh5R8v 事件信息: MOVED_FROM /data/test.txt 事件信息: MOVED_TO
sed命令替換邏輯 :
01. 創(chuàng)建臨時(shí)文件
02. 將原文件內(nèi)容放置到臨時(shí)文件中,修改替換臨時(shí)文件中的內(nèi)容,原有文件不做改動(dòng)
03. 重命名臨時(shí)文件,覆蓋原文件
1.6.5 inotifywait監(jiān)控中 -e 的參數(shù)使用
inotifywait -mrq /data --timefmt "%d/%m/%y %H:%M" --format "%T %w%f 事件信息: %e" -e create
說明:表示只監(jiān)聽create事件
inotifywait -mrq /data --timefmt "%d/%m/%y %H:%M" --format "%T %w%f 事件信息: %e"
說明:不指定-e參數(shù),表示監(jiān)聽所有事件
02. 刪除事件delete
# inotifywait -mrq /data --timefmt "%F %H:%M" --format "%T %w%f 事件信息: %@e" -e delete 2017-10-17 11:28 /data/02.txt 事件信息: DELETE 2017-10-17 11:28 /data/03.txt 事件信息: DELETE 2017-10-17 11:28 /data/04.txt 事件信息: DELETE
03. 修改事件close_write
# inotifywait -mrq /data --timefmt "%F %H:%M" --format "%T %w%f 事件信息: %@e" -e delete,close_write 2017-10-17 11:30 /data/oldgirl.txt 事件信息: CLOSE_WRITE@CLOSE 2017-10-17 11:30 /data/.oldgirl.txt.swx 事件信息: CLOSE_WRITE@CLOSE 2017-10-17 11:30 /data/.oldgirl.txt.swx 事件信息: DELETE 2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: CLOSE_WRITE@CLOSE 2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: DELETE 2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: CLOSE_WRITE@CLOSE 2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: DELETE
04. 移動(dòng)事件moved_to
inotifywait -mrq /data --timefmt "%F %H:%M" --format "%T %w%f 事件信息: %@e" -e delete,close_write,moved_to 2017-10-17 11:34 /data/hosts 事件信息: MOVED_TO
1.7 實(shí)時(shí)同步命令參數(shù)示意圖
第2章 inotify+rsync實(shí)時(shí)同步服務(wù)部署
2.1 第一個(gè)里程碑:部署rsync服務(wù)
2.1.1 rsync服務(wù)端部署
1)軟件是否存在
[root@backup ~]# rpm -qa |grep rsync rsync-3.0.6-12.el6.x86_64
需求:查詢到某個(gè)命令非常有用。但是不知道屬于哪個(gè)軟件包
yum provides rysnc provides Find what package provides the given value
2)進(jìn)行軟件服務(wù)配置
[root@backup ~]# vim /etc/rsyncd.conf uid = rsync gid = rsync use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore errors read only = false list = false hosts allow = 172.16.1.0/24 auth users = rsync_backup secrets file = /etc/rsync.password [backup] comment = "backup dir by oldboy" path = /backup [nfsbackup] comment = "nfsbackup dir by hzs" path = /nfsbackup
3)創(chuàng)建rsync管理用戶
[root@backup ~]# useradd -s /sbin/nologin -M rsync
4)創(chuàng)建數(shù)據(jù)備份儲(chǔ)存目錄,目錄修改屬主
[root@backup ~]# mkdir /nfsbackup/ [root@backup ~]# chown -R rsync.rsync /nfsbackup/
5)創(chuàng)建認(rèn)證用戶密碼文件并進(jìn)行授權(quán)600
echo "rsync_backup:clsn123" >>/etc/rsync.password chmod 600 /etc/rsync.password
6)啟動(dòng)rsync服務(wù)
rsync --daemon
至此服務(wù)端配置完成
[root@backup ~]# ps -ef |grep rsync root 2076 1 0 17:05 ? 00:00:00 rsync --daemon root 2163 1817 0 17:38 pts/1 00:00:00 grep --color=auto rsync
2.1.2 rsync客戶端配置
1)軟件是否存在
[root@backup ~]# rpm -qa |grep rsync rsync-3.0.6-12.el6.x86_64
2)創(chuàng)建安全認(rèn)證文件,并進(jìn)行修改權(quán)限600
echo "clsn123" >>/etc/rsync.password chmod 600 /etc/rsync.password
3) 測(cè)試數(shù)據(jù)傳輸
[root@nfs01 sersync]# rsync -avz /data rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password sending incremental file list data/ data/.hzs data/.tar.gz data/.txt
2.2 第二個(gè)里程碑:部署inotify服務(wù)
首先先確認(rèn)是否有epel源用來安裝inotify-tools軟件
[root@nfs01 ~]# yum repolist Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com repo id repo name status base CentOS-6 - Base - mirrors.aliyun.com 6,706 epel Extra Packages for Enterprise Linux 6 - x86_64 12,401 extras CentOS-6 - Extras - mirrors.aliyun.com 46 updates CentOS-6 - Updates - mirrors.aliyun.com 722 repolist: 19,875
2.2.1 安裝inotify軟件
兩種安裝方式
1) yum install -y inotify-tools
2) 手工編譯安裝
注:
手工編譯安裝方式需要到github上進(jìn)行下載軟件包
inotify軟件的參考資料鏈接:
https://github.com/rvoicilas/inotify-tools/wiki
2.2.2 查看inotify安裝上的兩個(gè)命令(inotifywait,inotifywatch)
[root@nfs01 ~]# rpm -ql inotify-tools /usr/bin/inotifywait #主要 /usr/bin/inotifywatch
2.2.2.1 inotifywait和inotifywatch的作用:
一共安裝了2個(gè)工具(命令),即inotifywait和inotifywatch
inotifywait : 在被監(jiān)控的文件或目錄上等待特定文件系統(tǒng)事件(open close delete等)發(fā)生,
執(zhí)行后處于阻塞狀態(tài),適合在shell腳本中使用
inotifywatch :收集被監(jiān)控的文件系統(tǒng)使用的統(tǒng)計(jì)數(shù)據(jù),指文件系統(tǒng)事件發(fā)生的次數(shù)統(tǒng)計(jì)。
說明:yum安裝后可以直接使用,如果編譯安裝需要進(jìn)入到相應(yīng)軟件目錄的bin目錄下使用
#命令 man手冊(cè)說明 # man inotifywait inotifywait - wait for changes to files using inotify 使用inotify進(jìn)行監(jiān)控,等待產(chǎn)生變化的文件信息 # man inotifywatch inotifywatch - gather filesystem access statistics using inotify 使用inotify進(jìn)行監(jiān)控,收集文件系統(tǒng)訪問統(tǒng)計(jì)佶息
2.3 第三個(gè)里程碑:編寫腳本,實(shí)現(xiàn)rsync+inotify軟件功能結(jié)合
2.3.1 rsync服務(wù)命令:
rsync -avz --delete /data/ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password
2.3.2 inotify服務(wù)命令:
inotifywait -mrq /data -format "%w%f" -e create,delete,move_to,close_write
2.3.3 編寫腳本:
[root@nfs01 sersync]# vim /server/scripts/inotify.sh #!/bin/bash inotifywait -mrq /data --format "%w%f" -e create,delete,moved_to,close_write|\ while read line do rsync -az --delete /data/ rsync_backup@172.16.1.41::nfsbackup --password- file=/etc/rsync.password done
腳本說明:
for循環(huán)會(huì)定義一個(gè)條件,當(dāng)條件不滿足時(shí)停止循環(huán)
while循環(huán):只要條件滿足就一直循環(huán)下去
2.3.4 對(duì)腳本進(jìn)行優(yōu)化
#!/bin/bash Path=/data backup_Server=172.16.1.41 /usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /data | while read line do if [ -f $line ];then rsync -az $line --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password else cd $Path &&\ rsync -az ./ --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password fi done
2.4 第四個(gè)里程碑:測(cè)試編寫的腳本
2.4.1 讓腳本在后臺(tái)運(yùn)行
在/data 目錄先創(chuàng)建6個(gè)文件
[root@nfs01 data]# sh /server/scripts/inotify.sh & [root@nfs01 data]# touch {1..6}.txt
在backup服務(wù)器上,已經(jīng)時(shí)候同步過去了6個(gè)文件。
[root@backup ~]# ll /nfsbackup/ total 8 -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 1.txt -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 2.txt -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 3.txt -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 4.txt -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 5.txt -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 6.txt
2.5 利用while循環(huán)語(yǔ)句編寫的腳本停止方法(kill)
01. ctrl+z暫停程序運(yùn)行,kill -9殺死
02. 不要暫停程序,直接利用殺手三劍客進(jìn)行殺進(jìn)程
說明:kill三個(gè)殺手不是萬(wàn)能的,在進(jìn)程暫停時(shí),無(wú)法殺死;kill -9 (危險(xiǎn))
2.5.1 查看后臺(tái)都要哪些程序在運(yùn)行
[root@nfs01 data]# jobs [1]+ Running sh /server/scripts/inotify.sh &
2.5.2 fg將后臺(tái)的程序調(diào)到前臺(tái)來
[root@nfs01 data]# fg 1 sh /server/scripts/inotify.sh
2.6 進(jìn)程的前臺(tái)和后臺(tái)運(yùn)行方法:
fg-- 前臺(tái)
bg-- 后臺(tái)
2.6.1 腳本后臺(tái)運(yùn)行方法
01. sh inotify.sh &
02. nohup sh inotify.sh &
03. screen實(shí)現(xiàn)腳本程序后臺(tái)運(yùn)行
sh /server/scripts/inotify.sh & nohup nohup sh inotify.sh &
2.7 screen實(shí)現(xiàn)腳本程序后臺(tái)運(yùn)行
2.7.1 經(jīng)過yum查找發(fā)現(xiàn)screen命令屬于screen包
[root@test ~]# yum provides screen Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com base | 3.7 kB 00:00 epel | 4.3 kB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 screen-4.0.3-19.el6.x86_64 : A screen manager that supports multiple logins on : one terminal Repo : base Matched from:
2.7.2 安裝screen軟件
[root@test ~]# yum install -y screen
2.7.3 screen命令的參數(shù)
在shell中輸入 screen即可進(jìn)入screen 視圖
[root@test ~]# screen
Screen實(shí)現(xiàn)后臺(tái)運(yùn)行程序的簡(jiǎn)單步驟:
screen -ls :可看screen會(huì)話
screen +a d :退出當(dāng)前的screen,保持其后臺(tái)運(yùn)行
screen -r ID :指定進(jìn)入哪個(gè)screen會(huì)話
Screen命令中用到的快捷鍵
Ctrl+a c :創(chuàng)建窗口
Ctrl+a w :窗口列表
Ctrl+a n :下一個(gè)窗口
Ctrl+a p :上一個(gè)窗口
Ctrl+a 0-9 :在第0個(gè)窗口和第9個(gè)窗口之間切換
Ctrl+a K(大寫) :關(guān)閉當(dāng)前窗口,并且切換到下一個(gè)窗口 ,
(當(dāng)退出最后一個(gè)窗口時(shí),該終端自動(dòng)終止,并且退回到原始shell狀態(tài))
exit :關(guān)閉當(dāng)前窗口,并且切換到下一個(gè)窗口
(當(dāng)退出最后一個(gè)窗口時(shí),該終端自動(dòng)終止,并且退回到原始shell狀態(tài))
Ctrl+a d :退出當(dāng)前終端,返回加載screen前的shell命令狀態(tài)
Ctrl+a " : 窗口列表不同于w
以上這篇inotify+rsync實(shí)現(xiàn)實(shí)時(shí)同步的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Apache、Nginx 服務(wù)配置服務(wù)器端包含(SSI)
今天在給寫新的sws安裝包,測(cè)試程序采用了cmstop,其中安裝有一項(xiàng)需要支持服務(wù)器端包含(ssi),下面教給大家如何讓你的apache或者nginx支持。2010-12-12Linux運(yùn)維基礎(chǔ)進(jìn)程管理實(shí)時(shí)監(jiān)控控制
這篇文章主要介紹了Linux運(yùn)維基礎(chǔ)中進(jìn)程的管理及實(shí)時(shí)監(jiān)控控制,有需要的朋友可以借鑒參考下,希望可以有所幫助,共同學(xué)習(xí)共同進(jìn)步2021-09-09Apache虛擬目錄配置及vue-cli反向代理的設(shè)置方法
本篇文章主要介紹了Apache虛擬目錄配置及vue-cli反向代理的設(shè)置方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09Vmvare虛擬機(jī)給ubuntu根目錄分區(qū)介紹
大家好,本篇文章主要講的是Vmvare虛擬機(jī)給ubuntu根目錄分區(qū)介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Apache服務(wù)器一個(gè)IP多個(gè)站點(diǎn)的配置方法示例
這篇文章主要介紹了Apache服務(wù)器一個(gè)IP多個(gè)站點(diǎn)的配置方法示例,本文介紹的方法其實(shí)就是虛擬主機(jī)的方式,需要的朋友可以參考下2015-07-0713個(gè)實(shí)用的Apache Rewrite重寫規(guī)則
這篇文章主要介紹了13個(gè)實(shí)用的Apache Rewrite重寫規(guī)則,需要的朋友可以參考下2014-03-03