linux之间通过rsync+inotify-tools实现自动实时备份

两台rhel5.6web服务器,上面跑的都是Apache,目录都是/va/www/html,现在想把线上的web数据实时备份到另外一台web服务器,即这个“另外一台web服务器”作为实时备用机器,这里我把线上的web机称为源服务器,作为实时备用的服务器称为目标服务器

实验过程:

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

#service iptables stop

#setenforce 0

#yum install httpd

#service httpd start

#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

}

#service xinetd restart

[root@mubiao ~]# 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

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

    yunwei_01:123456

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

#chmod 600 /etc/rsyncd.pass

#service xinetd restart   ####重启服务,使更改生效


第二部分:配置源服务器

#service iptables stop

#setenforce 0

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

#service httpd start

#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

}

#service xinetd restart

[root@yuan ~]# 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  ####在源服务器上进行测试,发现可以实现把原服务器数据备份到目标服务器上面

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

#cd inotify-tools-3.14

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

#make &&make install

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

#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

对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"

在源服务器上面创建脚本,并在源服务器上面运行该脚本,实时出发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

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

设置开机脚本自动执行

[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 &


重启源服务器

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

到这里配置完成。

本文出自 “个人感受” 博客,谢绝转载!

linux之间通过rsync+inotify-tools实现自动实时备份,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。