• HOME
  • DOCS
  • WTF
  • TECH
  • LIFE
  • PAGES
    • ARCHIVE
    • TAGS
    • ABOUT
    • FRIENDS
    • RSS
  • TOOLS
    • GEO
    • RANDOM()
    • GOO.GL
    • CSS HEART
Aj's Blog

记录时间溜走的瞬间和折腾过的那些事

双机热备:file篇
2016-05-18 @ TECH excludeinotifyinotifywaitlnmprsync

双机热备:mysql篇

一、必要条件
apt-get install rsync
apt-get install inotify-tools

设置rsync自启动:
vi /etc/default/rsync
RSYNC_ENABLE=true

二、rsync 配置
2太服务器配置一样即可,注意IP不同,我们定一个需要同步的模块名: wwwroot 配置如下
注意:rsyncd.password 需要禁止其他用户访问,比如chmod 600
rsync安装成功后 可以自己先单文件测试一下,这里就不赘述了

uid=root
gid=root
use chroot=no
max connections=10
timeout=300
log file=/var/log/rsyncd.log

[wwwroot]
path=/home/wwwroot/
auth users=*** RSYNC_USERNAME *** 
secrets file=/etc/rsyncd.password
read only=no
list=yes
uid=www
gid=www
hosts allow=*** SERVER2_IP ***
hosts deny=0.0.0.0/32

三、实例:黑名单方案(除指定文件外,其他文件变动都需要同步)
首先创建一个目录,用户存放我们的脚本 假设为:rsync_cron,本文中以 lnmp 安装后的默认情况为例,同步除 /home/wwwroot/default 外所有网站文件到另一台服务器

#文件: exclude.rsync
default/
*.sw*

#文件: exclude.inotify
/home/wwwroot/
@/home/wwwroot/default/

#文件: password
只放密码就是前面rsync配置 rsyncd.password 中的密码

inotifywait监控文件实现,符合条件就用rsync主动推送

#!/bin/bash
#--------------------------------------------------
#   1号服务器脚本
#--------------------------------------------------
DIR="/root/rsync_cron"         #上面提到的目录
LOG="/var/log/rsync_push.log"  #主动推送文件的日志
cd $DIR

#rsync
RSYNC="$(which rsync)"
RSYNC_SERVER="SERVER_2_IP"           #目标服务器 IP
RSYNC_USER="SERVER_2_RSYNC_USERNAME" #目标服务器 用户名
RSYNC_PASS="$DIR/password"           #目标服务器 密码
RSYNC_MODULE_SITE="wwwroot"          #目标服务器 同步模块,配合下面的路径 可以自己定义多个模块同步,在下面的脚本中做检查属于那个模块
RSYNC_SOURCE_SITE="/home/wwwroot/"   #本地存储路径
RSYNC_EXCLUDE="$DIR/exclude.rsync"   #文件排除规则

#inotify
INOTIFY="$(which inotifywait)"
INOTIFY_EXCLUDE="$DIR/exclude.inotify" #文件排除规则

#inotify process
CORE() {
    $INOTIFY \
        --quiet \
        --monitor \
        --recursive \
        --event create,close_write,delete,move \
        --exclude "\.(swp|swx|swo|\w+~)$" \
        --timefmt "%Y-%m-%d %H:%M:%S" \
        --format "%T %:e %w %f" \
        --fromfile $INOTIFY_EXCLUDE | while read file
    do
        if [[ "$file" =~ [0-9]+$ ]]; then
            echo "[-] $file"
        else
            echo "[+] $file"
            RSYNC_MODULE=$RSYNC_MODULE_SITE
            RSYNC_SOURCE=$RSYNC_SOURCE_SITE
            $RSYNC \
                --quiet \
                --recursive \
                --archive \
                --update \
                --delete-after \
                --compress \
                --owner \
                --group \
                --perms \
                --times \
                --bwlimit=100 \
                --exclude-from=$RSYNC_EXCLUDE \
                --password-file=$RSYNC_PASS \
                $RSYNC_SOURCE \
                $RSYNC_USER@$RSYNC_SERVER::$RSYNC_MODULE
        fi
    done
}
#log to file
CORE >> $LOG 2>&1 &

该脚本我已经上线使用,因为对文件时效性要求不高主要是作备份用途,所以有些问题无关紧要,但是仍需说明一下
1) delete/move事件会导致2台服务器间几次同步后才能成功,文件多了、事件多了会产生更多流量
2) 如果不兼容delete和move又会导致大量文件漏掉

下一篇:   debian squeeze upgrade to wheezy online
上一篇:   双机热备:mysql篇
暂无评论

Cancel reply