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/
目錄 [隱藏]mysql
inotify-tools工具安裝
與rsync配合經過shell腳本實現同步
將inotify加入自動開機啓動服務中
inotify參數優化
rsync+intity壓力測試效果
rsync+inotify優缺點
高併發數據實時同步方案
inotify-tools工具安裝
查看系統支持:linux
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源碼包git
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
編譯安裝github
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
命令位置web
/usr/bin/inotifywait
inotifywait 能夠直接使用
/usr/bin/inotifywait
inotifywait 能夠直接使用
inotify安裝在rsync客戶端,監控到數據變化後經過rsync推給rsync服務端.sql
與rsync配合經過shell腳本實現同步mongodb
#!/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
簡易版shell
#!/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 腳本 &vim
將inotify加入自動開機啓動服務中bash
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,備沒有找主解決延遲問題)