[rsync+inotify]——监控客户端文件变化,rsync同步到服务器

关于rsync的配置请参考博文:http://www.cnblogs.com/snsdzjlz320/p/5630695.html

 

实验环境

    (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

 

posted @ 2016-06-30 20:40  Jelly_lyj  阅读(332)  评论(0编辑  收藏  举报