rsync+inotify每秒监测动态目录同步功能_2018_lcf

脚本功能:动态目录比对同步更新(主要是减少同步时对比的文件数,从而减少比对战用资源)

使用软件:

rsync-3.0.9-18.el7.x86_64(CentOS Linux release 7.4.1708自带)    inotify-tools-3.13.tar.gz(可以网上下载) ssh(系统自带)

同步条件:1.启用和设置ssh免密登陆

                   2.安装rsync和inotify-tools

                   3.编写同步条件脚本

1) ssh 这里就自己找文档去弄好了

2)设置rsync和安装inotify-tools

—A:设置rsync(关系到读写权限问题)

(这里两台机测试,分别是mysql1(192.168.20.10)  mysql2(192.168.20.11)  

如果要把当地目录同步到远程目录,就要修改下执行rsync命令的这台机子/etc/rsyncd.conf 这个文件后,重启服务

[root@mysql2 ~]# vim /etc/rsyncd.conf          //如果是远程目录同步到本地,就是读,就不用操作这步

uid = root
gid = root

 去掉注释,修改成root即可

[root@mysql2 ~]# systemctl restart rsyncd.service

—B:安装inotify-tools-3.13.tar.gz

inotifywait监控操作:

  • inotifywait [选项] 目标文件夹

inotifywait常用命令选项:

  • -m,持续监控(捕获一个事件后不退出)
  • -r,递归监控、包括子目录及文件
  • -q,减少屏幕输出信息
  • -e,指定监视的 modify、move、create、delete、attrib 等事件类别

[root@mysql1 ~]# tar xf inotify-tools-3.13.tar.gz  -C /usr/src/

[root@mysql1 ~]# cd  /usr/src/inotify-tools-3.13/  

[root@mysql1 ~]# ./configure

[root@mysql1 ~]# make  && make install       //这个单独执行make 和 make install 操作效果是一样的

[root@mysql1 ~]# echo '/usr/local/lib/libinotifytools.so.0' >> /etc/ld.so.conf

[root@mysql1 ~]# ldconfig

[root@mysql1 ~]#  inotifywait --help

inotifywait 3.13

......

 inotifywait -mrq -e create,modify,move,attrib,delete /opt   // 可以先测试是否有效

[1] 55564

2)编写脚本

#!/bin/bash
# --脚本主要通过把监控到的变化写入指定文件,再通过时间间隔读取这个文件.
# --获取更新了哪个目录,再对这个目录进行同步。
# --rsync.log监控操作日志,rsync_data.log数据同步日志。
# --JKFILES指定要监控的目录,YCIP指定要远程的IP(记得在远程主机先创建和本地一致的主目录)
:>/tmp/rsync.log
:>/tmp/rsync_data.log
    JKFILES=/data/www
    YCIP=172.17.171.10
    STEP=()
    i=0
    STEP[i]=1
    SNOW=0
RsyncCheck(){
    while :
    do
        SNOW=`cat /tmp/rsync.log | wc -l`
        temp=$SNOW
    sleep 1
        SNOW=`cat /tmp/rsync.log | wc -l`
        if [ $SNOW != $temp ];then
            let "i++"
            STEP[i]=$SNOW
            lists=(`sed -n ''$((temp+1))','${STEP[i]}'p' /tmp/rsync.log | awk '{print $1}'`)
            for li in ${lists[@]}
            do
                rsync -atvcr $li $YCIP:$li &>> /tmp/rsync_data.log
            done
        else
            SNOW=0
            temp=0
        fi
    done
}
RsyncCheck &
inotifywait -mrq --exclude ".*/website/public" -e create,modify,move,attrib,delete $JKFILES &>> /tmp/rsync.lo

[root@mysql2 ~]# /root/rsyncfiles.sh 

[root@mysql2 ~]# ps aux | grep rsync
root      5899  0.0  0.0 113128   844 pts/1    S    11:46   0:00 /bin/bash /root/rsyncfiles.sh
root      6765  0.0  0.0 112676   980 pts/1    R+   11:48   0:00 grep --color=auto rsync
[root@mysql2 ~]# kill 5899
这样就可以了

在两台机上测试一边建目录或更改文件,然后在另一台机上查看:

[root@mysql2 gg3]# cat /tmp/rsync.log 
[root@mysql2 gg3]# cat /tmp/rsync_data.log 
[root@mysql2 gg3]# cd /root/www/
[root@mysql2 www]# ls
dsfe  gg3  wfef
[root@mysql2 www]# mkdir 3425
[root@mysql2 www]# cat /tmp/rsync_data.log 
sending incremental file list
./
3425/

sent 237 bytes  received 29 bytes  532.00 bytes/sec
total size is 11  speedup is 0.04
[root@mysql2 www]# ls
3425  dsfe  gg3  wfef
[root@mysql2 www]# cd gg3
[root@mysql2 gg3]# ls
3  34  5  dsffwf  er  fgdfg  r  yy
[root@mysql2 gg3]# echo 'fdsfwe' >> yy
[root@mysql2 gg3]# cat yy
aaa
efsdfs
fdsfwe
[root@mysql2 gg3]# cat /tmp/rsync_data.log 
sending incremental file list
./
3425/

