(转)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,备没有找主解决延迟问题)
(转)Linux下通过rsync与inotify(异步文件系统事件监控机制)实现文件实时同步的更多相关文章
- linux下rsync+inotify实现服务器之间文件实时同步
先介绍一下rsync与inotify. 1.rsync 与传统的cp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例 ...
- linux系统中rsync+inotify实现服务器之间文件实时同步
最近需要对服务器上的文件实施动态备份,我又不想每次都手动来进行备份,在网上找了挺多资料,发现使用rsync就可以实现,如果想要实现实时同步,还可以使用rsync+inotify组合,本文就是以组合方式 ...
- rsync+inotify 实现服务器之间目录文件实时同步(转)
软件简介: 1.rsync 与传统的 cp. tar 备份方式相比,rsync 具有安全性高.备份迅速.支持增量备份等优点,通过 rsync 可 以解决对实时性要求不高的数据备份需求,例如定期的备份文 ...
- rsync+inotify实现服务器之间文件实时同步--转
之前做了“ssh信任与scp自动传输脚本”的技术文档,此方案是作为公司里备份的方法,但在实际的运行中,由于主服务器在给备份服务器传输的时候,我们的主服务器需要备份的文件是实时.不停的产生的,造成不知道 ...
- centos6.5 rsync+inotify实现服务器之间文件实时同步
1. rsync的优点与不足 与传统的cp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据 ...
- rsync + inotify 打造多server间文件实时同步
在上篇文章ssh无password登陆server的基础之上.能够利用rsync + Inotify 在多server间实现文件自己主动同步. 例如以下測试机基于三台server做的.内网IP分别例如 ...
- inotify+rsync文件实时同步
原文转自http://dl528888.blog.51cto.com/2382721/771533/ 之前做了“ssh信任与scp自动传输脚本”的技术文档,此方案是作为公司里备份的方法,但在实际的运行 ...
- rsync+inotify实现文件实时同步
一.相关组件简介 1.rsync 与传统的cp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器 ...
- Linux(10):期中架构(2)--- NFS存储服务 & 实时同步
1. 共享存储服务概念: # NFS是Network File System的缩写,中文意思是网络文件系统, # 它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录. 2. ...
随机推荐
- poj3274 Gold Balanced Lineup(HASH)
Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...
- 简单工厂(Simple Factory)模式
工厂模式专门负责将大量有共同接口的类实例化.工厂模式可以动态决定将哪一个类实例化,不必事先知道每次要实例化哪一个类.工厂模式有以下几种形态: 简单工厂(Simple Factory)模式 工厂方法(F ...
- Ubuntu16.04切换python3和python2
切换Python3为默认版本: sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 sudo ...
- CentOS 系统管理与yum软件仓库搭建
重启 reboot shutdown -r now init 6 关闭 init 0 shutdown -h now shutdown -h 20:25 #8点25关机查看内存 free CPU利用率 ...
- angular OnChange事件
import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core'; @Component({ sel ...
- 苹果微信内置浏览器cookie
苹果微信内置浏览器cookie会被自动清掉,但safari不会清除,原因还未找到,解决方法是把前端把数据通过header传到后台
- Task ContinueWith
前正无生意,且记Task.ContinueWith之用法. using System; using System.Collections.Generic; using System.Diagnosti ...
- golang subprocess tests
golang Subprocess tests Sometimes you need to test the behavior of a process, not just a function. f ...
- mac下redis和zookeeper启动及测试命令
mac下启动命令: sudo su - root cd /usr/local/bin/ ./redis-server ../etc/redis.conf cd /software/zook ...
- Educational Codeforces Round 62 (Rated for Div. 2)E(染色DP,构造,思维,组合数学)
#include<bits/stdc++.h>using namespace std;const long long mod=998244353;long long f[200007][2 ...