Rsync and INotify
Single rsync can only synchronize data, a single inotify can only real-time monitoring files, the combination of both to meet the real-time data synchronization needs, the following is a data publisher and two Web server instance resolution rsync+inotify to achieve real-time synchronization.
Data Publishing Server 192.168.1.5 (rsync+inotify)
Web server 192.168.1.6 192.168.1.7 (rsync)
First deploy rsync on the Web server
192.168.1.6 Configuration
[Email protected]~]# yum install-y rsync
[Email protected]~]# mkdir-p/var/www/001
[Email protected]~]# chmod 660/var/www/001
[Email protected]~]# chown nobody.nobody/var/www/001
[[Email protected]~]# vim/etc/rsync.conf
Transfer logging = yes
Log file =/var/log/rsyncd.log
PID file =/var/run/rsyncd.pid
Lock file =/var/run/rsync.lock
UID = nobody
GID = Nobody
User Chroot = No
Ignore errors
Read Only = no
[Web1]
Comment = Web Comment
Path =/var/www/001
Auth users = Tom
Secrets file =/etc/rsyncd.passwd
Hosts allow=192.168.1.5
Hosts deny=*
List = False
[Email protected]~]# echo "tom:123456" >/ETC/RSYNCD.PASSWD
[Email protected]~]# chmod 600/etc/rsyncd.passwd
[[Email protected]~]# rsync--daemon
[[Email protected]~]# echo "rsync--daemon" >>/etc/rc.local
[[Email protected]~]# iptables-i input-p TCP--dport 873-j ACCEPT
[[Email protected]~]# service Iptables Save
192.168.1.7 Configuration
[Email protected]~]# yum install-y rsync
[Email protected]~]# mkdir-p/var/www/002
[Email protected]~]# chmod 660/var/www/002
[Email protected]~]# chown nobody.nobody/var/www/002
[[Email protected]~]# vim/etc/rsync.conf
Transfer logging = yes
Log file =/var/log/rsyncd.log
PID file =/var/run/rsyncd.pid
Lock file =/var/run/rsync.lock
UID = nobody
GID = Nobody
User Chroot = No
Ignore errors
Read Only = no
[WEB2]
Comment = Web Comment
Path =/var/www/002
Auth users = Tom
Secrets file =/etc/rsyncd.passwd
Hosts allow=192.168.1.5
Hosts deny=*
List = False
[Email protected]~]# echo "tom:123456" >/ETC/RSYNCD.PASSWD
[Email protected]~]# chmod 600/etc/rsyncd.passwd
[[Email protected]~]# rsync--daemon
[[Email protected]~]# echo "rsync--daemon" >>/etc/rc.local
[[Email protected]~]# iptables-i input-p TCP--dport 873-j ACCEPT
[[Email protected]~]# service Iptables Save
Then download Inotify-tool on the data publisher, install Rsync and inotify (192.168.1.5)
[Email protected]~]# yum install-y rsync
[Email protected]~]# yum install-y automake Libtool
[[Email protected]~]# cd/home/soft/inotify-tools-master
[Email protected] inotify-tools-master~]#./configure
[[email protected] inotify-tools-master~]# make && make install
[Email protected]~]# echo "123456" >/root/rsync.pass
[Email protected]~]# chmod 600/root/rsync.pass
[Email protected]~]# vim rsync_notify.sh
#!/bin/bash
Export Path=/bin:/usr/bin:/usr/local/bin
src=/home/webdata/
Dest1=web1
Dest2=web2
client1=192.168.1.6
client2=192.168.1.7
User=tom
Passfile=/root/rsync.pass
[!-e $Passfile] && exit 2
Inotifywait-mrq--timefmt '%y-%m-%d%h:%m '--format '%T%w%f%e '--event modify,create,delete,attrib $SRC |while Read line
Do
echo "$line" >/var/log/inotify_web 2>&1
/usr/bin/rsync-avz--delete--progress--password-file= $Passfile $SRC ${user}@ $Client 1:: $DEST 1 >>/var/log/sync _web1 2>&1
/usr/bin/rsync-avz--delete--progress--password-file= $Passfile $SRC ${user}@ $Client 2:: $DEST 2 >>/var/log/sync _WEB2 2>&1
Done &
[Email protected]~]# chmod a+x rsync_notify.sh
[Email protected]~]#./rsync_notify.sh
[Email protected]~]# echo "/root/rsync_notify.sh"/etc/rc.local
Inotifywait usage
inotifywait [-HCMRQ] [-e] [-t] [--format] [--timefmt] [...]
Parameters:
-h,–help
Output Help information
@
Exclude files that you do not need to monitor, either a relative path or an absolute path.
–fromfile
Read from a file a file that needs to be monitored or excluded, a file line, and the excluded files begin with @.
-m,–monitor
Receives a thing without exiting, executes indefinitely. The default behavior is to exit immediately after receiving a thing.
-d,–daemon
Like –monitor, in addition to running in the background, you need to specify –outfile to export things to a file. Also means the use of –syslog.
-o,–outfile
Output things to a file instead of the standard output.
-s,–syslog
Output error message to System log
-r,–recursive
Monitors all subdirectories in a directory.
-q,–quiet
Specified once, does not output details, specified two times, except for fatal errors, will not output any information.
–exclude
Regular matching files that need to be excluded are case-sensitive.
–excludei
Regular matches the files that need to be excluded, ignoring the case.
-T, –timeout
Set the time-out, if 0, to execute indefinitely.
-E, –event
Specifies the monitored events.
-c,–csv
Output CSV format.
–timefmt
Specifies the time format for the%t format in the –format option.
–format
Specifies the output 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
This article is from the "Practical Linux knowledge and Skills sharing" blog, please be sure to keep this source http://superleedo.blog.51cto.com/12164670/1889742
Rsync and inotify for real-time data synchronization