(转)Linux下通过rsync与inotify(异步文件系统事件监控机制)实现文件实时同步

时间:2023-03-10 08:04:52
(转)Linux下通过rsync与inotify(异步文件系统事件监控机制)实现文件实时同步

Linux下通过rsync与inotify(异步文件系统事件监控机制)实现文件实时同步
原文:http://www.summerspacestation.com/linux%E4%B8%8B%E9%80%9A%E8%BF%87rsync%E4%B8%8Einotify%E5%BC%82%E6%AD%A5%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E4%BA%8B%E4%BB%B6%E7%9B%91%E6%8E%A7%E6%9C%BA%E5%88%B6%E5%AE%9E%E7%8E%B0%E6%96%87%E4%BB%B6/
目录 [隐藏]

inotify-tools工具安装
与rsync配合通过shell脚本实现同步
将inotify加入自动开机启动服务中
inotify参数优化
rsync+intity压力测试效果
rsync+inotify优缺点
高并发数据实时同步方案
inotify-tools工具安装
查看系统支持:

uname -r #系统内核至少达到2.6.13
ls -l /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 6月 20 13:17 max_queued_events
-rw-r--r-- 1 root root 0 6月 20 13:17 max_user_instances
-rw-r--r-- 1 root root 0 6月 20 13:17 max_user_watches
#显示这三个文件则证明支持

uname -r #系统内核至少达到2.6.13
ls -l /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 6月 20 13:17 max_queued_events
-rw-r--r-- 1 root root 0 6月 20 13:17 max_user_instances
-rw-r--r-- 1 root root 0 6月 20 13:17 max_user_watches
#显示这三个文件则证明支持
下载inotify源码包

mkdir -p /home/kendall/tools/
cd /home/kendall/tools/
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
ls inotify-tools-3.14.tar.gz

mkdir -p /home/kendall/tools/
cd /home/kendall/tools/
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
ls inotify-tools-3.14.tar.gz
编译安装

cd /home/kendall/tools/
tar zxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14/
./configure --prefix=/usr/local/inotify-tools-3.14
make && make install
echo $?
cd ../
ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools

cd /home/kendall/tools/
tar zxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14/
./configure --prefix=/usr/local/inotify-tools-3.14
make && make install
echo $?
cd ../
ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools
命令位置

/usr/bin/inotifywait
inotifywait 可以直接使用

/usr/bin/inotifywait
inotifywait 可以直接使用
inotify安装在rsync客户端,监控到数据变化后通过rsync推给rsync服务端.

与rsync配合通过shell脚本实现同步

#!/bin/bash
inotify=/usr/bin/inotifywait
Path=/data
IP=172.16.1.41
$inotify -mrq --format '%w%f' -e create,close_write,delete $Path \
|while read file
do
if [ -f $file ];then
rsync -az $file --delete rsync_backup@$IP::nfsbackup --password-file=/etc/rsync.password
else
cd $Path
rsync -az ./ --delete rsync_backup@$IP::nfsbackup --password-file=/etc/rsync.password
fi
done

#!/bin/bash
inotify=/usr/bin/inotifywait
Path=/data
IP=172.16.1.41
$inotify -mrq --format '%w%f' -e create,close_write,delete $Path \
|while read file
do
if [ -f $file ];then
rsync -az $file --delete rsync_backup@$IP::nfsbackup --password-file=/etc/rsync.password
else
cd $Path
rsync -az ./ --delete rsync_backup@$IP::nfsbackup --password-file=/etc/rsync.password
fi
done
简易版

#!/bin/bash
inotify=/usr/bin/inotifywait
$inotify -mrq --format '%w%f' -e create,close_write,delete /data \
|while read file
do
cd /data &&\
rsync -az ./ --delete rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password & #可以增加效率
done

#!/bin/bash
inotify=/usr/bin/inotifywait
$inotify -mrq --format '%w%f' -e create,close_write,delete /data \
|while read file
do
cd /data &&\
rsync -az ./ --delete rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password & #可以增加效率
done
后台运行,开机启动
/bin/sh 脚本 &

