inotify+rsync实时同步 - 脚本备份

参考

https://www.ruanyifeng.com/blog/2020/08/rsync.html
https://cloud.tencent.com/developer/article/1373541

限速参数

rsync -avH -e 'ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no' --delete --bwlimit=1m --progress  root@172.28.144.143:/root/dir1/ /root/dir1

脚本

#!/bin/bash
if [[ ! -f slave_hosts.cache ]]; then
    touch slave_hosts.cache
fi

cluster_node=$1   # master
src_dir=$2        # src_dir=/root/dir1
master_host=172.28.144.143
slave_hosts=172.30.59.86
slave_hosts_arr=(${slave_hosts//,/ })


# 处理参数
echo "input params: $cluster_node , $src_dir , $target_host"
src_dir=${src_dir%*/}
echo "opt src_dir: $src_dir"

OLD_SLAVE_HOSTS=$(cat slave_hosts.cache)
echo "$slave_hosts" > slave_hosts.cache

# 创建文件夹
mkdir -p "$src_dir"

# 文件监控和同步
PIDS=`ps -ef |grep inotifywait |grep -v grep | awk '{print $2}'`
if [ "$PIDS" != "" ]; then
    echo "$cluster_node inotifywait is runing! "
    if [ "master" == "$cluster_node" ] && [ "$OLD_SLAVE_HOSTS" == "$slave_hosts" ]; then
        exit 0
    fi
    if [ "slave" == "$cluster_node" ] || [ "$OLD_SLAVE_HOSTS" != "$slave_hosts" ]; then
        echo "slave node kill inotifywait!"
        kill -9 "$PIDS"
    fi
fi

if [[ "$OLD_SLAVE_HOSTS" != "$slave_hosts" ]]; then
    echo "slave_hosts change old: [$OLD_SLAVE_HOSTS]  new:[$slave_hosts] "
fi

echo "check rsync pid"
RSYNC_PIDS=`ps -ef |grep rsync |grep -v grep | awk '{print $2}'`
if [ "$RSYNC_PIDS" != "" ]; then
    echo "rsync is runing exit."
    exit 0
fi

#运行进程
if [[ "slave" == "$cluster_node" ]]; then
    echo "slave node rsync pull master:$master_host"
    rsync -avH -e 'ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no' --delete root@"$master_host":"$src_dir"/ "$src_dir"
fi

#运行进程
if [[ "master" == "$cluster_node" ]]; then
    # 全部同步一次
    for target_host in ${slave_hosts_arr[@]}
    do
        echo "master node to rsync $target_host, ignore delete!"
        rsync -avH -e 'ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no' --rsync-path="mkdir -p $src_dir && rsync" "$src_dir"/ root@"$target_host":"$src_dir"
    done

    echo "double check inotifywait pid..."
    PIDS=`ps -ef |grep inotifywait |grep -v grep | awk '{print $2}'`
    if [ "$PIDS" != "" ]; then
        echo "inotifywait is runing! exit script......"
        exit 0
    fi

    echo "master node to run inotifywait!"
    #/usr/bin/inotifywait -mrq --format '%Xe %w%f' -e modify,create,delete,attrib,close_write,move "$src_dir" | while read file 
    /usr/bin/inotifywait -mrq --format '%Xe %w%f' -e modify,create,delete,attrib,move "$src_dir" | while read file 
    do
        echo "$(date "+%Y-%m-%d %H:%M:%S") $file"
        INO_EVENT=$(echo $file | awk '{print $1}')
        INO_FILE=$(echo $file | awk '{print $2}')
        # SYNC_DIR=$(dirname ${INO_FILE})
        # SYNC_DIR=$(echo $INO_FILE | awk -F '/' '{print "/"$2"/"$3"/"$4"/"$5}')
        SYNC_DIR=$( echo $INO_FILE | awk -F '/' '{  if(NF>5){print "/"$2"/"$3"/"$4"/"$5} else if(NF>4){print "/"$2"/"$3"/"$4} else{print "'$src_dir'"}; }' )
        if [[ ! -d "$SYNC_DIR" ]]; then
            # 如果文件夹不存在,找上层
            SYNC_DIR=$(dirname ${SYNC_DIR})
        fi
        # 处理每个host
        echo "SYNC_DIR:$SYNC_DIR"
        for target_host in ${slave_hosts_arr[@]}
        do
            echo "$(date "+%Y-%m-%d %H:%M:%S") $file  ---> $target_host"
            rsync -avH -e 'ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no' --delete --rsync-path="mkdir -p $SYNC_DIR && rsync" "$SYNC_DIR"/ root@"$target_host":"$SYNC_DIR"
        done
    done
fi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值