rsync+inotify实现数据的实时备份


说明:使用服务器版本CentOS5.5_x64,Web1-2是服务端安装rsync,server是客户端安装 inotify-tools,规划如下:


1. 安装rsync与inotify-tools

[root@web1~]# rpm -q rsync

rsync-2.6.8-3.1

[root@web2~]# rpm -q rsync

rsync-2.6.8-3.1

[root@server~]# tar zxf inotify-tools-3.14.tar.gz

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

[root@serverinotify-tools-3.14]# ./configure ;make ;make install

2. 在2个服务节点配置rsync(在web1/web2的操作)

[root@web1~]# vi /etc/rsyncd.conf

uid = nobody

gid = nobody

use chroot = no

max connections = 10

pid file =/var/run/rsyncd.pid

lock file =/var/run/rsync.lock

log file =/var/log/rsyncd.log

[www]

path = /var/www/html/

comment = web1 file

ignore errors

read only = no

write only = no

hosts allow = 192.168.1.0/24

list = false

uid = root

gid = root

auth users = webuser

secrets file = /etc/rsyncd_users.db

[root@web1~]# echo "123" >> /etc/rsyncd_users.db

[root@web1~]# echo "webuesr:123" >> /etc/rsyncd_users.db

[root@web1~]# chmod 600 /etc/rsyncd_users.db

[root@web1~]# /usr/bin/rsync --daemon

[root@web1~]# netstat -anpt |grep 873

tcp    0      0 0.0.0.0:873        0.0.0.0:*        LISTEN      9182/rsync

[root@web1~]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local

3 配置内容发布节点(在Server上的操作)

    配置内容发布节点的主要工作是将生成的静态网页实时的同步到二个服务节点,这个过程可以通过一个shell脚本来完成

[root@server~]# vi /usr/local/inotify/inotify.sh

#!/bin/bash

host="192.168.1.200192.168.1.201"

src=/web/data/

dst=www

user=webuser

/usr/local/bin/inotifywait-mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -emodify,delete,create,attrib  $src \

| while read files

        do

        for hostip in $host

        do

        /usr/bin/rsync -vzrtopg --delete--progress --password-file=/etc/rsyncd_users.db $src $user@$hostip::$dst&> /dev/null

                echo "${files} wasrsynced" >>/tmp/rsync.log 2>&1

         done

         done

[root@server~]# echo "webuser:123" > /etc/rsyncd_users.db

[root@server~]# chmod 600 /etc/rsyncd_users.db

[root@server~]# /usr/local/inotify/inotify.sh &

[root@server~]# echo "/usr/local/inotify/inotify.sh &" >> /etc/rc.local

4.测试是否同步

[root@server~]# touch /web/data/index.html

[root@web1~]# ls /var/www/html/

index.html

[root@web2~]# ls /var/www/html/

index.html

以上可以看到同步成功了!

 

有时会遇到这样的情况:向inotify监控的目录(这里是/web/data/)写入一个很大文件时,由于写入这个大文件需要一段时间,此时inotify就会持续不停的输出该文件被更新的信息,这样就会持续不停的触发rsync去执行同步操作,占用了大量系统资源,那么针对这种情况,最理想的做法是等待文件写完后再去触发rsync同步。 在这种情况下,可以修改inotify的监控事件,即:“-e close_write,delete,create,attrib”。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值