linux system through rsync+inotify to achieve automatic page synchronization

  • 2020-05-10 23:18:27
  • OfStack

Multiple web servers are used to realize load balancing. In order to maintain the 1 uniqueness of the resources on the front-end web server, the updated files can be synchronized to other secondary servers (read-only servers) on the master server (writable data) through rsync, but real-time synchronization cannot be automatically carried out. Real-time synchronization can be realized with inotify

Master server: 192.168.6.205 inotify
Slave server: 192.168.6.36 rsync

1. Configure rsync on the slave server and enable rsync service 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 profile
vim /etc/rsync.passwd
User: password
rsync:rsync

2. Install inotify-tools on the primary 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 primary server to synchronize the data to the slave server
vim /etc/rsync.passwd
# your password
rsync

Create a 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 'H y m/d / % % % % : % M' � format 'w T % % % f - e modify, delete, create, attrib   $src   | while read files
do
          delete progress file=/etc/ rsync. passwd $src @$host::$dst
              echo "${files} was rsynced" > > /tmp/rsync.log 2 > &1
    done


Related articles: