This article describes how to use rsync + inotify to automatically synchronize webpages in linux. For more information, see how to use multiple web servers to achieve load balancing, to maintain resource consistency on the front-end web server, you can use rsync to synchronize updated files to other slave servers (read-only servers) on the master server (data can be written ), however, real-time synchronization cannot be performed automatically. inotify can be used for real-time synchronization.
Master server: 192.168.6.205 inotify
Slave server: 192.168.6.36 rsync
1. configure rsync on the slave server to enable rsync so that the master 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 the/etc/rsync. passwd password configuration file
Vim/etc/rsync. passwd
# User: password
Rsync: rsync
2. install inotify-tools on the master server
Tar-zxvf inotify-tools-3.14.tar.gz
Cd inotify-tools-3.14
./Configure-prefix =/usr/local/inotify
Make & make install
Configure the rsync password file on the master server to synchronize data to the slave server.
Vim/etc/rsync. passwd
# Password
Rsync
Create script
Vim inotifyrsync. sh
#! /Bin/bash
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
For more articles about automatic webpage synchronization through rsync + inotify in linux, please follow PHP's Chinese network!