Rsync+intify实时同步

inotify企业工作场景:
  inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,使用的软件有inotify,sersync
具体操作:
  1.准备环境,配置好rsync,确定可以拉取推送文件,然后在客户端配inotify

[root@localhost ~]# uname -r #查看一下内核版本,要在2.6.13以上才行
[root@localhost ~]# ls -l /proc/sys/fs/inotify/ #显示三个文件则证明支持
-rw-r--r-- 1 root root 0 Sep 23 05:49 max_queued_events#监控的数量在这里
-rw-r--r-- 1 root root 0 Sep 23 05:49 max_user_instances#
-rw-r--r-- 1 root root 0 Sep 23 05:49 max_user_watches

  2.下载inotify的源码包

[root@localhost tools]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

  下载完记得看一下啊:

[root@localhost tools]# ls inotify-tools-3.14.tar.gz

  3.解压源码包并编译安装

[root@localhost tools]# tar -xf inotify-tools-3.14.tar.gz
[root@localhost tools]# cd inotify-tools-3.14
[root@localhost inotify-tools-3.14]# ./configure --prefix=/usr/local/inontify-tools-3.14
[root@localhost inotify-tools-3.14]# make&&make install

  4.创建软连接

[root@localhost tools]# ln -s /usr/local/inontify-tools-3.14 /usr/local/inontify
[root@localhost inontify]# ll
total 16
drwxr-xr-x 2 root root 4096 Sep 23 06:07 bin#inoyify执行命令(二进制)
drwxr-xr-x 3 root root 4096 Sep 23 06:07 include#inotify程序所需的头文件
drwxr-xr-x 2 root root 4096 Sep 23 06:07 lib#动态链路的库文件
drwxr-xr-x 4 root root 4096 Sep 23 06:07 share#帮助文件

----------------------------------------------------------------------------------------------------
inotifywait命令常用参数详情

-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.
-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.
-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.
-e|--event <event1> [ -e|--event <event2> ... ]#通过此参数可以指定需要监控的事件,如下所示:
Listen for specific event(s). If omitted, all events are 
listened for.#注意这个参数是配合下面的参数用的
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#文件系统被卸载

----------------------------------------------------------------------------------------------------------------
  5.人工测试监控事件
    测试create:

[root@localhost inontify]# /usr/local/inontify-tools-3.14/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /backup 

  再开一个连接然后在监控的目录建立文件,这样就可以看见原来的CRT窗口内容会改变

[root@localhost backup]# touch 6.txt
24/09/19 00:46 /backup/6.txt

  测试delete和create:

[root@localhost inontify]# /usr/local/inontify-tools-3.14/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete /backup 

  6.编写实时监控脚本
  脚本要放在一个固定的位置:/server/scriots下

[root@localhost scriots]# vim inotify.sh
#!/bin/sh
cmd="/usr/local/inontify/bin/inotifywait"
$cmd -mrq --format '%T %w%f' -e create,close_write,delete /backup|
while read line
do
rsync -az --deldet $line rsync_backup@192.168.157.132::oldboy --password-file=/etc/rsync.password
done

  7.事件相关参数大小:

[root@localhost scriots]# cat /proc/sys/fs/inotify/max_queued_events 
16384#改成327679
[root@localhost scriots]# cat /proc/sys/fs/inotify/max_user_instances 
128
[root@localhost scriots]# cat /proc/sys/fs/inotify/max_user_watches 
8192#改成50000000

inotify的缺点:
1.并发不能大于200个文件
2.一般的脚本,每次都是全部推送一次

原文地址:https://www.cnblogs.com/zrxuexi/p/11579423.html