全网数据实时备份方案【inotify,sersync】


环境搭建

0.环境安装   gcc  yum install gcc -y

1.安装inotify(源码软件包)


1.  cd /opt/tools/inotify-tools-3.14/

2. ./configure --prefix=/opt/tools/inotify-tools-3.14/

3. yum install -y gcc

4. make && make install

5. ln -s /opt/tools/inotify-tools-3.14/ /opt/tools/inotify-tools

6. ls -l /opt/tools/inotify-tools

2.inotify重要工具

    1.查看目录

          cd /opt/tools/inotify-tools/bin     -->lib是动态链接的库文件

全网数据实时备份方案[inotify,sersync]_实时同步

   2.inotifywait:在被监控的文件或者目录上等待特定文件系统事件(delete,open,close等)的发生,然后处于阻塞状态,适合shell脚本使用

     inotifywatch:收集被监视文件系统使用度统计数据,指文件系统事件发生的次数统计

3.inotifywait命令常用参数详解 

-r|--recursive  Watch directories recursively.

-m|--monitor Keep listening for events forever. Without this option, inotifywait will exit after one event is received.

-t|--timeout <seconds>

-q|--quiet Print less (only print events).

-qq Print nothing (not even events)

--fromfile <file> Read files to watch from <file> or `-' for stdin

-e|--event <event1>

access| modify| attrib| open| close| create| delete|unmount

4.inotify实战(实时同步,最好加上--delete)

  1.手动调用(inotify)

/opt/tools/inotify-tools/bin/inotifywait -mrq --timefmt '%d%m%y %H:%M' --format '%T %w%f' -e create,close_write,delete,  /data_inotify |while read line; do echo $line >>/var/log/inotify.log;done &

全网数据实时备份方案[inotify,sersync]_文件系统_02

  2.脚本实现(rsync+inotify+nfs)实时监控备份NFS

#!/bin/sh
inotifywait=/opt/tools/inotify-tools/bin/inotifywait
#count=1
$inotifywait -mrq --format '%w%f' -e create,close_write,delete /NFS |while read line
do
cd / &&
#echo ".................Starting rsync $line......................."
rsync -az /NFS rsync_backup@192.168.25.141::backup --password-file=/etc/rsync.password
done

  3.配合NFS实现监控

/opt/tools/inotify-tools/bin/inotifywait -mrq --timefmt '%y-%m-%d %H:%M' --format '%T %w%f' -e create,delete  /NFS_141 |while read line; do echo $line >>/var/log/inotify.log;done &

4.写入环境变量中,可以直接使用

cp  /opt/tools/inotify-tools/bin/inotifywait inotifywait

全网数据实时备份方案[inotify,sersync]_实时同步_03

6.关键参数说明

cd /proc/sys/fs/inotify/     -->有3个重要的目录,对inotify机制有一定的限制

max_queued_events:inotify可以监听的文件数量

max_user_instances:设置每个用户可运行的inotify进程数量

max_user_watches:设置inotify实例事件队列可容纳的事件数量

10k -100k的文件的并发是 200左右

  7.inotify优缺点


优点:配合rsync实现实时数据同步

缺点:大于200K的时候有延迟,可以使用sersync解决

调用rsync同步时单进程的


8.sersync功能:


1.配置文件    2.真正的守护进程 socket  3.可以对失败的文件定时重传(定时任务)

4.第三方HTTP接口 5.默认多线程 


9.高并发数据实时同步方案小结:


1.inotify(sersync)+rsync  -->文件级别

2.drdb           -->文件系统级别

3.第三方软件的同步功能    mysql级别,oracle,mongodb

4.程序双写                -->一个文件,从前台分别写入2个或者多个服务器

5.业务逻辑解决



作者:​​小a玖拾柒​​​ ​

-------------------------------------------

个性签名: 所有的事情到最後都是好的,如果不好,那說明事情還沒有到最後~

本文版权归作者【​​小a玖拾柒​​,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利!