Inotify + rsync for real-time network data synchronization on the CentOS Server
Instance:
Assume that web01 and web02 are the company's websites. We add data to the content publishing server. inotify detects that data updates are transmitted to web01 and web02 in real time through rsync.
1. web configuration (the configurations of web01 and web02 are almost the same. Pay attention to the red part)
# Vi/etc/rsyncd. conf
# Filename:/etc/rsyncd. conf
Uid = nobody
Gid = nobody
Use chroot = no
Max connections = 10
# Set password
Strict modes = yes
Pid file =/var/run/rsyncd. pid
Lock file =/var/run/rsyncd. lock
Log file =/var/log/rsyncd. log
[Web_01]
Path =/var/www/html
Comment = web dir
Ignore errors
Read only = no
Write only = no
Hosts allow = 172.16.10.0/255.255.255.0
Hosts deny = *
List = false
Auth = backup
Secrets file =/etc/rsyncd. auth
# Echo backup: backup>/etc/rsyncd. auth
# Chmod 600/etc/rsyncd. auth
Ii. Content Publishing Server Configuration
// Install inotify-tool # tar zxvf inotify-tools-3.14.tar.gz # cd inotify-tools-3.14 #. /configure; make install // create the rsync verification file # echo backup: backup>/etc/rsyncd. auth # chmod 600/etc/rsyncd. auth
[root@10.1 ~]# vi inotify_rsync.sh#!/bin/bash# This rsync script based on inotify.export PATH=/bin:/usr/bin:/usr/local/binsrc=/var/www/html/dst=/var/www/html/client1=172.16.10.101client2=172.16.10.102user=backup#password file must not be other-accessible.Passfiles=/etc/rsyncd.auth#Wait for changeinotifywait -mrq --timefmt '%d-%m-%y %H:%M' --format '%T %w %fi %e' --event modify,delete,create,attrib $src|while read files dorsync -vzrtopg --progress --delete --password-file=$Passfiles $src $user@$client1::$dstrsync -vzrtopg --progress --delete --password-file=$Passfiles $src $user@$client2::$dst echo "${files} was rsynced" >> /var/log/inotify_web 2>&1done
Run the preceding shell script in the background and modify the file on the content publishing server.
Log Overview
# cat /var/log/inotify_web 12-09-14 14:41 /var/www/html test_01i MODIFY was rsynced12-09-14 14:42 /var/www/html test_01i MODIFY was rsynced12-09-14 14:43 /var/www/html auditdi CREATE was rsynced12-09-14 14:43 /var/www/html auditdi MODIFY was rsynced12-09-14 14:43 /var/www/html blk-availabilityi CREATE was rsynced12-09-14 14:43 /var/www/html blk-availabilityi MODIFY was rsynced12-09-14 14:43 /var/www/html cgconfigi CREATE was rsynced12-09-14 14:43 /var/www/html cgconfigi MODIFY was rsynced12-09-14 14:43 /var/www/html cgredi CREATE was rsynced12-09-14 14:43 /var/www/html cgredi MODIFY was rsynced12-09-14 14:43 /var/www/html crondi CREATE was rsynced12-09-14 14:43 /var/www/html crondi MODIFY was rsynced12-09-14 14:43 /var/www/html drbdi CREATE was rsynced12-09-14 14:43 /var/www/html drbdi MODIFY was rsynced12-09-14 14:43 /var/www/html fcoei CREATE was rsynced12-09-14 14:43 /var/www/html fcoei MODIFY was rsynced12-09-14 14:43 /var/www/html functionsi CREATE was rsynced12-09-14 14:43 /var/www/html functionsi MODIFY was rsynced12-09-14 14:43 /var/www/html halti CREATE was rsynced12-09-14 14:43 /var/www/html halti MODIFY was rsynced12-09-14 14:43 /var/www/html htcachecleani CREATE was rsynced12-09-14 14:43 /var/www/html htcachecleani MODIFY was rsynced