Automatic real-time backup between Linux via Rsync+inotify-tools

Source: Internet
Author: User
Tags inotify rsync

Two Rhel5.6web servers, which ran Apache, the directory is/va/www/html, now want to put online web data in real-time backup to another Web server, that is, this "another Web server" as a real-time standby machine, Here I refer to the Web machine on the line as the source server, as a real-time standby server called the target server

Experimental process:

Part I: Configuring the target server:

#service iptables Stop

#setenforce 0

#yum Install httpd

#service httpd Start

#chkconfig--level httpd on

[Email protected] ~]# Cat/etc/xinetd.d/rsync

# Default:off

# description:the Rsync server is a good addition to an FTP server, as it \

#allows CRC checksumming etc.

Service rsync

{

Disable= no # # # #将yes改为no, that is, the rsync function is turned on

Socket_type = Stream

wait = no

user = root

Server =/usr/bin/rsync

Server_args =--daemon

Log_on_failure + = USERID

}

#service xinetd Restart

[Email protected] ~]# cat/etc/rsyncd.conf # # # #创建配置文件

Log file =/var/log/rsyncd.log

PID file =/var/run/rsyncd.pid

Lock file =/var/run/rsyncd.lock

Secrets file =/etc/rsyncd.pass

MOTD file =/ETC/RSYNCD.MOTD


[web]

Path =/var/www/html/

Comment = Web Apache

UID = root

GID = root

Port = 873

Use chroot = no

Read Only = no

List = no

Max connections = 200

Timeout = 600

Auth users = yunwei_01

Hosts allow = 192.168.65.151

Hosts Deny = 192.168.65.134

[[email protected] ~]# Cat/etc/rsyncd.pass # # # #创建用户认证文件, here the user name password is created in the following file, and the system user name password does not matter

yunwei_01:123456

#chmod 600/etc/rsyncd.conf # # # #设置文件权限

#chmod 600/etc/rsyncd.pass

#service xinetd Restart # # # #重启服务 for the changes to take effect


Part II: Configuring the source server

#service iptables Stop

#setenforce 0

#yum Install httpd gcc gcc-c++-y

#service httpd Start

#chkconfig--level httpd on

[Email protected] ~]# Cat/etc/xinetd.d/rsync

# Default:off

# description:the Rsync server is a good addition to an FTP server, as it \

#allows CRC checksumming etc.

Service rsync

{

Disable= no # # # #将yes改为no, that is, the rsync function is turned on

Socket_type = Stream

wait = no

user = root

Server =/usr/bin/rsync

Server_args =--daemon

Log_on_failure + = USERID

}

#service xinetd Restart

[Email protected] ~]# Cat/etc/password.txt # # # #创建认证密码文件

123456

#chmod 600/etc/password.txt # #设置文件权限,

#rsync-AVH--port 873--progress--delete/var/www/html/[email protected]::web--password-file=/etc/password.txt # # #在 Tested on the source server and found it possible to back up the original server data to the target server

#tar zxf inotify-tools-3.14.tar.gz # # # #安装inotify-tools

#cd inotify-tools-3.14

#./configure--prefix=/usr/local/inotify

#make &&make Install

Set environment variables below and add soft links

#echo "Path=/usr/local/inotify/bin: $PATH" >>/etc/profile.d/inotify.sh

#source/etc/profile.d/inotify.sh

# echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf

# ln-s/usr/local/inotify/include/usr/include/inotify

Tuning for INotify

#sysctl-W fs.inotify.max_queued_events= "99999999"

#sysctl-W fs.inotify.max_user_watches= "99999999"

#sysctl-W fs.inotify.max_user_instances= "65535"

Create a script on the source server and run the script on the source server to sync with rsync in real time

[Email protected] ~]# cat/usr/local/inotify/rsync.sh

#!/bin/sh

srcdir=/var/www/html/

Dstdir=web

Rsyncuser=yunwei_01

Rsyncpassdir=/etc/password.txt

dstip= "192.168.65.129"

For IP in $dstip

Do

RSYNC-AVH--port=873--progress--delete $srcdir [email protected] $ip:: $dstdir--password-file= $rsyncpassdir

Done

/usr/local/inotify/bin/inotifywait-mrq--timefmt '%d/%m/%y%h:%m '--format '%T%w%f%e '-e close_write,modify,delete,  Create,attrib,move $srcdir | While read file

Do

For IP in $dstip

Do

RSYNC-AVH--port=873--progress--delete $srcdir [email protected] $ip:: $dstdir--password-file= $rsyncpassdir

echo "${file} was rsynced" >>/tmp/rsync.log 2>&1

Done

Done

#chmod +x/usr/local/inotify/rsync.sh

Set the boot script to execute automatically

[Email protected] ~]# cat/etc/rc.d/rc.local

#!/bin/sh

#

# This script is executed *after* all and the other init scripts.

# can put your own initialization stuff in here if you don ' t

# want to does the full Sys V style init stuff.


Touch/var/lock/subsys/local

Sh/usr/local/inotify/rsync.sh &


Restarting the source server

After rebooting, you will find that the source and destination servers are now synchronized in real time

The configuration is complete here.

This article from "Personal Feelings" blog, declined reprint!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.