Server A:
Forum master server, run DZ X2 forum program; Server B: Forum from server, need to put X2 picture attachment and MySQL data real-time from a master server to B server. The MySQL sync settings are in the next section. The following is a picture for synchronizing two servers in real time.
Because the general rsync requires Cron to periodically run the SH script to achieve synchronization, this poses some problems. For example, users upload a picture from the master server, which takes at least a minute to display from the server. Since the Linux 2.6 kernel, the inotify mechanism has been supported, When some files or folders have changed, issue a corresponding event so that a third-party program can handle the operation as long as it subscribes to these events. At this time, as long as the file is modified, the implementation of a Rsnyn, the modified file to actively upload to another server on it.
I am using Google's inotify-tools, relatively simple. Domestic active power can be very powerful similar program, but it is very complicated. Also need to note that if you use Inotify-tools to achieve real-time synchronization, our primary server--the source file server (that is, Server a) Implementation is rsync from the server, our server-target synchronization servers (server B) is Rsync's primary server. Don't get mixed up.
Starting with primary Server A, you need to determine if your system supports inotify:
Copy Code code as follows:
Ll/proc/sys/fs/inotify
Total 0
-rw-r--r--1 Root 0, 4 17:56 max_queued_events
-rw-r--r--1 Root 0, 4 17:56 max_user_instances
-rw-r--r--1 Root 0, 4 17:56 max_user_watches
Can output this kind of result to express support.
Download and install Inotify-tools:
Copy Code code as follows:
wget--no-check-certificate http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
Tar xzvf inotify-tools-3.14.tar.gz
CD inotify-tools-3.14
./configure--PREFIX=/USR
Make
Make install
Complete the installation of the Inotify-tools.
Next you need to write two sh scripts, inotify_init.sh and inotify_monitor.sh:
Inotify_init.sh is used for the first initialization, which is to run a full rsync synchronization.
vi/root/inotify_init.sh
Copy Code code as follows:
#!/bin/sh
src=/Primary Server A requires synchronized directory/#记得在最后面加/otherwise Rync will automatically add a level of directory
Des=bbsatt
ip= IP from Server B
User=rsync
#DST the directory under the =/ETC/RSYNCD remote rsync module
Inwt=/usr/bin/inotifywait
Rsync=/usr/bin/rsync
$RSYNC-zahqt--password-file=/root/rsync.pwd $SRC $USER @ $IP:: $DES
Save exit.
To set executable permissions:
chmod +x/root/inotify_init.sh
The following is inotify_monitor.sh, which is used to subscribe to file modification events. Note that, for special reasons, I'm doing incremental backup + Live sync, which means that when the picture on Master A is deleted, the picture is not deleted from Server B.
vi/root/inotify_monitor.sh
Copy Code code as follows:
#!/bin/bash
##---------------------
Sync[0]= '/primary server needs to sync directory/, from Server B's Ip,bbsatt,rsync ' # Localdir,host,rsync_module,auth_user
Inwt=/usr/bin/inotifywait
Rsync=/usr/bin/rsync
Pass=/root/rsync.pwd
##---------------------
For item in ${sync[@]}; Todo
Dir= ' echo $item | Awk-f "," ' {print $} '
Host= ' echo $item | Awk-f "," ' {print $} '
Module= ' echo $item | Awk-f "," ' {print $} '
User= ' echo $item | Awk-f "," ' {print $} '
$INWT-mrq--timefmt '%d/%m/%y%h:%m '--format '%T%w%f '
--event Close_write,create,move $dir | While the Read date time file event
Todo
#echo $event '-' $file
Case $event in
modify| create| move| modify,isdir| create,isdir| Modify,isdir)
If ["${file:-4}"!= ' 4913 '] && ["${file:-1}"!= ']; Then
cmd= "$RSYNC-zahqzt--exclude= ' *"--password-file= $PASS
--include= $file $dir $user @ $host:: $module >/dev/null 2>1& "
Echo $cmd
$cmd
Fi
Moved_from| moved_from,isdir| Delete,isdir)
If ["${file:-4}"!= ' 4913 '] && ["${file:-1}"!= ']; Then
cmd= "$RSYNC-zahqzt--password-file= $PASS--exclude= $file \
$dir $user @ $host:: $module >/dev/null 2>1& "
Echo $cmd
$cmd
Fi
Esac
Done &
Done
chmod +x/root/inotify_monitor.sh
Set rsync Automatic Logon authentication password
Vi/root/rsync.pwd
Xxxxxx
Save, exit
Set permissions that only root can view.
chmod 0600/root/rsync.pwd
The following is the configuration of the standby (s) B:
Install Rsync
Yum Rsync-y
#----Configure RSNYD Services
Vi/etc/rsyncd.conf
Content as follows, you need to change Apache to run the site's user name, I was because the original use of Apache, although now with Nginx, also has not changed to username:
Copy Code code as follows:
UID = Apache
GID = Apache
Use chroot = no
Max connections = 4
PID file =/var/run/rsyncd.pid
Lock file =/var/run/rsync.lock
Log file =/var/log/rsyncd.log
[Bbsatt]
Path =/directory from server B for storing backups locally
Ignore errors
Read Only = no
List = False
Hosts allow = IP of primary Server A
Auth users = rsync
Secrets file =/etc/rsync.pas
Vi/etc/rsync.pas
Rsync:xxxxxx
chmod 0600/etc/rsync.pas
Start RSYNCD
Rsync--daemon
Add Boot auto Start service:
Vi/etc/rc.local
Rsync--daemon
Back to primary Server A,
Vi/etc/rc.local
Add the following, real-time power-on automatic sync:
Copy Code code as follows:
/root/inotify_init.sh
/root/inotify_monitor.sh
Save exit
Run:
Copy Code code as follows:
/root/inotify_init.sh
/root/inotify_monitor.sh
This will enable real-time synchronization of picture files, in the primary server A synchronization directory under a new file test.