将inotify加入自动开机启动服务中

vim /etc/init.d/syncd
1
vim /etc/init.d/syncd

#!bin/bash
#chkconfig: 2345 38 46
#######################
#
#######################
. /etc/init.d/functions
if [ $# -ne 1 ];then
usage: $0 {start|stop}
exit 1
fi
case "$1" in
start)
/bin/bash /server/scripts/inotify.sh &
echo $$ >/var/run/inotify.pid
if [ `ps -ef|grep inotify|wc -l` -gt 2 ];then
action "inotify service is started" /bin/true
else
action "inotify service is started" /bin/false
fi
;;
stop)
kill -9 cat /var/ run/inotify.pid >/dev/null 2>&1
pkill inotifywait
sleep 2
if [ `ps -ef|grep inotify|grep -v grep|wc -l` -eq 0 ];then
action "inotify service is stopped" /bin/true
else
action "inotify service is stopped" /bin/false
fi
;;
*)
usage: $0 {start|stop}
exit 1
esac

#!bin/bash
#chkconfig: 2345 38 46
#######################
#
#######################
. /etc/init.d/functions

if [ $# -ne 1 ];then
usage: $0 {start|stop}
exit 1
fi

case "$1" in
start)
/bin/bash /server/scripts/inotify.sh &
echo $$ >/var/run/inotify.pid
if [ `ps -ef|grep inotify|wc -l` -gt 2 ];then
action "inotify service is started" /bin/true
else
action "inotify service is started" /bin/false
fi
;;
stop)
kill -9 cat /var/ run/inotify.pid >/dev/null 2>&1
pkill inotifywait
sleep 2
if [ `ps -ef|grep inotify|grep -v grep|wc -l` -eq 0 ];then
action "inotify service is stopped" /bin/true
else
action "inotify service is stopped" /bin/false
fi
;;
*)
usage: $0 {start|stop}
exit 1
esac

chmod +x /etc/init.d/syncd
chkconfig --add syncd
chkconfig syncd on
1
2
3
chmod +x /etc/init.d/syncd
chkconfig --add syncd
chkconfig syncd on
inotify参数优化
下面两条命令需要加入开机启动/etc/rc.local中

echo "50000000" > /proc/sys/fs/inotify/max_user_watches
echo "50000000" > /proc/sys/fs/inotify/max_queued_events
max_queued_events #队列容纳事件数量
max_user_instances #每个用户可以运行wait watch的数量
max_user_watches #最大监控文件数量
1
2
3
4
5
echo "50000000" > /proc/sys/fs/inotify/max_user_watches
echo "50000000" > /proc/sys/fs/inotify/max_queued_events
max_queued_events #队列容纳事件数量
max_user_instances #每个用户可以运行wait watch的数量
max_user_watches #最大监控文件数量
rsync+intity压力测试效果
每秒200文件以下并发,基本没有差异.

rsync+inotify优缺点
优点:

监控文件系统事件变化,通过同步工具实现实时数据同步

缺点:

并发如果大于200个文件(10-100k),同步会有延迟。
我们前面写的脚本,每次都是全部推送一次,但确实是增量的,也可以只同步变化的文件,不变化的不理。
监控到事件后,调用rsync同步是单进程的(加&并发),sersync多进程同步。
高并发数据实时同步方案
inotify(sersync)+rsync 文件级别
drbd 文件系统级别,基于block块同步 缺点:备份节点数据不可用
第三方软件同步功能:mysql同步,oracle mongodb
程序双写,直接写两台服务器
业务逻辑解决(【写主nfs,读备backup】读写分离,备读不到,读主 )防止延迟,弃用NFS方案,用web做nfs的备节点。效率提高很多
NFS集群(145方案整合)(双写主存储,备存储用inotify(sersync)+rsync,备没有找主解决延迟问题)