使用rsync可以实现数据同步,但是即使使用crontab定时任务最小执行间隔为1分钟,在数据实时性要求比较高场合需使用inotify+rsync实现实时同步

  下载inotify

wget https://github.s3.amazonaws.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

  安装

tar -xf inotify-tools-3.14.tar.gz
yum -y install gcc-c++
./configure --prefix=/usr/local/inotify-tools-3.14
make && make install
ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify

  显示帮助

 /usr/local/inotify/bin/inotifywait --help

  

inotifywait 3.14
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:
-h|--help Show this help text.
@<file> Exclude the specified file from being watched.
--exclude <pattern>
Exclude all events on files matching the
extended regular expression <pattern>.
--excludei <pattern>
Like --exclude but case insensitive.
-m|--monitor Keep listening for events forever. Without
this option, inotifywait will exit after one
event is received.
-d|--daemon Same as --monitor, except run in the background
logging events to a file specified by --outfile.
Implies --syslog.
-r|--recursive Watch directories recursively.
--fromfile <file>
Read files to watch from <file> or `-' for stdin.
-o|--outfile <file>
Print events to <file> rather than stdout.
-s|--syslog Send errors to syslog rather than stderr.
-q|--quiet Print less (only print events).
-qq Print nothing (not even events).
--format <fmt> Print using a specified printf-like format
string; read the man page for more details.
--timefmt <fmt> strftime-compatible format string for use with
%T in --format string.
-c|--csv Print events in CSV format.
-t|--timeout <seconds>
When listening for a single event, time out after
waiting for an event for <seconds> seconds.
If <seconds> is 0, inotifywait will never time out.
-e|--event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s). If omitted, all events are
listened for. Exit status:
0 - An event you asked to watch for was received.
1 - An event you did not ask to watch for was received
(usually delete_self or unmount), or some error occurred.
2 - The --timeout option was given and no events occurred
in the specified interval of time. Events:
access file or directory contents were read
modify file or directory contents were written
attrib file or directory attributes changed
close_write file or directory closed, after being opened in
writeable mode
close_nowrite file or directory closed, after being opened in
read-only mode
close file or directory closed, regardless of read/write mode
open file or directory opened
moved_to file or directory moved to watched directory
moved_from file or directory moved from watched directory
move file or directory moved to or from watched directory
create file or directory created within watched directory
delete file or directory deleted within watched directory
delete_self file or directory was deleted
unmount file system containing file or directory unmounted

  监控一个文件夹,开启另外一个窗口创建修改删除文件

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f ' -e create,delete,close_write,modify /data/

  创建删除打开修改或者重定向都会有输出

 touch test
vim test

  显示如下

  写一个脚本配合rsync实现数据同步

#!/bin/bash
inotify=/usr/local/inotify/bin/inotifywait
$inotify -mrq --format '%w%f' -e create,close_write,delete,motify /data \
|while read file
do
cd / &&
rsync -az ./data --delete rsync_backup@192.168.1.4::backup/ \
--password-file=/etc/rsync.password
done

  rsync安装配置参考文档 https://www.cnblogs.com/minseo/p/8080426.html

  把该脚本设置在后台及开机启动运行即可实现实时的数据同步

Inotify+rsync实现实时数据同步的更多相关文章

  1. Linux学习系列之Inotify+Rsync实现实时数据同步

    Inotify简介 inotify介绍 inotify是一种强大的.异步的文件系统监控机制,linux内核从2.6.13起,加入了inotify的支持,通过inotify可以监控文件系统中添加.删除. ...

  2. centos7部署inotify与rsync实现实时数据同步

    实验环境:CentOS Linux release 7.6.1810 node1:192.168.216.130 客户端(向服务端发起数据同步) node2:192.168.216.132 服务端(接 ...

  3. Linux系统实时数据同步inotify+rsync

    一.inotify简介 inotify是Linux内核的一个功能,它能监控文件系统的变化,比如删除.读.写和卸载等操作.它监控到这些事件的发生后会默认往标准输出打印事件信息.要使用inotify,Li ...

  4. inotify+rsync实现实时同步

    第1章 数据实时同步介绍 1.1 什么是实时同步:如何实现实时同步 A. 要利用监控服务(inotify),监控同步数据服务器目录中信息的变化 B. 发现目录中数据产生变化,就利用rsync服务推送到 ...

  5. 【转】inotify+rsync实现实时同步

    [转]inotify+rsync实现实时同步 1.1 什么是实时同步:如何实现实时同步 要利用监控服务(inotify),监控同步数据服务器目录中信息的变化 发现目录中数据产生变化,就利用rsync服 ...

  6. rsync+inotify实时数据同步多目录实战

    rsync+inotify实时数据同步多目录实战       inotify配置是建立在rsync服务基础上的配置过程 操作系统 主机名 网卡eth0 默认网关 用途 root@58server1 1 ...

  7. rsync+inotify实时数据同步单目录实战

    rsync+inotify实时数据同步单目录实战   rsync+inotify实时数据同步单目录实战 inotify是一个强大的.细粒度的.异步的文件系统事件监控机制,linux内核从2.6.13起 ...

  8. 【linux运维】rsync+inotify与sersync+rsync实时数据同步笔记

    Rsync(remote sync)远程同步工具,通过rsync可以实现对远程服务器数据的增量备份通过,但rsync自身也有缺陷,同步数据时,rsync采用核心算法对远程服务器的目标文件进行对比,只进 ...

  9. inotify+rsync实现实时同步(附解决crontab中无法执行python脚本的问题)

    1.准备环境 # 系统支持的话,下面的目录就会存在 ls /proc/sys/fs/inotify/ rpm -qa inotify-tools yum -y install inotify-tool ...

随机推荐

  1. 从n个元素中选择k个的所有组合(包含重复元素)

    LeetCode:Combinations这篇博客中给出了不包含重复元素求组合的5种解法.我们在这些解法的基础上修改以支持包含重复元素的情况.对于这种情况,首先肯定要对数组排序,以下不再强调 修改算法 ...

  2. Flume采集Nginx日志到HDFS

    下载apache-flume-1.7.0-bin.tar.gz,用 tar -zxvf 解压,在/etc/profile文件中增加设置: export FLUME_HOME=/opt/apache-f ...

  3. addEventListener 第三个参数

    addEventListener api target.addEventListener(type, listener[, options]); target.addEventListener(typ ...

  4. 基于Centos搭建Jenkins 环境搭建

    系统要求: CentOS 7.2 64 位操作系统 安装 Jenkins Jenkins 简介 Jenkins 是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提 ...

  5. CentOS下网卡启动、配置等ifcfg-eth0教程(转)

    步骤1.配置/etc/sysconfig/network-scripts/ifcfg-eth0 里的文件.it动力的CentOS下的ifcfg-eth0的配置详情: [root@localhost ~ ...

  6. JDK自带JVM性能调优监控工具jps、jstack、jmap、jhat、jstat

    原文地址:https://www.jianshu.com/p/db954cb968fb JVM性能调优监控工具jps.jstack.jmap.jhat.jstat位于JDK的bin目录,这些工具短小精 ...

  7. sed 简明教程 (转)

    sed 简明教程 2013年2月20日   awk于1977年出生,今年36岁本命年,sed比awk大2-3岁,awk就像林妹妹,sed就是宝玉哥哥了.所以 林妹妹跳了个Topless,他的哥哥sed ...

  8. 开始逐步补充下相关Web知识,很多年没搞了....

    <script type="text/javascript"> $(function(){ ShowProduct(); $("#ShowUserInfo&q ...

  9. Approx Analytic Arealight

    试着实现的基于分析方法的近似面积光源.

  10. 假设分配给命令的连接位于本地挂起事务中,ExecuteReader 要求命令拥有事务。命令的 Transaction 属性尚未初始化

    {System.InvalidOperationException: 假设分配给命令的连接位于本地挂起事务中.ExecuteReader 要求命令拥有事务.命令的 Transaction 属性尚未初始 ...