I. Preface * with the development of the company's business, a single web server cannot meet users' access needs. Multiple web servers need to be added to achieve balanced and high-availability access. In order to unify the data of various web servers at any time, the manual method is definitely not desirable. Considering the need for real-time synchronization, we can use the inotify + rsync solution for synchronization! Configuration environment: ce
I. Preface *
With the development of the company's business, a single web server cannot meet users' access needs. Therefore, multiple web servers need to be added to achieve balanced and high-availability access. In order to unify the data of various web servers at any time, the manual method is definitely not desirable. Considering the need for real-time synchronization, inotify + rsy is used.NcTo synchronize!
Configuration environment: centos 5.3 x86_64
RSyncVersion: rsync-0.3.7
Inotify version: inotify-tooLs-3.14
Server: 192.168.2.79 [Also rsync + inotify server]
Client: 192.168.2.100 [server to be synchronized to ensure data consistency]
First download the required software:
Http://blog.mgcrazy.com/download/rsync-3.0.7.tar.gz
Http://blog.mgcrazy.com/download/inotify-tools-3.14.tar.gz
Ii. formal installation:
Log on to the server 192.168.2.79 and download the file to:/usr/src:
Cd/Usr/src & wget http://blog.mgcrazy.com/download/{inotify-tools-3.14.tar.gz,rsync-3.0.7.tar.gz} & tar xzf rsync-3.0.7.tar.gz & cd rsync-3.0.7 &./configure? Prefix =/usr/local/rsync & make install & cd .. /& tar xzf inotify-tools-3.14.tar.gz & cd inotify-tools-3.14 &&. /configure & make install
Since then, rsync and inotify-tools have been installed! [Copy and execute directly]
3. Configure ssh authentication:
[Purpose: To avoid password input during real-time synchronization]
Run this command on the serverCommandGenerate public key: ssh-keygen one-way carriage return: as shown in Figure
Then, remotely copy the public key to the client server and rename it as authoriz.Ed_ Keys:
SCp-R/root/. ssh/Id_ Rsa. pub root@192.168.2.100:/root/. ssh/authorized_keys!
4. configure a Real-Time Synchronization script:
#!/bin/shsrc=/home/webapps/wwwdes=/home/webapps/ip=192.168.2.100 inotifywait -mrq ?timefmt ‘%d/%m/%y-%H:%M’ ?format ‘%T %w%f’ -e modify,delete,create,attrib ${src} | while read filedofor i in $ipdo/usr/local/rsync/bin/rsync -aP ?delete $src root@$ip:$desdonedone
Save as rsync. sh and grant the execution permission:ChmodO + x rsync. sh the path to be synchronized and Client ip address can be modified as needed: 192.168.2.100 is the Client ip address.
V. Test results:
The rest is to keep the script in the background: You can use: screen to execute the script in the background; then. /rsync. sh; crtl + a + d Exit, modify and create a file on the server, which is equivalent to an action that triggers inotify. If there is no action, it is not synchronized by default, then the system will automatically execute rsync to synchronize to the client. You can view the data on the client!