基于zk-shell的zookeeper备份恢复脚本

python3.x

pip3 install zk-shell

备份脚本

#!/bin/bash

# Set the Zookeeper server address
zk_server="127.0.0.1:2181"

# Create the target directory with timestamps
dir="/zkbackup/$(date +%Y%m%d%H%M)"
mkdir -p $dir

# Get the first and second fields of the directory
dir1=$(echo $dir|cut -d'/' -f2)
dir2=$(echo $dir|cut -d'/' -f3)

# Execute the "ls" command on the Zookeeper server
list=$(/usr/local/bin/zk-shell --run-once "ls" $zk_server | grep -v zookeeper)
if [ $? -ne 0 ];then
    echo "Error: failed to execute zk-shell ls command"
    exit 1
fi

# Copy the files to the target directory
for i in $list;do
    /usr/local/bin/zk-shell --run-once "cp zk://$zk_server/$i json://\!$dir1\!\!$dir2\!$i.json/ true true" $zk_server > /dev/null
    if [ $? -ne 0 ];then
        echo "Error: failed to execute zk-shell cp command for $i"
        exit 1
    fi
    echo $i
done

备份脚本执行后会在zkbackup目录下生成例如202212091448的文件夹

恢复脚本(cd到需要恢复的目录下执行)

cd /zkbackup/202212091448 并在当前目录执行脚本

#!/bin/bash

# Set the Zookeeper server address
zk_server="127.0.0.1:2181"

# Extract current directory names
dir1=$(pwd |awk -F '/' '{print $2}')
dir2=$(pwd |awk -F '/' '{print $3}')

# Extract file names
list=$(ls -l | grep -v *.sh | sed '1d' | awk '{print $9}')

# Define a function to run the zk-shell command
function run_zk_shell() {
  local file="$1"
  local dir3=$(echo "$file" | awk -F '.json' '{print $1}')
  /usr/local/bin/zk-shell --run-once  "cp json://\!$dir1\!\!$dir2\!$file/  zk://$zk_server/$dir3 true true" $zk_server > /dev/null
}

# Iterate over the file list and run the zk-shell command
for file in $list; do
  run_zk_shell "$file"
done

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值