rsync缺点/不足:

1.rsync在同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的,并且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。

2.rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加、删除、修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施监控的软件。

在使用rsync首次全量同步后,结合inotify对源目录进行实时监控,只有有文件变动或新文件产生,就会立刻同步到目标目录下,非常高效使用!

inotify :创建一个文件描述符,附加一个或多个监视器(一个监视器是一个路径和一组事件),然后用read方法从描述符获取事件,read并不会用完整个周期,它是事件发生之前是被阻塞的。

文件描述符:linux内核为了更优秀的管理被打开的文件创建的索引,是一个非负整数。用于指代被打开的文件,所有执行io的操作 系统调用都是通过文件描述符。0表示标准输入,1表示标准输出,2表示标准错误输出。

查看linux内核版本

需要看当前linux是否支持inotify[root@Rsync-139 ~]# uname -a

Linux Rsync-139 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

[root@Rsync-139 ~]# ls -lsart /proc/sys

sys/           sysrq-trigger  sysvipc/

[root@Rsync-139 ~]# ls -lsart /proc/sys/fs/inotify/

总用量 0

0 dr-xr-xr-x 0 root root 0 11月  5 15:31 ..

0 dr-xr-xr-x 0 root root 0 11月  6 16:12 .

0 -rw-r--r-- 1 root root 0 11月  6 16:12 max_user_watches

0 -rw-r--r-- 1 root root 0 11月  6 16:12 max_user_instances

0 -rw-r--r-- 1 root root 0 11月  6 16:12 max_queued_events

安装[root@Rsync-139 ~]# tar -zxvf inotify-tools-3.14.tar.gz -C /usr/local/src/

[root@Rsync-139 ~]# ./configrue --prefix=/usr/local/inotify

[root@Rsync-139 ~]# make &&make install

需要加环境变量vim /etc/profile

最低行加:/usr/local/inotify/bin/

重新加载配置文件. /etc/profile 或者

source /etc/profile

查看帮助信息

inotifywait --help[root@WebA-136 script]# 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.

@         Exclude the specified file from being watched.

--exclude

Exclude all events on files matching the

extended regular expression .

--excludei

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