sent 237 bytes  received 29 bytes  532.00 bytes/sec
total size is 11  speedup is 0.04
sending incremental file list
yy

sent 211 bytes  received 44 bytes  510.00 bytes/sec
total size is 18  speedup is 0.07
[root@mysql1 gg3]# ls
3  34  5  dsffwf  er  fgdfg  r  yy
[root@mysql1 gg3]# cat yy 
aaa
efsdfs
[root@mysql1 gg3]# cat yy
aaa
efsdfs
fdsfwe
[root@mysql1 gg3]# cat yy 
aaa
efsdfs
fdsfwe

------------------------------------------

inotify-tools命令使用讲解

inotify-tools 是为linux下inotify文件监控工具提供的一套c的开发接口库函数,同时还提供了一系列的命令行工具,这些工具可以用来监控文件系统的事件。 inotify-tools是用c编写的,除了要求内核支持inotify外,不依赖于其他。inotify-tools提供两种工具,一是 inotifywait,它是用来监控文件或目录的变化,二是inotifywatch,它是用来统计文件系统访问的次数。现在介绍一下它的使用方法。

安装方法:

1

2

3

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

tar xzf inotify-tools-3.14.tar.gz ;cd inotify-tools-3.14

./configure --prefix=/usr && make && su -c 'make install'

使用例子:

inotifywait

1、实时监控/home的所有事件(包括文件的访问,写入,修改,删除等)

1

inotifywait -rm /home

2、监控/var/log/messeges中有关httpd的日志

1

2

3

4

5

6

#!/bin/sh

while inotifywait -e modify /var/log/messages; do

    if tail -n1 /var/log/messages | grep httpd; then

        kdialog --msgbox "Apache needs love!"

    fi

 done

inotifywatch

1、统计/home文件系统的事件

1

inotifywatch -v -e access -e modify -t 60 -r /home

参数说明

inotifywait

语法:
inotifywait [-hcmrq] [-e ] [-t ] [--format ] [--timefmt ] [ ... ]
参数:
-h,–help
输出帮助信息
@
排除不需要监视的文件,可以是相对路径,也可以是绝对路径。
–fromfile 
从文件读取需要监视的文件或排除的文件,一个文件一行,排除的文件以@开头。
-m, –monitor
接收到一个事情而不退出,无限期地执行。默认的行为是接收到一个事情后立即退出。
-d, –daemon
跟–monitor一样,除了是在后台运行,需要指定–outfile把事情输出到一个文件。也意味着使用了–syslog。
-o, –outfile 
输出事情到一个文件而不是标准输出。
-s, –syslog
输出错误信息到系统日志
-r, –recursive
监视一个目录下的所有子目录。
-q, –quiet
指定一次,不会输出详细信息,指定二次,除了致命错误,不会输出任何信息。
–exclude 
正则匹配需要排除的文件,大小写敏感。
–excludei 
正则匹配需要排除的文件,忽略大小写。
-t , –timeout 
设置超时时间,如果为0,则无限期地执行下去。
-e , –event 
指定监视的事件。
-c, –csv
输出csv格式。
–timefmt 
指定时间格式,用于–format选项中的%T格式。
–format 
指定输出格式。
%w 表示发生事件的目录
%f 表示发生事件的文件
%e 表示发生的事件
%Xe 事件以“X”分隔
%T 使用由–timefmt定义的时间格式

inotifywatch

语法:
inotifywatch [-hvzrqf] [-e ] [-t ] [-a ] [-d ] [ ... ]
参数:
-h, –help
输出帮助信息
-v, –verbose
输出详细信息
@
排除不需要监视的文件,可以是相对路径,也可以是绝对路径。
–fromfile 
从文件读取需要监视的文件或排除的文件,一个文件一行,排除的文件以@开头。
-z, –zero
输出表格的行和列,即使元素为空
–exclude 
正则匹配需要排除的文件,大小写敏感。
–excludei 
正则匹配需要排除的文件,忽略大小写。
-r, –recursive
监视一个目录下的所有子目录。
-t , –timeout 
设置超时时间
-e , –event 
只监听指定的事件。
-a , –ascending 
以指定事件升序排列。
-d , –descending 
以指定事件降序排列。

可监听事件

access 文件读取
modify 文件更改。
attrib 文件属性更改,如权限,时间戳等。
close_write 以可写模式打开的文件被关闭,不代表此文件一定已经写入数据。
close_nowrite 以只读模式打开的文件被关闭。
close 文件被关闭,不管它是如何打开的。
open 文件打开。
moved_to 一个文件或目录移动到监听的目录,即使是在同一目录内移动,此事件也触发。
moved_from 一个文件或目录移出监听的目录,即使是在同一目录内移动,此事件也触发。
move 包括moved_to和 moved_from
move_self 文件或目录被移除,之后不再监听此文件或目录。
create 文件或目录创建
delete 文件或目录删除
delete_self 文件或目录移除,之后不再监听此文件或目录
unmount 文件系统取消挂载,之后不再监听此文件系统。

猜你喜欢

转载自blog.csdn.net/cbuy888/article/details/81559428