study-15:inotify + rsync 实时同步

来源:互联网 发布:剑桥ccdc数据库 编辑:程序博客网 时间:2024/06/02 06:23

一,inotify简介:是一种强大的、细粒度的、异步的文件系统事件监控机制。可以监控文件系统中添加、删除、修改、移动等各种事件;而rsync配置为crontab最短时间也需要1分钟同步一次,无法做到实时同步;

客户端的inotify进程会监控目录内文件的变化,然后通知执行 rsync 命令同步数据到服务器端

inotify实现的常用的软件工具:inotify, sersync 


二,开始安装(客户端)

先检查rsync daemon 服务配置成功,可以在客户端推送拉取数据;检查 linux内核版本是否到达2.6.13;


1,查看当前系统是否支持inotify

ls -l /proc/sys/fs/inotify/

-rw-r--r-- 1 root root 0 Dec 13 15:18 max_queued_events   (需要调正大一些)
-rw-r--r-- 1 root root 0 Dec 13 15:18 max_user_instances
-rw-r--r-- 1 root root 0 Dec 13 15:18 max_user_watches


2,下载inotify源码包

 wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz


3,编译安装 inotify

tar zxvf inotify-tools-3.14.tar.gz

cd inotify-tools-3.14

./configure --prefix=/usr/local/inotify-tools-3.14

make && make install

cd ../

ln -s /usr/local/inotify-tools-3.14   /usr/local/inotify

cd /usr/local/inotify


[root@B inotify]# tree
.
|-- bin
|   |-- inotifywait     --->  主要命令
|   `-- inotifywatch  ---->  主要命令
|-- include
|   `-- inotifytools
|       |-- inotify-nosys.h
|       |-- inotify.h
|       `-- inotifytools.h
|-- lib
|   |-- libinotifytools.a
|   |-- libinotifytools.la
|   |-- libinotifytools.so -> libinotifytools.so.0.4.1
|   |-- libinotifytools.so.0 -> libinotifytools.so.0.4.1
|   `-- libinotifytools.so.0.4.1
`-- share
    |-- doc
    |   `-- inotify-tools
    |       |-- doxygen.css
    |       |-- doxygen.png
    |       |-- files.html
    |       |-- globals.html
    |       |-- globals_func.html
    |       |-- index.html
    |       |-- inotifytools_8c_source.html
    |       |-- inotifytools_8h.html
    |       |-- inotifytools_8h_source.html
    |       |-- pages.html
    |       |-- tab_b.gif
    |       |-- tab_l.gif
    |       |-- tab_r.gif
    |       |-- tabs.css
    |       `-- todo.html
    `-- man
        `-- man1
            |-- inotifywait.1
            `-- inotifywatch.1


./bin/inotifywait --help 




4,手工测试监控inotify的功能

1)第一个窗口执行: /usr/local/inotify-tools-3.14/bin/inotifywait -mrq --timefmt '%d/%m/%d %H:%M' --format '%T %w%f' -e create,deleate   /backup  (backup是监控的目录)

去掉时间参数:/usr/local/inotify-tools-3.14/bin/inotifywait -mrq --format '%w%f' -e create,deleate   /backup

2)第二个窗口对/backup 做 touch  , rm  ;都可以在第一个窗口看见


5,编写脚本测试监控,并执行sh -x inotify.sh  (缺点:一个文件变化了,全部文件都推送一次,不太好)

#!/bin/sh


host01=192.168.1.245
src=/backup
dst=oldboy
user=rsync_backup
rsync_passfile=/etc/rsync.password
inotify_home=/usr/local/inotify-tools-3.14/


${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
        cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} > /dev/null 2>&1
done
exit 0


6,缺点:rsync同步的文件比如10~100k,并发不能大于200个文件,工作中,压力测试很重要,测到有延迟时就是临界点,需要关注这个点;


7,补充:总结企业linux运维场景数据同步方案

1)文件级别同步方案

SCP , NFS , SFTP , HTTP , SAMBA , RSYNC(单向同步) , CSYNC2 , UNION(两个机器之间同步)

http://oldboy.blog.51cto.com/2561410/775056

文件及级别也可以利用mysql , mongodb等软件


2)文件系统级别同步

DRBD(基于文件系统同步网络RAID1),缺点是备节点不可以接管

mysql数据库的官方推荐drbd同步数据;所有的单点服务例如 NFS , MFS (DRBD)等都可以用drbd


3),数据库同步方案

a,自身同步机制

mysql replication (mysql主从复制,逻辑的基于SQL语句从写)

oracle dataguard(物理的基于磁盘块,逻辑的基于SQL语句从写)

b,第三方drbd,参考:http://oldboy.blog.51cto.com/2561410/1240412



0 0