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