一、inotify簡介
inotify是Linux內核的一個功能,它能監控文件系統的變化,比如刪除、讀、寫和卸載等操作。它監控到這些事件的發生后會默認往標准輸出打印事件信息。要使用inotify,Linux內核版本必須是大於2.6.13,在這之前的內核都沒有這個功能。我們可以利用inotify去監控我們要監控的資源,當資源發生變化時,我們撲捉它的事件,從而觸發同步數據等操作。
二、inotify軟件介紹
inotify是內核的一個功能,眾所周知內核的功能我們必須要配合工具才能使用,通常情況下用戶要使用內核的功能,都需要用戶空間的軟件去調用才可以達到使用內核的功能的目的,用戶是無法直接操內核的。實現inotify軟件有inotify-tools、sersync、lrsyncd。我們這里以inotify-tools這個軟件包為例進行實驗;inotify-tools包主要有兩個文件,一個是inotifywait: 在被監控的文件或目錄上等待特定文件系統事件(open close delete等)發生,常用於實時同步的目錄監控;一個是inotifywatch:收集被監控的文件系統使用的統計數據,指文件系統事件發生的次數統計。通常情況下我們使用iontifywait就可以了。接下來我們來安裝inotify-tools
三、inotify軟件安裝和使用
首先我們要確認我們手上的Linux系統是否支持inotify這個功能
[root@test ~]# uname -r 3.10.0-957.27.2.el7.x86_64 [root@test ~]# ll /proc/sys/fs/inotify/ 總用量 0 -rw-r--r-- 1 root root 0 1月 31 16:29 max_queued_events -rw-r--r-- 1 root root 0 1月 31 16:29 max_user_instances -rw-r--r-- 1 root root 0 1月 31 16:29 max_user_watches [root@test ~]#
提示:如果我們的內核版本高於2.6.13,並且有/proc/sys/fs/inotify/目錄,說明我們的系統上支持inotify功能的
inotify內核參數說明
我們在/proc/sys/fs/inotify這個目錄下看到有三個文件,這三個文件就是記錄inotify的內核參數的文件。其中max_queued_events這個文件記錄inotify事件隊列最大長度,如值太小會報Event Queue Overflow 錯誤,默認值:16384,當然這個值是可以更改的,臨時更改可以用echo 命令加上值給重定向到這個文件中。永久修改需要修改編輯/etc/sysctl.conf
[root@test ~]# cat /proc/sys/fs/inotify/max_queued_events 16384 [root@test ~]# echo 'fs.inotify.max_queued_events=999999' >> /etc/sysctl.conf [root@test ~]# cat /etc/sysctl.conf # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). fs.inotify.max_queued_events=999999 [root@test ~]# sysctl -p fs.inotify.max_queued_events = 999999 [root@test ~]# cat /proc/sys/fs/inotify/max_queued_events 999999 [root@test ~]#
提示:修改內核其他參數,也是是類似的方法。
max_user_watches:這個文件主要存放單個進程監控文件的數量,默認是8129;生產環境中如果監控的文件數量龐大,可以通過上面的方式去修改這個值
max_user_instances:這個文件存放用戶創建inotify實例的最大值,默認是128;同樣也是可以通過上面的方式修改
inotify軟件安裝
參考文檔https://github.com/rvoicilas/inotify-tools/wiki
inotify是基於epel源,在用yum安裝前請先配好yum源
[root@test ~]# yum info inotify-tools 已加載插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com 可安裝的軟件包 名稱 :inotify-tools 架構 :x86_64 版本 :3.14 發布 :9.el7 大小 :51 k 源 :epel/x86_64 簡介 : Command line utilities for inotify 網址 :http://inotify-tools.sourceforge.net/ 協議 : GPLv2 描述 : inotify-tools is a set of command-line programs for Linux providing : a simple interface to inotify. These programs can be used to monitor : and act upon filesystem events. [root@test ~]#
提示:如果用yum可以看到inotify-tools的說明信息,說明你的Linux yum倉庫里有這個軟件包,可以直接yum 安裝即可
[root@test ~]# yum install -y inotify-tools 已加載插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com base | 3.6 kB 00:00:00 dockerrepo | 2.9 kB 00:00:00 epel | 5.3 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 updates/7/x86_64/primary_db | 5.9 MB 00:00:01 正在解決依賴關系 --> 正在檢查事務 ---> 軟件包 inotify-tools.x86_64.0.3.14-9.el7 將被 安裝 --> 解決依賴關系完成 依賴關系解決 ================================================================================================================================= Package 架構 版本 源 大小 ================================================================================================================================= 正在安裝: inotify-tools x86_64 3.14-9.el7 epel 51 k 事務概要 ================================================================================================================================= 安裝 1 軟件包 總下載量:51 k 安裝大小:111 k Downloading packages: inotify-tools-3.14-9.el7.x86_64.rpm | 51 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安裝 : inotify-tools-3.14-9.el7.x86_64 1/1 驗證中 : inotify-tools-3.14-9.el7.x86_64 1/1 已安裝: inotify-tools.x86_64 0:3.14-9.el7 完畢! [root@test ~]#
提示:我們安裝好軟件包要習慣的去看這個軟件包裝了那些東西,它的配置文件大概放在哪個位置等信息
[root@test ~]# rpm -ql inotify-tools /usr/bin/inotifywait /usr/bin/inotifywatch /usr/lib64/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0.4.1 /usr/share/doc/inotify-tools-3.14 /usr/share/doc/inotify-tools-3.14/AUTHORS /usr/share/doc/inotify-tools-3.14/COPYING /usr/share/doc/inotify-tools-3.14/ChangeLog /usr/share/doc/inotify-tools-3.14/NEWS /usr/share/doc/inotify-tools-3.14/README /usr/share/man/man1/inotifywait.1.gz /usr/share/man/man1/inotifywatch.1.gz [root@test ~]#
提示:從上面查詢的信息我們可以清楚的知道,inotify-tools這兩個包主要安裝了兩個程序,一個是/usr/bin/inotifywait,一個是/usr/bin/inotifywatch,其他都是它的幫助文檔;在上面的介紹了這兩個程序的作用;安裝好了inotify工具后,我們再來看看inotifywait這個工具的用法和選項
inotifywait命令常見選項說明
[root@test ~]# inotifywait --help inotifywait 3.14 Wait for a particular event on a file or set of files. Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ] Options: -h|--help Show this help text. @<file> Exclude the specified file from being watched. --exclude <pattern> Exclude all events on files matching the extended regular expression <pattern>. --excludei <pattern> Like --exclude but case insensitive. -m|--monitor Keep listening for events forever. Without this option, inotifywait will exit after one event is received. -d|--daemon Same as --monitor, except run in the background logging events to a file specified by --outfile. Implies --syslog. -r|--recursive Watch directories recursively. --fromfile <file> Read files to watch from <file> or `-' for stdin. -o|--outfile <file> Print events to <file> rather than stdout. -s|--syslog Send errors to syslog rather than stderr. -q|--quiet Print less (only print events). -qq Print nothing (not even events). --format <fmt> Print using a specified printf-like format string; read the man page for more details. --timefmt <fmt> strftime-compatible format string for use with %T in --format string. -c|--csv Print events in CSV format. -t|--timeout <seconds> When listening for a single event, time out after waiting for an event for <seconds> seconds. If <seconds> is 0, inotifywait will never time out. -e|--event <event1> [ -e|--event <event2> ... ] Listen for specific event(s). If omitted, all events are listened for. Exit status: 0 - An event you asked to watch for was received. 1 - An event you did not ask to watch for was received (usually delete_self or unmount), or some error occurred. 2 - The --timeout option was given and no events occurred in the specified interval of time. Events: access file or directory contents were read modify file or directory contents were written attrib file or directory attributes changed close_write file or directory closed, after being opened in writeable mode close_nowrite file or directory closed, after being opened in read-only mode close file or directory closed, regardless of read/write mode open file or directory opened moved_to file or directory moved to watched directory moved_from file or directory moved from watched directory move file or directory moved to or from watched directory create file or directory created within watched directory delete file or directory deleted within watched directory delete_self file or directory was deleted unmount file system containing file or directory unmounted [root@test ~]#
提示:-m,--monitor表示始終保持事件監聽,如果不加這個選項,默認它只是監控一次,也就是說只要我們監控的目錄里的資源發生一次變化后,它就退出監控了;通常情況我們是結合-m選項持續的監控一個目錄下的資源變化情況;-d表示以守護進程方式執行,和-m相似,配合-o使用。-d選項我們可以理解為后台守護進程的方式監控我們的資源,后台守護進程的方式執行我們是看不到說監控的資源變化情況,我們可以結合-o來指定一個文件,把資源變化情況類似寫日志的方式給記錄下來; -r, --recursive表示遞歸監控目錄數據信息變化;-q,--quiet表示輸出少量事件信息;--timefmt <fmt>表示指定輸出事件的時間格式;--format <fmt> 表示指定的輸出格式;即實際監控輸出內容; -e 表示指定監聽指定的事件,如果省略,表示所有事件都進行監聽;--exclude <pattern>表示指定排除文件或目錄,使用擴展的正則表達式匹配的模式實現;--excludei <pattern> 和exclude相似,不區分大小寫; -o, --outfile <file>打印事件到文件中,相當於標准正確輸出;-s, --syslogOutput 發送錯誤到syslog相當於標准錯誤輸出
--timefmt <fmt>時間格式
%Y:表示年份信息,包含世紀信息
%y:表示年份信息,不包含世紀信息
%m:表示月份,范圍01-12
%d:表示每月的第幾天,范圍01-31
%H:表示小時信息,使用24小時制度,范圍00-24
%M:表示分鍾,范圍00-59
--format<fmt>輸出信息格式定義
%T:輸出時間格式中定義的時間格式,這個表示調用我們前面定義時間格式,通常情況--timefmt選項和--format結合使用
%w:表示事件出現時,監控文件或目錄的名稱信息。也就是監控資源名稱信息
%f:時間出現時,顯示監控目錄下觸發事件的文件或目錄信息
%e:顯示發生的事件信息,不同事件默認用逗號分隔
%Xe:顯示發生的事件,不同的時間指定用"X"分割
[root@test ~]# inotifywait -m --timefmt "%Y-%m-%d %H:%M" --format "%T %w event:%;e" /root/dir2 Setting up watches. Watches established. 2020-01-31 18:07 /root/dir2/ event:OPEN;ISDIR 2020-01-31 18:07 /root/dir2/ event:CLOSE_NOWRITE;CLOSE;ISDIR 2020-01-31 18:07 /root/dir2/ event:OPEN;ISDIR 2020-01-31 18:07 /root/dir2/ event:CLOSE_NOWRITE;CLOSE;ISDIR 2020-01-31 18:07 /root/dir2/ event:OPEN;ISDIR 2020-01-31 18:07 /root/dir2/ event:CLOSE_NOWRITE;CLOSE;ISDIR 2020-01-31 18:07 /root/dir2/ event:OPEN;ISDIR 2020-01-31 18:07 /root/dir2/ event:CLOSE_NOWRITE;CLOSE;ISDIR
提示:指定信息不輸出變化資源的文件名和目錄,輸出信息格式如上
[root@test ~]# inotifywait -m --timefmt "%Y-%m-%d %H:%M" --format "%T %w%f event:%;e" /root/dir2 Setting up watches. Watches established. 2020-01-31 18:09 /root/dir2/test event:OPEN;ISDIR 2020-01-31 18:09 /root/dir2/test event:CLOSE_NOWRITE;CLOSE;ISDIR 2020-01-31 18:09 /root/dir2/ event:OPEN;ISDIR 2020-01-31 18:09 /root/dir2/ event:CLOSE_NOWRITE;CLOSE;ISDIR 2020-01-31 18:09 /root/dir2/f1 event:CREATE 2020-01-31 18:09 /root/dir2/f1 event:OPEN 2020-01-31 18:09 /root/dir2/f1 event:ATTRIB 2020-01-31 18:09 /root/dir2/f1 event:CLOSE_WRITE;CLOSE 2020-01-31 18:09 /root/dir2/f1 event:MOVED_FROM 2020-01-31 18:09 /root/dir2/ff event:MOVED_TO
提示:指定信息輸出變化資源的文件名和目錄,輸出信息格式如上
[root@test ~]# inotifywait -m --timefmt "%Y-%m-%d %H:%M" --format "%T %w%f %e" /root/dir2 Setting up watches. Watches established. 2020-01-31 18:10 /root/dir2/ OPEN,ISDIR 2020-01-31 18:10 /root/dir2/ CLOSE_NOWRITE,CLOSE,ISDIR 2020-01-31 18:10 /root/dir2/ OPEN,ISDIR 2020-01-31 18:10 /root/dir2/ CLOSE_NOWRITE,CLOSE,ISDIR 2020-01-31 18:10 /root/dir2/ OPEN,ISDIR 2020-01-31 18:10 /root/dir2/ CLOSE_NOWRITE,CLOSE,ISDIR 2020-01-31 18:10 /root/dir2/ OPEN,ISDIR 2020-01-31 18:10 /root/dir2/ CLOSE_NOWRITE,CLOSE,ISDIR 2020-01-31 18:10 /root/dir2/test OPEN,ISDIR 2020-01-31 18:10 /root/dir2/test CLOSE_NOWRITE,CLOSE,ISDIR
提示:不指定輸出事件信息分割符,默認是逗號分隔
-e選項指定監控事件類型說明
create:表示文件或目錄的創建
delete:表示文件或目錄被刪除
modify:表示文件或目錄內容的修改
attrib:表示文件或目錄的屬性發生變化
close_write:表示文件或目錄關閉,在寫入模式打開之后關閉的
close_nowrite:表示文件或目錄關閉,在只讀模式打開之后關閉的
close:表示文件或目錄關閉,不管是讀還是寫模式
open:文件或目錄被打開
moved_to:表示文件被移動到監控目錄中
moved_from:表示文件或目錄從監控的目錄中被移動
move:文件或目錄不管移動到或移出監控目錄都觸發事件
access:表示文件或目錄內容讀取
delete_self:表示文件或目錄被刪除,目錄本身被刪除
unmount:表示取消掛載
提示:-e選項指定監控目錄下文件或目錄的事件,它只響應對應目錄下的文件發生了指定事件后,才給予響應。以上表示只要/root/dir2目錄下的文件或目錄發生了create,delete,move這三種事件的任何一種,它都給予響應,我們查看/root/dir2下的文件它是不監控的,也不予響應。
了解了inotifywait命令的各種選項的使用后,我們就可以根據自己的實際需求來定義怎么監控我們的資源,當資源發生變化時,我們后續該怎么處理,這個不是inotify關心的事,它只負責告訴我們監控的資源發生了什么樣的變化。如果我們想要備份變化后的資源,我們就需要用到別的服務,比如rsync;rsync這個軟件是一個同步數據軟件,它可以通過腳本和inotify結合實現,當資源文件發生變化時,觸發rsync去同步數據,這樣實現數據實時的同步。接下來我們在來搭建rsync服務
四、rsync服務安裝配置
1、查看系統是否安裝的有rsync,如果沒有就安裝
[root@test ~]# rpm -qa rsync rsync-3.1.2-6.el7_6.1.x86_64 [root@test ~]# yum info rsync 已加載插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com 已安裝的軟件包 名稱 :rsync 架構 :x86_64 版本 :3.1.2 發布 :6.el7_6.1 大小 :815 k 源 :installed 來自源:updates 簡介 : A program for synchronizing files over a network 網址 :http://rsync.samba.org/ 協議 : GPLv3+ 描述 : Rsync uses a reliable algorithm to bring remote and host files into : sync very quickly. Rsync is fast because it just sends the differences : in the files over the network instead of sending the complete : files. Rsync is often used as a very powerful mirroring process or : just as a more capable replacement for the rcp command. A technical : report which describes the rsync algorithm is included in this : package. [root@test ~]#
2、查看rsync配置文件路徑
[root@test ~]# rpm -ql rsync /etc/rsyncd.conf /etc/sysconfig/rsyncd /usr/bin/rsync /usr/lib/systemd/system/rsyncd.service /usr/lib/systemd/system/rsyncd.socket /usr/lib/systemd/system/rsyncd@.service /usr/share/doc/rsync-3.1.2 /usr/share/doc/rsync-3.1.2/COPYING /usr/share/doc/rsync-3.1.2/NEWS /usr/share/doc/rsync-3.1.2/OLDNEWS /usr/share/doc/rsync-3.1.2/README /usr/share/doc/rsync-3.1.2/support /usr/share/doc/rsync-3.1.2/support/Makefile /usr/share/doc/rsync-3.1.2/support/atomic-rsync /usr/share/doc/rsync-3.1.2/support/cvs2includes /usr/share/doc/rsync-3.1.2/support/deny-rsync /usr/share/doc/rsync-3.1.2/support/file-attr-restore /usr/share/doc/rsync-3.1.2/support/files-to-excludes /usr/share/doc/rsync-3.1.2/support/git-set-file-times /usr/share/doc/rsync-3.1.2/support/instant-rsyncd /usr/share/doc/rsync-3.1.2/support/logfilter /usr/share/doc/rsync-3.1.2/support/lsh /usr/share/doc/rsync-3.1.2/support/lsh.sh /usr/share/doc/rsync-3.1.2/support/mapfrom /usr/share/doc/rsync-3.1.2/support/mapto /usr/share/doc/rsync-3.1.2/support/mnt-excl /usr/share/doc/rsync-3.1.2/support/munge-symlinks /usr/share/doc/rsync-3.1.2/support/rrsync /usr/share/doc/rsync-3.1.2/support/rsync-no-vanished /usr/share/doc/rsync-3.1.2/support/rsync-slash-strip /usr/share/doc/rsync-3.1.2/support/rsyncstats /usr/share/doc/rsync-3.1.2/support/savetransfer.c /usr/share/doc/rsync-3.1.2/tech_report.tex /usr/share/man/man1/rsync.1.gz /usr/share/man/man5/rsyncd.conf.5.gz [root@test ~]#
提示:可從上面的信息了解到rsync的配置文件是/etc/rsyncd.conf,二進制文件是/usr/bin/rsync
3、/etc/rsyncd.conf配置文件內容說明
[root@test ~]# cat /etc/rsyncd.conf # /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for more options. # configuration example: # uid = nobody # gid = nobody # use chroot = yes # max connections = 4 # pid file = /var/run/rsyncd.pid # exclude = lost+found/ # transfer logging = yes # timeout = 900 # ignore nonreadable = yes # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # [ftp] # path = /home/ftp # comment = ftp export area [root@test ~]#
提示:我們打開rsync的配置文件,里面內容告訴我們,這個配置文件用於rsync工作為獨立守護進程的服務配置,我們可以通過man rsyncd.conf來查看里面的選項說明
4、編輯/etc/rsyncd.conf
[root@test ~]# cat /etc/rsyncd.conf # /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for more options. # configuration example: # uid = nobody # gid = nobody # use chroot = yes # max connections = 4 # pid file = /var/run/rsyncd.pid # exclude = lost+found/ # transfer logging = yes # timeout = 900 # ignore nonreadable = yes # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # [ftp] # path = /home/ftp # comment = ftp export area uid = root gid = root use chroot = no 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 hosts allow = 192.168.0.0/24 [backup] path = /backup/ comment = backup read only = no auth users = rsyncuser secrets file = /etc/rsync.pass [root@test ~]#
提示:uid,gid表示rsync這個服務是以那個用戶的身份去運行;use chroot表示是否可切換指定path里的路徑,如果是yes表示在傳輸文件之前首先 chroot 到 path 參數所指定的目錄下。這樣做的原因是實現額外的安全防護,但是缺點是需要 root並且不能備份指向 path 外部的符號連接所指向的目錄文件;max connections表示指定最大並發連接數,0表示不限制;ignore errors表示忽略守護進程上的I/O錯誤;exclude表示排除監控的資源列表,也就是exclude后面的資源不監控;log file、pid file、lock file指定日志文件,pid文件,lock文件的路徑;reverse lookup表示是否使用名稱解析(控制守護進程是否對客戶端IP地址執行反向查找以確定其主機名);hosts allow表示指定允許的主機或網段內的主機來訪問rsync服務器;[backup] 這個表示共享出來的目錄共享名稱,這個同samba的配置一樣,這個名稱只是對客戶端看到共享出來的目錄名稱,它不是真的服務器路徑名稱;path表示指定真實存放同步數據的路徑,也就是共享出去的名稱對應到系統上的那個目錄;comment表示描述說明;auth users表示那些用戶可以訪問這個目錄;secrets file表示auth users的用戶的密碼信息文件
5、准備備份數據目錄和創建secrets file所指定的用戶密碼文件,並把auth users所指定的用戶和密碼信息寫入到該文件中
[root@test ~]# mkdir /backup [root@test ~]# echo 'rsyncuser:admin' > /etc/rsync.pass [root@test ~]# cat /etc/rsync.pass rsyncuser:admin [root@test ~]# chmod 600 /etc/rsync.pass [root@test ~]# ll /etc/rsync.pass -rw------- 1 root root 16 1月 31 19:27 /etc/rsync.pass [root@test ~]#
提示:/etc/rsync.pass這個文件是secrets file指定的文件,它相當於一個虛擬的用戶文件,不需要存在Linux系統中,這個文件主要保存用於同步數據時,所用到的用戶;通常情況下為了rsync的用戶信息安全,建議把這個文件的權限修改成600
6、啟動rsync服務以Daemon方式運行,並檢查端口運行情況
[root@test ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 25 *:514 *:* LISTEN 0 128 *:41319 *:* LISTEN 0 50 *:3306 *:* LISTEN 0 128 *:111 *:* LISTEN 0 100 ::1:25 :::* LISTEN 0 25 :::514 :::* LISTEN 0 128 :::41319 :::* LISTEN 0 128 :::111 :::* LISTEN 0 128 :::80 :::* [root@test ~]# rsync --daemon [root@test ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 25 *:514 *:* LISTEN 0 128 *:41319 *:* LISTEN 0 5 *:873 *:* LISTEN 0 50 *:3306 *:* LISTEN 0 128 *:111 *:* LISTEN 0 100 ::1:25 :::* LISTEN 0 25 :::514 :::* LISTEN 0 128 :::41319 :::* LISTEN 0 5 :::873 :::* LISTEN 0 128 :::111 :::* LISTEN 0 128 :::80 :::* [root@test ~]#
提示:rsync默認端口是873 當然也可以通過在配置文件中用port 來指定
7、在客戶端上創建密碼文件
[root@test-centos6-node1 ~]# echo "admin" >/etc/rsync.pass [root@test-centos6-node1 ~]# chmod 600 /etc/rsync.pass [root@test-centos6-node1 ~]# ll /etc/rsync.pass -rw------- 1 root root 6 Feb 1 03:38 /etc/rsync.pass [root@test-centos6-node1 ~]#
8、在客戶端測試rsync是否可以把指定文件同步到rsync共享出來的backup下(這里的backup不是指服務器上的真實路徑,是中括號里指定的名稱,這個名稱可以隨便起,沒有特殊要求)
[root@test-centos6-node1 ~]# tree . └── test 1 directory, 0 files [root@test-centos6-node1 ~]# echo "this is test file" >test/f1 [root@test-centos6-node1 ~]# rsync -avz --password-file=/etc/rsync.pass /root/test/ rsyncuser@192.168.0.99::backup sending incremental file list ./ f1 sent 93 bytes received 30 bytes 246.00 bytes/sec total size is 18 speedup is 0.15 [root@test-centos6-node1 ~]#
提示:可以看到在客戶端已經把指定的資源給同步到rsync服務器上了,接下來我們看看rsync指定存放同步數據的目錄是否有客戶端同步過來的文件
[root@test ~]# ll /backup 總用量 4 -rw-r--r-- 1 root root 18 2月 1 2020 f1 [root@test ~]# cat /backup/f1 this is test file [root@test ~]#
提示:在服務端可以看到客戶端同步過來的文件;到此rsync服務就搭建完畢了。接下來我們編寫腳本結合inotify和rsync實現實時同步
五、在客戶端編寫腳本實現inotify+rsync實現實時同步
實驗環境:centos6是客戶端,在上面安裝inotify-tools工具;centos7是rsync服務端;實驗目的是實現實時監控centos6上的/root/test/目錄下的所有資源,如果有變化就把文件通過rsync推送到rsync服務器上,實現實時同步
1、在centos6上安裝inotify-tools
[root@test-centos6-node1 ~]# yum install -y inotify-tools Loaded plugins: fastestmirror Setting up Install Process Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com base | 3.7 kB 00:00 epel | 5.3 kB 00:00 extras | 3.4 kB 00:00 my_base | 4.0 kB 00:00 percona-release-noarch | 2.9 kB 00:00 percona-release-x86_64 | 2.9 kB 00:00 updates | 3.4 kB 00:00 Resolving Dependencies --> Running transaction check ---> Package inotify-tools.x86_64 0:3.14-2.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ========================================================================================================= Package Arch Version Repository Size ========================================================================================================= Installing: inotify-tools x86_64 3.14-2.el6 epel 46 k Transaction Summary ========================================================================================================= Install 1 Package(s) Total download size: 46 k Installed size: 104 k Downloading Packages: inotify-tools-3.14-2.el6.x86_64.rpm | 46 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : inotify-tools-3.14-2.el6.x86_64 1/1 Verifying : inotify-tools-3.14-2.el6.x86_64 1/1 Installed: inotify-tools.x86_64 0:3.14-2.el6 Complete! [root@test-centos6-node1 ~]#
2、編寫腳本
[root@test-centos6-node1 ~]# cat inotify_rsync.sh #!/bin/bash SRC='/root/test/' DEST='rsyncuser@192.168.0.99::backup' inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w %f' -e create,delete,moved_to,close_write ${SRC} |while read DATE TIME DIR FILE;do FILEPATH=${DIR}${FILE} rsync -az --delete --password-file=/etc/rsync.pass $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log done [root@test-centos6-node1 ~]#
提示:解釋下上面的腳本,首先我們定義要監控的目錄src和rsync的地址dest(這個地址就是客戶端rsync連接rsync服務端的地址),然后通過inotifywait來指定監控的資源和事件,以及輸出信息的格式,然后每一次觸發事件的輸出信息就交給while read處理,while read 把事件輸出信息分別用DATA TIME DIR FILE來保存;然后把DIR和FILE做合並,得到變化資源的真實路徑,然后通過rsync把變化的資源推到rsync服務器上,然后把對應的事件信息寫到/var/log/changelist.log中。
測試腳本是否能夠達到實時備份的效果
提示:在客戶端運行腳本,在rsync服務端監控/backup里的變化。然后在客戶端操作在/root/test/目錄下進行文件的增加刪除修改,看看是否能夠及時的同步到rysnc服務器上
提示:可以看到我們在客戶端新建一個文件是能夠及時的同步到rysnc服務器上的
提示:到此我們就實現了把客戶端的指定目錄下的資源通過inotify監控和rsync實時的把數據同步到rysnc服務器上。監控的事件我們可以在腳本里用inotifywait -e去指定即可;我們發現腳本運行,它是前端運行的,如果我們把CRT窗口關閉了,隨之我們的監控也失效了,所以我們希望腳本后台運行
腳本以后台方式運行
[root@test-centos6-node1 ~]# bash inotify_rsync.sh & [1] 1697 [root@test-centos6-node1 ~]#
提示:我們把腳本以后台方式執行也是可以正常的把客戶端指定目錄下的文件同步rsync服務器上,到此inotify+rsync實現實時數據同步就完成了。