rsync + inotify 实现远程实时同步数据

#确定一下安装环境,系统是否支持
[root@linux-node2 ~]# uname -r
2.6.32-642.6.1.el6.x86_64
[root@linux-node2 ~]# ls -l /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Aug 29 16:23 max_queued_events
-rw-r--r-- 1 root root 0 Aug 29 16:23 max_user_instances
-rw-r--r-- 1 root root 0 Aug 29 16:23 max_user_watches
[root@linux-node2 ~]#
#下载
[root@linux-node2 ~]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
#安装
[root@linux-node2 ~]# tar zxf inotify-tools-3.14.tar.gz
[root@linux-node2 ~]# cd inotify-tools-3.14
[root@linux-node2 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14
[root@linux-node2 inotify-tools-3.14]# make && make install
#做软连接
[root@linux-node2 inotify-tools-3.14]# ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools
[root@linux-node2 inotify-tools-3.14]# ls -l /usr/local/inotify-tools/
total 16
drwxr-xr-x 2 root root 4096 Aug 29 17:02 bin
drwxr-xr-x 3 root root 4096 Aug 29 17:02 include
drwxr-xr-x 2 root root 4096 Aug 29 17:02 lib
drwxr-xr-x 4 root root 4096 Aug 29 17:02 share
[root@linux-node2 inotify-tools-3.14]#

#检查
[root@linux-node2 inotify-tools-3.14]# cd /usr/local/inotify-tools
[root@linux-node2 inotify-tools]# ll
total 16
drwxr-xr-x 2 root root 4096 Aug 29 17:02 bin
drwxr-xr-x 3 root root 4096 Aug 29 17:02 include
drwxr-xr-x 2 root root 4096 Aug 29 17:02 lib
drwxr-xr-x 4 root root 4096 Aug 29 17:02 share
[root@linux-node2 inotify-tools]# ll bin
total 88
-rwxr-xr-x 1 root root 44271 Aug 29 17:02 inotifywait
-rwxr-xr-x 1 root root 41393 Aug 29 17:02 inotifywatch

#监控 /data 目录 create,delete
[root@linux-node2 ~]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e
create,delete /data


[root@linux-node2 data]# touch bbb.txt
[root@linux-node2 data]# rm -rf aaa.txt

[root@linux-node2 ~]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e
create,delete /data
29/08/18 17:16 /data/bbb.txt
29/08/18 17:16 /data/aaa.txt

##################################################################

实战脚本实时监测data目录
[root@linux-node2 scripts]# cat /server/scripts/inotify.sh
#!/bin/bash
inotify=/usr/local/inotify-tools/bin/inotifywait
$inotify -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,close_write,delete /data\
|while read file
do
cd /data &&
rsync -az ./ --delete [email protected]::data --password-file=/etc/rsync.password
done
[root@linux-node2 scripts]#

#添加到开机启动文件中
vi /etc/rc.loacl
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/bin/sh /server/scripts/inotify.sh &

#实战演示

[root@linux-node2 scripts]# /bin/sh /server/scripts/inotify.sh &
[1] 25537
[root@linux-node2 scripts]#

[root@linux-node2 data]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 29 17:35 a.txt
-rw-r--r-- 1 root root 0 Aug 29 17:35 b.txt
-rw-r--r-- 1 root root 0 Aug 29 17:35 c.txt
-rw-r--r-- 1 root root 0 Aug 29 17:35 d.txt
-rw-r--r-- 1 root root 0 Aug 29 17:35 e.txt
-rw-r--r-- 1 root root 0 Aug 29 17:35 f.txt
-rw-r--r-- 1 root root 0 Aug 29 17:35 g.txt
[root@linux-node2 data]# rm -rf {a..g}.txt
[root@linux-node2 data]# touch {b..g}.txt

#服务器端

[root@backup data]# ll
total 0
-rw-r--r-- 1 rsync rsync 0 Aug 29 17:35 a.txt
-rw-r--r-- 1 rsync rsync 0 Aug 29 17:35 b.txt
-rw-r--r-- 1 rsync rsync 0 Aug 29 17:35 c.txt
-rw-r--r-- 1 rsync rsync 0 Aug 29 17:35 d.txt
-rw-r--r-- 1 rsync rsync 0 Aug 29 17:35 e.txt
-rw-r--r-- 1 rsync rsync 0 Aug 29 17:35 f.txt
-rw-r--r-- 1 rsync rsync 0 Aug 29 17:35 g.txt
[root@backup data]# ll
total 0
[root@backup data]# ll
total 0
-rw-r--r-- 1 rsync rsync 0 Aug 29 17:53 b.txt
-rw-r--r-- 1 rsync rsync 0 Aug 29 17:53 c.txt
-rw-r--r-- 1 rsync rsync 0 Aug 29 17:53 d.txt
-rw-r--r-- 1 rsync rsync 0 Aug 29 17:53 e.txt
-rw-r--r-- 1 rsync rsync 0 Aug 29 17:53 f.txt
-rw-r--r-- 1 rsync rsync 0 Aug 29 17:53 g.txt

猜你喜欢

转载自www.cnblogs.com/ahtornado/p/9555988.html