rsync & inotify-tools 实时同步

1、根据之前一篇关于rsync的随笔部署好rsync服务后,可以开始inotify的部署

2、inotify的部署使用

  ①、检查系统是否支持inotify

[root@iZ25w1kdi5zZ ~]# ls -lsart /proc/sys/fs/inotify
total 0
0 dr-xr-xr-x 0 root root 0 Sep 19 09:38 ..
0 -rw-r--r-- 1 root root 0 Jan  1 13:51 max_user_watches
0 -rw-r--r-- 1 root root 0 Jan  1 13:51 max_user_instances
0 -rw-r--r-- 1 root root 0 Jan  1 13:51 max_queued_events
0 dr-xr-xr-x 0 root root 0 Jan  1 13:51 .

如果出现上面结果说明系统支持inotify。

  ②、安装

tar -zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14/
./configure --prefix=/usr/local/inotify-tools-3.14
make && make install
ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools

  ③、inotify参数

  * 说明:

    max_user_instances:每个用户创建inotify实例最大值

    max_queued_events:inotify队列最大长度,如果值太小,会出现错误,导致监控文件不准确

    max_user_watches:要知道同步的文件包含的目录数

  * 查看默认参数

[root@11 scripts]# sysctl -a|egrep "inotify.max_queued_events|inotify.max_user_watches|epoll.max_user_watches|inotify.max_user_instances"
fs.epoll.max_user_watches = 201011
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 128
fs.inotify.max_user_watches = 8192
sysctl: reading key "net.ipv6.conf.all.stable_secret"
sysctl: reading key "net.ipv6.conf.default.stable_secret"
sysctl: reading key "net.ipv6.conf.enp0s3.stable_secret"
sysctl: reading key "net.ipv6.conf.enp0s8.stable_secret"
sysctl: reading key "net.ipv6.conf.lo.stable_secret"
sysctl: reading key "net.ipv6.conf.virbr0.stable_secret"
sysctl: reading key "net.ipv6.conf.virbr0-nic.stable_secret"

  * 修改参数

  -命令修改

  sysctl -w fs.inotify.max_user_instances=130

[root@11 scripts]# sysctl -w fs.inotify.max_user_instances=65535
fs.inotify.max_user_instances = 65535
[root@11 scripts]# sysctl -w fs.inotify.max_user_watches=99999999
fs.inotify.max_user_watches = 99999999
[root@11 scripts]# sysctl -w fs.inotify.max_queued_events=99999999
fs.inotify.max_queued_events = 99999999

  -配置文件修改

[root@tmp]# vi /etc/sysctl.conf
#添加如下代码
fs.inotify.max_user_instances=130

  ④、创建实时监控脚本

##例一
[root@11 scripts]# cat inotify.sh
#!/bin/sh
cmd="/usr/local/inotify-tools/bin/inotifywait"
$cmd -mrq --format '%w%f' -e create,close_write,delete /backup|
while read line
do
  cd /backup &&
  rsync -az --delete --timeout=100 ./ rsync_user@10.0.0.10::www --password-file=/etc/rsync.password
done
View Code
##例子二
    [root@iZ25w1kdi5zZ ~]# mkdir -p /opt/scripts
     
    [root@iZ25w1kdi5zZ ~]# cd /opt/scripts
     
    [root@iZ25w1kdi5zZ scripts]# vi inotify_start.sh
    /usr/local/inotify/bin/inotifywait -mrq -e modify,create,move,delete 
    --fromfile '/opt/scripts/ffile' 
    --timefmt '%y-%m-%d %H:%M' --format '%T %f %e' 
    --outfile '/tmp/rsync.log'

    inotifywait常用参数:
    --timefmt 时间格式
    %y年 %m月 %d日 %H小时 %M分钟
    --format 输出格式
    %T时间 %w路径 %f文件名 %e状态
    -m 始终保持监听状态,默认触发事件即退出。
    -r 递归查询目录
    -q 打印出监控事件
    -e 定义监控的事件,可用参数:
    open 打开文件
    access 访问文件
    modify 修改文件
    delete 删除文件
    create 新建文件
    attrb  属性变更
View Code
原文地址:https://www.cnblogs.com/jpinsz/p/10842846.html