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,备没有找主解决延迟问题)

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

  1. linux下rsync+inotify实现服务器之间文件实时同步

    先介绍一下rsync与inotify. 1.rsync 与传统的cp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例 ...

  2. linux系统中rsync+inotify实现服务器之间文件实时同步

    最近需要对服务器上的文件实施动态备份,我又不想每次都手动来进行备份,在网上找了挺多资料,发现使用rsync就可以实现,如果想要实现实时同步,还可以使用rsync+inotify组合,本文就是以组合方式 ...

  3. rsync+inotify 实现服务器之间目录文件实时同步(转)

    软件简介: 1.rsync 与传统的 cp. tar 备份方式相比,rsync 具有安全性高.备份迅速.支持增量备份等优点,通过 rsync 可 以解决对实时性要求不高的数据备份需求,例如定期的备份文 ...

  4. rsync+inotify实现服务器之间文件实时同步--转

    之前做了“ssh信任与scp自动传输脚本”的技术文档,此方案是作为公司里备份的方法,但在实际的运行中,由于主服务器在给备份服务器传输的时候,我们的主服务器需要备份的文件是实时.不停的产生的,造成不知道 ...

  5. centos6.5 rsync+inotify实现服务器之间文件实时同步

    1. rsync的优点与不足 与传统的cp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据 ...

  6. rsync + inotify 打造多server间文件实时同步

    在上篇文章ssh无password登陆server的基础之上.能够利用rsync + Inotify 在多server间实现文件自己主动同步. 例如以下測试机基于三台server做的.内网IP分别例如 ...

  7. inotify+rsync文件实时同步

    原文转自http://dl528888.blog.51cto.com/2382721/771533/ 之前做了“ssh信任与scp自动传输脚本”的技术文档,此方案是作为公司里备份的方法,但在实际的运行 ...

  8. rsync+inotify实现文件实时同步

    一.相关组件简介 1.rsync 与传统的cp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器 ...

  9. Linux(10):期中架构(2)--- NFS存储服务 & 实时同步

    1. 共享存储服务概念: # NFS是Network File System的缩写,中文意思是网络文件系统, # 它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录. 2. ...

随机推荐

  1. 【题解】 UVa11300 Spreading the Wealth

    题目大意 圆桌旁边坐着\(n\)个人,每个人有一定数量的金币,金币的总数能被\(n\)整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数量相等.您的任务是求出被转手的金币的数量的最小值. ...

  2. subset子集全排序问题

    思路一 可以用递推的思想,观察S=[], S =[1], S = [1, 2] 时解的变化. 可以发现S=[1, 2] 的解就是 把S = [1]的所有解末尾添上2,然后再并上S = [1]里面的原有 ...

  3. 和Webbrowser进行简单交互

    作为第一篇,简单的控件使用就不说了. 直接从简单的交互开始吧! C#使用网页中已有的js函数 webBrowser.Document.InvokeScript("Stop");// ...

  4. python-循环(while循环、for循环)

    循环:循环会重复执行循环体里面的代码,python中循环可分为while循环和for循环. break 不管循环有没有完成,立即结束循环 continue 结束本次循环,继续进行下一次循环 一.whi ...

  5. mybatis 学习笔记(四):mybatis 和 spring 的整合

    mybatis 学习笔记(四):mybatis 和 spring 的整合 尝试一下整合 mybatis 和 spring. 思路 spring通过单例方式管理SqlSessionFactory. sp ...

  6. HDU6300-2018ACM暑假多校联合训练1003-Triangle Partition

    题意是给3n个点,其中不可能存在任意三点共线的情况,让你在其中建n个三角形,点不能重复使用,三角形不能相互覆盖 做法是给每个点排序,按照先y轴排,再x轴排的顺序,三个三个一组从下往上输出,有人说是凸包 ...

  7. javascript的代码块

    a block of code 注意到这个问题是在看书的时候,中文版中出现“代码片段”这样的词语,于是就去翻看了英文版的原书.书中的用了a block of code,难道不应该翻译成代码块吗?(作为 ...

  8. Qt 学习之路 2(58):编辑数据库外键

    Qt 学习之路 2(58):编辑数据库外键(skip) 豆子 2013年7月12日 Qt 学习之路 2 13条评论 前面几章我们介绍了如何对数据库进行操作以及如何使用图形界面展示数据库数据.本章我们将 ...

  9. 【KMP】洛谷P2375 [NOI2014]动物园 题解

        一开始的方向应该对了,但是没有想到合理的优化还是没写出来…… 题目描述 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己 ...

  10. Tarjan算法打包总结(求强连通分量、割点和Tarjan-LCA)

    目录 Tarjan打包总结(求强连通分量.割点和Tarjan-LCA) 强连通分量&缩点 原理 伪代码 板子(C++) 割点 原理 伪代码 最近公共祖先(LCA) 原理 伪代码 板子 Tarj ...