-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathetcd-copy
executable file
·98 lines (82 loc) · 3.01 KB
/
etcd-copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/sh
init() {
echo "This is etcd copy shell, welcome!"
export CURRENT_DIR=$PWD
}
usage() {
echo "usage: etcd copy <options>"
echo "功能说明:"
echo " 复制目录或者文件"
echo "参数说明:"
echo " -o 必选参数: origin url, 指定其源目录或者文件的url 例如: http://127.0.0.1:2379/v2/keys/dev/yangtao ."
echo " -d 必选参数: dest url, 指定其目标目录或者文件的url 例如: http://127.0.0.1:2379/v2/keys/qa/yangtao ."
}
cmd_help() {
usage
exit 0
}
recursion_copy() {
echo 参数个数: $# ...
if [ $# = 2 ]; then
local origin_url=$1
local dest_url=$2
echo 递归复制数据开始 ...
echo 源目录或者文件的url: $origin_url ...
local key=`curl "$origin_url" | jq '.node.key' | sed -e "s/\"//g"`
echo 目录或者文件的相对路径: $key ...
local origin_root_url=`echo $origin_url | sed -e "s%$key%%g"`
echo 源目录或者文件的根路径: $origin_root_url ...
local is_directory=`curl $origin_url | jq '.node.dir'`
echo 检测到的该路径是否是目录: $is_directory ...
if [ "$is_directory" = "true" ]; then
echo 创建目录 ...
curl "$dest_url" -XPUT -d "dir=true"
echo 分组所有etcd key值 ...
local origin_keys=(`curl "$origin_url" | jq '.node.nodes[] | .key' | sed -e "s/\"//g"`)
if [ -z $origin_keys ]; then
echo "origin_keys ports is not found!"
else
for j in ${origin_keys[@]}
do
echo 路径: $origin_url 下的子目录或者文件: $j ...
local additionalKey=`echo $j | sed -e "s%$key%%g"`
echo 附加的关键字为: $additionalKey ...
echo "recursion_copy $origin_root_url$j $dest_url$additionalKey"
recursion_copy "$origin_root_url$j" "$dest_url$additionalKey"
done
fi
else
etcd_key_copy "$origin_url" "$dest_url"
fi
fi
}
etcd_key_copy() {
echo 参数个数: $#
if [ $# = 2 ]; then
origin_url=$1
dest_url=$2
echo 找到原始值 ...
origin_value=`curl $origin_url | jq '.node.value' | sed -e "s/\"//g"`
echo 将原始值设置到目标对象 ...
curl "$dest_url" -XPUT --data-urlencode "value=$origin_value"
fi
}
cmd_default() {
echo 函数cmd_default执行开始 ...
DEFINE_string origin_url '' 'origin url' o
DEFINE_string dest_url '' 'dest url' d
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
echo 验证输入参数 -o ...
if [ "${FLAGS_origin_url}" = "" ]; then
usage
die "错误信息: -o 是必选参数."
fi
echo 验证输入参数 -d ...
if [ "${FLAGS_dest_url}" = "" ]; then
usage
die "错误信息: -d 是必选参数."
fi
echo 开始处理 ...
recursion_copy "${FLAGS_origin_url}" "${FLAGS_dest_url}"
}