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. 福大软工1816|K班—alpha冲刺

    Part.1 开篇 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Part.2 成员汇报 组员1(组长)柯奇豪 过去两天完成了哪些任务 了解前端方面的相关内容,便于后续对进 ...

  2. Sharepoint2013搜索学习笔记之搜索构架简单概述(一)

    Sharepoint搜索引擎主要由6种组件构成,他们分别是爬网组件,内容处理组件,分析处理组件,索引组件,查询处理组件,搜索管理组件.可以将这6种组件分别部署到Sharepoint场内的多个服务器上, ...

  3. Go 的垃圾回收机制在实践中有哪些需要注意的地方(转)

    在网上看到一篇非常好的文章http://www.zhihu.com/question/21615032,转载如下: go的gc还不完善但也不算不靠谱,关键看怎么用,尽量不要创建大量对象,也尽量不要频繁 ...

  4. 关于 ASP.NET 的 CompilationMode="Never" 性能问题

    今天在优化系统性的时候,想到了 ASP.NET 里的 CompilationMode="Never", 因为用户控件里没有任何代码, 只有控件,把用户控件的编译模式改为 Never ...

  5. .net core MVC 通过 Filters 过滤器拦截请求及响应内容

    前提: 需要nuget   Microsoft.Extensions.Logging.Log4Net.AspNetCore   2.2.6: Swashbuckle.AspNetCore 我暂时用的是 ...

  6. Daily translation 3th

    Source url:http://www.nzherald.co.nz/education/news/article.cfm?c_id=35&objectid=11149719 //plac ...

  7. form表单以get方式提交时action中?后面的参数部分不生效

    form表单的提交方式是get方式,action="?sss=test",问号后面参数是接受不到的,谨记!

  8. iOS11.0后APP的图标和启动图

    随着Xcode9的更新,APP的图标和启动图也发生了略微变化,下面介绍下图标和启动图的设置. 1.APP图标: 这些是系统默认你开发的项目支持iPad.Spotlight等,其实真正我们的项目只要支持 ...

  9. MySQL中查询时对字母大小写的区分

    我相信很多人在mysql中查询时都遇到过mysql不区分字母大小写的情况:如以下例子: 1.SELECT * FROM `user` WHERE userpass = 'Z20'; 结果为: 2.SE ...

  10. memcached 和 redis 安装

    memcached 1.搭建好lnmp 2.安装依赖包 yum install -y libevent-devel 3.安装memcached $ cd /usr/local/src $ wget h ...