For the configuration of rsync, please refer to the blog:http://www.cnblogs.com/snsdzjlz320/p/5630695.html
Experimental environment
(1) rsync server: 10.0.10.158
(2) rsync client: 10.0.10.173
I notify are configured on the client side
1. Check if the system supports inotify
# ls/proc/sys/fs/inotify/ max_queued_events max_user_instances max_user_watches # These values are generally not modified but should be adjusted according to demand when the number of files being monitored is large # cat /proc/sys/fs/inotify/max_queued_events #设置inotify实例事件 (event) Number of events The queue can hold 16384# cat /proc/sys/fs/inotify/max_user_instances # Set the number of processes per user that can run the inotifywait or Inotifywatch command 128# cat /proc/sys/fs/inotify/max_user_watches # Set the number of files that can be monitored by the inotifywait or Inotifywatch command (single process) 8192
2. Installing INotify
# tar xvf inotify-tools-3.14.tar.gz# cd inotify-tools-3.14#./configure--prefix=/usr/local/inotify# Make–j Install
3. Setting System environment variables
# vim/etc/profile path= "$PATH:/usr/local/inotify/bin" #在文件中添加这行 # source/etc/profile# vim/etc/ld.so.conf /usr/local/inotify/lib #在文件中添加这行 # Ldconfig
INotify Tools and use
# ls/usr/local/inotify/bin/ inotifywait
Inotifywait
Inotifywait is used to wait for a specific event on a file or set of files, it can monitor any file and directory settings, and can recursively monitor the entire directory tree.
Inotifywatch
Used to collect monitored file system statistics, including the number of inferior information for each inotify event.
Inotifywait Common Options
-m,--monitor indicates that the event listening state is always maintained
-R,--recursive means recursive query directory
-Q,--quiet indicates that a monitoring event is printed out
-E,--event Use this parameter to specify which events to monitor
Types of events supported by inotifywait
Access file read
Modify file Changes
Attrib file property changes, such as permissions, timestamps, etc.
Close_write file opened in writable mode is closed, does not mean that the file must have been written to the data
Close_nowrite files opened in read-only mode are closed
The close file is closed, regardless of how it was opened
Open file opens
Moved_to a file or directory is moved to a listening directory, even if it is moved within the same directory, this event triggers
Moved_from a file or directory is moved out of the listening directory, even if it is moved within the same directory, this event triggers
Move includes moved_to and Moved_from
Move_self file or directory is removed and no longer listens for this file or directory
Create file or directory creation
Delete file or directory deletion
Delete_self file or directory is removed and then no longer listens for this file or directory
Unmount file system is mounted and no longer listens for this file system
--timefmt
%Y represents the year
%m represents the Month
%d indicates the number of days
%H = Hour
%M = minutes
--format
%w indicates the directory where the event occurred
%f indicates the file in which the event occurred
%e indicates that an event occurred
%xe events are separated by "X"
%T using the time format defined by –TIMEFMT
rsync+inotify Continuous monitoring Scripts
# VIM/USR/LOCAL/INOTIFY/BIN/WEBROOT-BK.SH#!/bin/Bashwebserver=10.0.10.158src="/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}| whileRead Files Dorsync-VZRTOPG--delete--progress--password-file=/etc/rsync/password $src [email protected] $webserver:: $DST 1 &>/dev/NULL&&Echo "${files} was rsynced"&>>/var/log/rsyncd.log | |Echo "Error: ${files} not rsynced"&>>/var/log/Rsyncd.log Done
Run and validate
#./webroot-bk.sh & #脚本放在后台运行 [1] 6559# cd/var/www/html/# lshaha 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 rsynced16-06-30 16:11/var/www/html/inotify_new.htmlattrib was rsynced16-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 rsynced16-06-30 16:11/var/www/html/inotify_ New.htmlattrib was rsynced16-06-30 16:11/var/www/html/inotify_new.htmlclose_write,close was rsynced16-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]--monitors client file changes, rsync synchronizes to server