Read files to watch from  or `-' for stdin.

-o|--outfile

Print events to  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   Print using a specified printf-like format

string; read the man page for more details.

--timefmt  strftime-compatible format string for use with

%T in --format string.

-c|--csv        Print events in CSV format.

-t|--timeout

When listening for a single event, time out after

waiting for an event for  seconds.

If  is 0, inotifywait will never time out.

-e|--event  [ -e|--event  ... ]

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

常用

-r 递归(目录)

-m 永久监听

-d 后台运行

-q 静默,只输出较少的信息

编写脚本进行实时备份vim inotify.sh

inotifywait -mrq --timefmt '%d%m%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move /var/log/ |while read file

do

rsync -az --delete-before /var/log/ rsync_WebA@192.168.146.139::WebA/ --password-file=/etc/WebA.pass

done

./inotify &放入后台执行即可

注:

rsync error: some files/attrs were not transferred (see previous errors) (code 23) at

main.c(1505)是因为在同步的时候,源目录下有软链接文件!

rsync同步软链接文件,应该加参数-l

linux实时备份,51CTO博客-专业IT技术博客创作平台-技术成就梦想相关推荐

  1. linux初学文档,51CTO博客-专业IT技术博客创作平台-技术成就梦想

    linux 里 一切皆文件 7种文件类型: 普通文件 f d 目录 b 块设备 /dev/sda /dev/sda1 /dev/sr0 c 字符设备 /dev/pts/0 /dev/ tty0 #tt ...

  2. linux a8启动过程,51CTO博客-专业IT技术博客创作平台-技术成就梦想

    Linux系统启动流程之kernel 1.内核参数修改方法: 2.内核内核模块管理: 3.内核编译 用户空间访问.监控内核的方式:/proc, /sys 伪文件系统 /proc/sys: 此目录中的文 ...

  3. linux定时刷新命令结果,51CTO博客-专业IT技术博客创作平台-技术成就梦想

    基本指令等: 部分快捷键: ctrl+c 强制结束当前运行程序,终止命令 ctrl+d 结束当前运行程序 先按ESC然后按. 或者同时按住ALT和. 则输入上一条命令的最后一个参数,与!$相同 Ctr ...

  4. linux vbox 不能使用scsi_id 查看uuid,51CTO博客-专业IT技术博客创作平台-技术成就梦想...

    今天在搭建RAC的时候,使用udev方式来创建ASM磁盘,执行/sbin/scsi_id命令不知道啥原因,死活获取不到UUID,执行结果啥也不显示. [root@seiang2 ~]# scsi_id ...

  5. linux主节点启动nfs,51CTO博客-专业IT技术博客创作平台-技术成就梦想

    Windows系统之间下以实现文件和目录的共享,那么在linux系统下面是否也可以实现了,我们就测试一下: 在linux下面实现目录共享的软件是nfs 要配置nfs服务首先要配置rsh服务才可以,具体 ...

  6. linux grep 快速,51CTO博客-专业IT技术博客创作平台-技术成就梦想

    什么是grep? grep (global search regular expression(RE) and print out the line,其全称意义为全局搜索正则表达式,并打印出来.是一种 ...

  7. linux httpd 域名映射,51CTO博客-专业IT技术博客创作平台-技术成就梦想

    一.DNS服务器的设置 我们知道互联网网是基于TCP/IP协议的,要进行通信必须获得对方的IP地址,这是通过DNS服务器来实现的.因此要想实现虚拟域名首先应当令DNS 服务器接受该虚拟域名,即把它映射 ...

  8. linux使用grep数字个数,51CTO博客-专业IT技术博客创作平台-技术成就梦想

    一.作业(练习)内容: 1.总结本此课程中所涉及命令的使用方法及相关示例展示: Linux文本处理三剑客: grep: 文本过滤工具: sed:文本编辑器(行):stream editor awk:文 ...

  9. pxe安装linux dhcp失败,51CTO博客-专业IT技术博客创作平台-技术成就梦想

    原理有必要说明一下           (百度偷来的) 原理和概念: 1.1 什么是PXE 严格来说,PXE 并不是一种安装方式,而是一种引导的方式.进行 PXE 安装的必要条件是要安装的计算机中包含 ...

最新文章

  1. cmake 成功后, make 出现 No such file or directory 问题解决
  2. android studio zbar,Android Studio 0.2.6和ZBar项目设置
  3. mysql ddl dcl_MySQL常用DDL、DML、DCL语言整理(附样例)
  4. leetcode 1052. 爱生气的书店老板(滑动窗口)
  5. iptable 详解_最全的iptables防火墙详解.pdf
  6. linux ntp 追赶,Linux 时间同步 ntpd
  7. Linux命令之awk:运算与判断(三)
  8. 2020-11-30 OpenCV人工智能图像处理学习笔记 第3章 计算机视觉加强之几何变换 warpAffine
  9. matlab前馈仿真,基于前馈神经网络的自适应PID控制器仿真研究(MATLAB仿真程序)
  10. labview软件+测试步骤,labview软件三取二逻辑测试平台.doc
  11. python处理excel数据画曲线图_python读取excel数据绘制简单曲线图的完整步骤记录...
  12. Openerp对象字段定义详解
  13. 【高级篇 / DNS】(7.0) ❀ 03. FortiGate作为Window DNS的备用DNS服务器 ❀ FortiGate 防火墙
  14. java面试常见设计模式
  15. L->data 与 L.data比较
  16. 多元线性回归分析spss结果解读_多元线性回归分析理论详解及SPSS结果分析
  17. 国内小程序生态服务平台即速应用完成5000万元A+轮融资...
  18. 用javascript自定义SharePoint文档库/列表项菜单
  19. 这里有 9 本 AI 书籍
  20. Navicat使用教程及安装教程

热门文章

  1. 【新技术】CentOS系统下docker的安装配置及使用详解
  2. 12.委托是什么?委托的property声明用什么属性?为什么?
  3. js 正则判断字符串是否为字母或数字
  4. 小小的吹一下集结号~
  5. 匿名内部类的使用实例
  6. C++程序设计之函数对象
  7. Fedora下网络配置及相关命令
  8. android文件存储教程,android开发基础教程—文件存储功能实现
  9. RabbitMQ 消息确认机制 以及 原理解析
  10. 003_SpringBoot整合Filter