Home >Backend Development >PHP Tutorial >Automatic synchronization of web pages through rsync+inotify in Linux system
Use multiple web servers to achieve load balancing. In order to maintain the consistency of resources on the front-end web server, updated files can be synchronized to other slave servers (read-only servers) through rsync on the master server (data can be written). , but it cannot automatically perform real-time synchronization. Real-time synchronization can be achieved using inotify.
Master server: 192.168.6.205 inotify
Slave server: 192.168.6.36 rsync
1. On the slave server Configure rsync and enable the rsync service so that the main service can synchronize resources to the server
vim /etc/rsyncd.conf
uid = nginx
gid = nginx
port = 873
host all = 192.168.6.205
use chroot = on
max connections = 4
timeout = yes
[wordpress]
path = /usr/local/nginx/html/wordpress
comment = rsync files
ignore errors
read only = no
list = yes
auth users = rsync
secrets file = /etc/rsync.passwd
Create /etc/rsync.passwd password configuration file
vim /etc/rsync.passwd
#User: password
rsync:rsync
2. Install inotify-tools## on the main server
#tar -zxvf inotify-tools-3.14.tar.gzcd inotify-tools-3.14
./configure –prefix=/usr/local/inotify
make && make install
vim /etc/rsync.passwd
#Password
rsync
vim inotifyrsync.sh
host=192.168.6.36
src=/usr/local/nginx/html/wordpress/
dst=wordpress
user =rsync
inotifywait=/usr/local/inotify/bin/inotifywait
rsync=/usr/bin/rsync
$inotifywait -mrq –timefmt '%d/%m/%y %H:% M' –format '%T %w%f' -e modify,delete,create,attrib $src | while read files
do
$rsync -vzrtopg –delete –progress –password-file=/etc/ rsync.passwd $src $user@$host::$dst
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done