两台rhel5.6web服务器,上面跑的都是Apache,目录都是/va/www/html,现在想把线上的web数据实时备份到另外一台web服务器,即这个“另外一台web服务器”作为实时备用机器,这里我把线上的web机称为源服务器,作为实时备用的服务器称为目标服务器。  一般线上的各realserver服务器之间也是通过rsync+inotify方式实现实时同步

实验过程:

第一部分:配置目标服务器:

[root@mubiao ~]# service iptables stop

[root@mubiao ~]# setenforce 0

[root@mubiao ~]# yum install httpd

[root@mubiao ~]# service httpd start

[root@mubiao ~]# chkconfig --level 35 httpd on

[root@mubiao ~]# 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,即打开rsync功能

    socket_type     = stream

    wait            = no

    user            = root

    server          = /usr/bin/rsync

    server_args     = --daemon

    log_on_failure  += USERID

    }

[root@mubiao ~]#service xinetd restart   ####rsync服务在xinetd服务里面,此时重启xinetd服务就是启动rsync服务

[root@mubiao ~]# cat /etc/rsyncd.conf    ####创建配置文件

    log file = /var/log/rsyncd.log     ####日志文件位置,启动rsync后自动产生这个文件,不需要提前创建

    pid file = /var/run/rsyncd.pid   ###pid文件的存放位置

    lock file = /var/run/rsyncd.lock    #####支持max connections参数的锁文件

    secrets file = /etc/rsyncd.pass   ###用户认证配置文件,里面要手动新建用户名和密码,接下来会创建这个文件,这里的用户名密码和linux系统中的root等用户名不是一个概念

    motd file = /etc/rsyncd.motd   ###rsync启动时欢迎信息页面文件位置(文件内容自定义)

    

    [web]     #####模块的名字可以随便取,但要有意义

    path = /var/www/html/   ####rsync服务器端数据的目录路径,即源服务器数据的目录

    comment = web apache    ####模块名称的说明简介

    uid = root   ####设置rsync用户运行权限为root

    gid = root    ####设置rsync组运行权限为root

    port = 873   ####rsync的默认端口号

    use chroot = no  ####默认是true,修改为no,增加对目录文件软连接的备份

    read only = no   ###设置rsync的服务端文件为读写权限,写成yes时只能对rsync服务端文件读

    list = no   ###不显示rsync服务端资源列表

    max connections = 200   #####最大连接数

    timeout = 600   ####设置超时时间为600秒

    auth users = yunwei_01  ###执行数据同步的用户名,可以设置多个用户名,之间用逗号隔开,这些用户要现在/etc/rsyncd.pass 这个文件中定义好

    hosts allow = 192.168.65.151  ####允许进行数据同步的客户端的ip地址,可以设置多个ip,之间用逗号隔开,这里的客户端指的是源服务器

    hosts deny = 192.168.65.134   ####不允许数据同步的客户端ip地址,如果有多个,之间用逗号隔开

[root@mubiao ~]# cat /etc/rsyncd.pass    ####创建用户认证文件,这里的用户名密码是在下面的文件里创建的,和系统的用户名密码没有关系

    yunwei_01:123456

[root@mubiao ~]# chmod 600 /etc/rsyncd.conf    ####设置文件权限,只需将文件设成具有读写权限就可以了

[root@mubiao ~]# chmod 600 /etc/rsyncd.pass   ########设置文件权限,只需将文件设成具有读写权限就可以了

[root@mubiao ~]# service xinetd restart   ####重启服务,使更改生效


第二部分:配置源服务器

[root@yuan ~]# service iptables stop

[root@yuan ~]# setenforce 0

[root@yuan ~]# yum install httpd gcc gcc-c++ -y

[root@yuan ~]# service httpd start

[root@yuan ~]# chkconfig --level 35 httpd on

[root@yuan ~]# 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,即打开rsync功能

    socket_type     = stream

    wait            = no

    user            = root

    server          = /usr/bin/rsync

    server_args     = --daemon

    log_on_failure  += USERID

    }

[root@yuan ~]# service xinetd restart

[root@yuan ~]# cat /etc/password.txt    ####创建认证密码文件

123456

[root@yuan ~]# chmod 600 /etc/password.txt  #####设置文件权限,

[root@yuan ~]# rsync -avH --port 873 --progress --delete /var/www/html/ yunwei_01@192.168.65.129::web --password-file=/etc/password.txt  ####在源服务器上进行测试,发现可以实现把原服务器数据备份到目标服务器上面

[root@yuan ~]# tar zxf inotify-tools-3.14.tar.gz   ####安装inotify-tools

[root@yuan ~]# cd inotify-tools-3.14

[root@yuan inotify]# ./configure --prefix=/usr/local/inotify

[root@yuan inotify]# make &&make install

 下面设置环境变量,并添加软链接

[root@yuan ~]# echo "PATH=/usr/local/inotify/bin:$PATH" >> /etc/profile.d/inotify.sh

[root@yuan ~]# source /etc/profile.d/inotify.sh  ###使设置生效

[root@yuan ~]# echo "/usr/local/inotify/lib" > /etc/ld.so.conf.d/inotify.conf

[root@yuan ~]# ln -s /usr/local/inotify/include /usr/include/inotify

对inotify调优

[root@yuan ~]# sysctl -w fs.inotify.max_queued_events="99999999"

[root@yuan ~]# sysctl -w fs.inotify.max_user_watches="99999999"

[root@yuan ~]# sysctl -w fs.inotify.max_user_instances="65535"

  参数说明:  

        max_queued_events:

        inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监         控文件不准确

        max_user_watches:

        要同步的文件包含多少目录,可以用:find /var/www/html/yunwei_01 -type d | wc -l 统         计,必须保证max_user_watches值大于统计结果.

        max_user_instances:

        每个用户创建inotify实例最大值


在源服务器上面创建脚本,并在源服务器上面运行该脚本,实时触发rsync进行同步

[root@yuan ~]# 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 $rsyncuser@$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 $rsyncuser@$ip::$dstdir --password-       file=$rsyncpassdir

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

    done

    done

[root@yuan ~]# chmod +x /usr/local/inotify/rsync.sh   #####添加脚本执行的权限

    脚本参数说明: 

        srcdir=/var/www/html  ###源服务器同步目录

        dstdir=web   ####目标服务器rsync同步目录模块名称

        rsyncuser=yunwei_01  ###目标服务器rsync同步用户名

        rsyncpassdir=/etc/password.txt  ##目标服务器rsync同步用户的密码在源服务器的存放路径

        dstip="192.168.65.129"  ####目标服务器ip,多个ip用空格分开

        /tmp/rsync.log  ####脚本运行日志记录


设置开机脚本自动执行

[root@yuan ~]# cat /etc/rc.d/rc.local 

    #!/bin/sh

    #

    # This script will be executed *after* all the other init scripts.

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

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

    

    touch /var/lock/subsys/local

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

重启源服务器

重启后,会发现,此时源和目标服务器实现了实时同步

到这里配置完成。