Cent OS 5.4 rsync+inotify配置触发式(实时)远程同步

软件:rsync-2.6.8-3.1(一般系统默认安装)、inotify-tools-3.13.tar.gz

主机:Linux-Master:192.168.0.75(源主机)、Linux-Slave:192.168.0.82

 

1、介绍

Inotify 是文件系统事件监控机制,作为 dnotify 的有效替代。dnotify 是较早内核支持的文件监控机制。Inotify 是一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能。

inotify 可以监视的文件系统事件包括:

IN_ACCESS,即文件被访问

IN_MODIFY,文件被 write

IN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等

IN_CLOSE_WRITE,可写文件被 close

IN_CLOSE_NOWRITE,不可写文件被 close

IN_OPEN,文件被 open

IN_MOVED_FROM,文件被移走,如 mv

IN_MOVED_TO,文件被移来,如 mv、cp

IN_CREATE,创建新文件

IN_DELETE,文件被删除,如 rm

IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己

IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己

IN_UNMOUNT,宿主文件系统被 umount

IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)

IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO)

注:上面所说的文件也包括目录。 

2、安装软件
在源主机中安装inotify-tools-3.13.tar.gz
[root@Linux-Master src]# tar zxvf inotify-tools-3.13.tar.gz
[root@Linux-Master src]# cd inotify-tools-3.13
[root@Linux-Master inotify-tools-3.13]# ./configure --prefix=/usr/local/inotify
[root@Linux-Master inotify-tools-3.13]# make && make install
=======================================================
3、生产SSH KEY   #若用rsync同步可省略此步
[root@Linux-Master ~]# ssh-keygen -t rsa
这个命令生成一个密钥对:id_rsa(私钥文件)和id_rsa.pub(公钥文件)。默认被保存在~/.ssh/目录下。
[root@Linux-Master ~]# scp ~/.ssh/id_rsa.pub root@192.168.0.82:~/.ssh/
[root@Linux-Slave src]# cat ~/.ssh/id_rsa.pub >> authorized_keys
[root@Linux-Slave src]#/etc/init.d/sshd restart
=======================================================
4、编写脚本 /usr/local/bin/inotify.sh
#!/bin/bash
########### ssh 传输
srcdir="/home/inotify"
ip="192.168.0.82"        #多台服务器可以自己添加IP
dstdir="/home/"

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${srcdir} | while read file
do
        for i in ${ip}
        do
        rsync -aqztH --delete -progress '-e ssh -p 50023' ${srcdir} root@${i}:${dstdir}
        done
done



#!/bin/bash
########### rsync 传输
srcdir="/data_backup/"
ip="192.168.0.13"               #多台服务器可以自己添加IP
dstdir="database_51"/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${srcdir} | whi
le read file
do
        for i in ${ip}
        do
#               rsync -aqztH --delete -progress '-e ssh -p 50023' ${srcdir} root@${i}:${dstdir}
                rsync -lHvzrtopg --delete --progress --password-file=/etc/rsyncd_52.pas ${srcdir} xiaomo@${i}::${dstdir}
                if [ $? -ne 0 ]
                then
                    subject="211.100. *.*'s postgresql PITR rsync error"
                    body="211.100. *.*+postgresql+PITR+rsync+error"
                    wget --spider "http://mail.XXX.com/sendmail.php?tomail=${maillist}&subject=${subject}&body=${body}" > /dev/null
 2>&1
                    /usr/bin/curl http://210.51.*.*:7878/b4_fetion.php?message=${body} > /dev/null 2>&1
                    exit 1
                fi

        done
done



被同步端 rsync配置:
uid=root
gid=root
maxconnections=10
usechroot=no
logfile=/var/log/rsyncd.log
pidfile=/var/run/rsyncd.pid
lockfile=/var/run/rsyncd.lock


[voices]
path=/var/www/voices
comment=voices
ignoreerrors=yes
readonly=no
hostsallow=192.168.0.0/24
hostsdeny=*

[database_51]
path=/data_backup_51
comment=database-in-211.100.*.51
ignoreerrors=yes
readonly=no
hostsallow=192.168.0.0/24
hostsdeny=*

[database_52]
path=/data_backup_52
comment=database-in-115.85. *.52
ignoreerrors=yes
readonly=no
hostsallow=115.85.192.0/24
hostsdeny=*

 

posted @ 2017-08-05 18:03  yijiaotu  阅读(72)  评论(0编辑  收藏  举报