递归算法导出:
#!/bin/bash
ZK_SERVER="127.0.0.1:2181"
ZK_CLI_PATH="/opt/module/zookeeper-3.5.7-bin/bin/zkCli.sh"
OUTPUT_FILE="/opt/module/zookeeper-3.5.7-bin/bin/zk_data_export.txt"
TMP_SCRIPT="/opt/module/zookeeper-3.5.7-bin/bin/zk_command.tmp"
: > $OUTPUT_FILE
function zkCommand() {
# echo "$1" | $ZK_CLI_PATH -server $ZK_SERVER 2>&1
echo "addauth digest cache:svcctlg" > "$TMP_SCRIPT"
echo "$1" >> "$TMP_SCRIPT"
$ZK_CLI_PATH -server $ZK_SERVER < "$TMP_SCRIPT" 2>&1
}
# 对 ZooKeeper 进行认证
# zkCommand "addauth digest cache:svcctlg"
function exportZK() {
local path=$1
{
echo "节点: $path"
local data=$(zkCommand "get $path" | tail -2 | head -1)
echo "$data"
echo ""
} >> $OUTPUT_FILE
local children_output=$(zkCommand "ls $path")
local children=$(echo "$children_output" | grep -o '\[.*\]' | tail -2 | head -1 | tr -d '[],' | tr ',' '\n')
echo $children
for child in $children; do
local child_path="$path/$child"
if [ "$path" == "/" ]; then
child_path="/$child"
fi
exportZK "$child_path"
done
}
exportZK "/a1"
echo "ZooKeeper 数据导出完成,文件保存于 $OUTPUT_FILE"
评论