实验环境
(1) Rsync服务器:10.0.10.158
(2) Rsync客户端:10.0.10.173
Inotify都在客户端配置
1.查看系统是否支持inotify
# ls /proc/sys/fs/inotify/
max_queued_events max_user_instances max_user_watches #这些值一般不去修改但在监控的文件数量较大时要根据需求调整
# cat /proc/sys/fs/inotify/max_queued_events #设置inotify实例事件(event)队列可容纳的事件数量
16384
# cat /proc/sys/fs/inotify/max_user_instances #设置每个用户可以运行的inotifywait或inotifywatch命令的进程数
128
# cat /proc/sys/fs/inotify/max_user_watches #设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)
8192
2.安装inotify
# tar xvf inotify-tools-3.14.tar.gz
# cd inotify-tools-3.14
# ./configure --prefix=/usr/local/inotify
# make –j 2
# make install
3.设置系统环境变量
# vim /etc/profile
PATH=“$PATH:/usr/local/inotify/bin” #在文件中添加这行
# source /etc/profile
# vim /etc/ld.so.conf
/usr/local/inotify/lib #在文件中添加这行
# ldconfig
inotify工具与使用
# ls /usr/local/inotify/bin/
inotifywait inotifywatch
inotifywait
inotifywait用于等待文件或文件集上的一个特定事件,它可以监控任何文件和目录设置,并且可以递归地监控整个目录树。
inotifywatch
用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息。
inotifywait常用选项
-m,--monitor 表示始终保持事件监听状态
-r ,--recursive 表示递归查询目录
-q ,--quiet 表示打印出监控事件
-e ,--event 通过此参数可以指定要监控的事件
Inotifywait支持的事件类型
access 文件读取
modify 文件更改
attrib 文件属性更改,如权限,时间戳等
close_write 以可写模式打开的文件被关闭,不代表此文件一定已经写入数据
close_nowrite 以只读模式打开的文件被关闭
close 文件被关闭,不管它是如何打开的
open 文件打开
moved_to 一个文件或目录移动到监听的目录,即使是在同一目录内移动,此事件也触发
moved_from 一个文件或目录移出监听的目录,即使是在同一目录内移动,此事件也触发
move 包括moved_to和 moved_from
move_self 文件或目录被移除,之后不再监听此文件或目录
create 文件或目录创建
delete 文件或目录删除
delete_self 文件或目录移除,之后不再监听此文件或目录
unmount 文件系统取消挂载,之后不再监听此文件系统
--timefmt
%Y 表示年份
%m 表示月份
%d 表示天数
%H 表示小时
%M 表示分钟
--format
%w 表示发生事件的目录
%f 表示发生事件的文件
%e 表示发生的事件
%Xe 事件以“X”分隔
%T 使用由–timefmt定义的时间格式
Rsync+Inotify连续监控脚本
# vim /usr/local/inotify/bin/webroot-bk.sh
#!/bin/bash
webserver=10.0.10.158
src="/var/www/html/"
dst1="webroot"
username=admin
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%y-%m-%d %H:%M' \
--format '%T%w%f%e' -e close_write,delete,create,attrib ${src} \
| while read files
do
rsync -vzrtopg --delete --progress --password-file=/etc/rsync/password $src $username@$webserver::$dst1 &>/dev/null && echo "${files} was rsynced" &>> /var/log/rsyncd.log || echo "Error: ${files} not rsynced" &>> /var/log/rsyncd.log
done
运行与验证
# ./webroot-bk.sh & #脚本放在后台运行
[1] 6559
# cd /var/www/html/
# ls
haha index.html new.html page_2.html page.html
# touch inotify_new.html #客户端上新建文件
# cat /var/log/rsyncd.log #查看日志
16-06-30 16:11/var/www/html/inotify_new.htmlCREATE was rsynced
16-06-30 16:11/var/www/html/inotify_new.htmlATTRIB was rsynced
16-06-30 16:11/var/www/html/inotify_new.htmlCLOSE_WRITE,CLOSE was rsynced
# mkdir /var/www/html/test_inotify #客户端上新建目录
# cat /var/log/rsyncd.log
16-06-30 16:11/var/www/html/inotify_new.htmlCREATE was rsynced
16-06-30 16:11/var/www/html/inotify_new.htmlATTRIB was rsynced
16-06-30 16:11/var/www/html/inotify_new.htmlCLOSE_WRITE,CLOSE was rsynced
16-06-30 16:21/var/www/html/test_inotifyCREATE,ISDIR was rsynced
# ls /var/www/html/ #回到服务器上看是不是已经同步了
haha index.html inotify_new.html new.html new_inotify.html page_2.html page.html secret test_inotify
rsync+inotify百万级文件实时同步
实验环境:Centos7.4 目的:将源服务器的文件实时同步至目标服务器 源服务器:10.11.1.107 目标服务器:10.11.1.106 分别在两个节点安装rsync yum -y instal ...
rsync+inotify实现主机之间目录实时同步
原理: rsync:用于跨主机目录同步 inotify:用于监测目录变化 再编写一个触发脚本,一旦inotify检测到目录中内容发生变化,则调用rsync执行同步. rsync服务器的的配置: 因为r ...
rsync命令比对文件及增量同步
A fast,versatile,remote (and local) file-copying tool. rsync基于ssh协议实现高效率远程或本地文件复制,传输速度比scp快.复制文件时会比对 ...
通过inotify监控linux文件系统变化
http://www.mjmwired.net/kernel/Documentation/filesystems/inotify.txt http://www.ibm.com/developerwor ...
rsync+inotify 实现服务器之间目录文件实时同步(转)
软件简介: 1.rsync 与传统的 cp. tar 备份方式相比,rsync 具有安全性高.备份迅速.支持增量备份等优点,通过 rsync 可 以解决对实时性要求不高的数据备份需求,例如定期的备份文 ...
rsync+inotify文件同步
rsync+inotify文件同步 在服务器中,通常结合计划任务.shell脚本来执行本地备份.为了进一步提高备份的可靠性,使用异地备份也是非常重要的,利用rsync工具,可以实现快速.高效的异地备份 ...
nfs+rsync+inotify实现文件的实时同步
准备三台服务器进行测试: nfs_server:192.168.12.110 web_server:192.168.12.111 rsync_server:192.168.12.112 网络规划图: ...
rsync+inotify实现服务器数据同步
一.什么是rsync rsync,remote synchronize是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限.时间.软硬链接等附加信息.rsync是用 “rsync算 ...
rsync + inotify-tools实现文件的实时同步
文章摘自:http://lxw66.blog.51cto.com/5547576/1331048 rsync 帮助文档:http://man.linuxde.net/rsync 最近有个想法就是部署一 ...
随机推荐
用for循环打印菱形
package nothh; public class mmm { public static void main(String[] args) { //for循环内的 for按顺序运算,先打印1/4 ...
bzoj1391
很像最大权闭合子图的题目s向每个工作连边,流量为收益每个工序,由工作i向对应机器连边,流量为租用费每个机器向t连边,流量为购买费显然跑最小割,ans=总收益-mincut ; type node=re ...
json-lib-2.4-jdk15.jar maven
最近自己将一个web项目装换到使用mevan自动管理. 遇到了一个json包导入的问题.最终解决如下: