正文
工作中要保证生产环境部署的consul的集群能够安全稳定地对外提供服务,即使出现系统故障也能快速恢复,这里将讲述部分的备份还原操作及KV的导入导出操作。
备份与还原 配置文件、服务器状态
需要备份的主要有两类数据:consul相关的配置文件、consul的服务器状态,采用下面的脚本备份就可以了:
ts=$(date +%Y%m%d%H%M%S)
# 备份配置文件
tar -czpf consul_config_$ts.tar.gz /etc/consul/config.json /etc/consul/consul.d
# 备份consul的服务器状态,注意由于该consul开启了ACL,执行consul snapshot save时必须带Management Token,关于consul ACL token的说明见上一篇"consul安全加固"
consul snapshot save --http-addr=http://10.12.142.216:8500 -token=b3a9bca3-6e8e-9678-ea35-ccb8fb272d42 consul_state_$ts.snap
# 查看一下生成的consul服务器状态文件
consul snapshot inspect consul_state_$ts.snap
最后将生成的consul_config_xxx.tar.gz
、consul_state_xxx.snap
拷贝到其它服务器妥善存储。
还原也比较简单,采用下面的脚本就可以了:
# 还原配置文件
tar -xzpf consul_config_20180521145032.tar.gz -C /
# 还原consul服务器状态
consul snapshot restore --http-addr=http://10.12.142.216:8500 -token=b3a9bca3-6e8e-9678-ea35-ccb8fb272d42 consul_state_20180521145032.snap
KV存储的导入导出
consul直接提供命令对KV里存储的数据进行导入导出,如下:
$ ts=$(date +%Y%m%d%H%M%S)
# 导出所有kv键值对,注意最后一个参数是导出键值对的前缀,为空字符串说明要导出所有
$ consul kv export --http-addr=http://10.12.142.216:8500 -token=b3a9bca3-6e8e-9678-ea35-ccb8fb272d42 '' > consul_kv_$ts.json
# 查看下导出的json文件格式
$ cat consul_kv_$ts.json
[
{
"key": "xxxxxx",
"flags": 0,
"value": "yyyyyy"
},
{
"key": "xxxxxx2",
"flags": 0,
"value": "eyJ2ZXJzaW9uX3RpbWVzdGFtcCI6IC0xfQ=="
},
]
发现是每个键值对都是json数值中一项,其中key为键值对Key的名称,value为键值对Value的base64编码,使用base64 -d
命令编码就可以看到原始的value值,如:
$ echo 'eyJ2ZXJzaW9uX3RpbWVzdGFtcCI6IC0xfQ==' | base64 -d
{"version_timestamp": -1}
导入就更简单了:
consul kv import --http-addr=http://10.12.142.216:8500 -token=b3a9bca3-6e8e-9678-ea35-ccb8fb272d42 @consul_kv_20180521150322.json
OVER..................................................................................................完结撒花
如常备份恢复更新脚本
consul_backup.sh
[root@bastion back_consul]# cat consul_backup.sh
#!/bin/bash
# Author:laujian
ts=$(date +"%Y_%m_%d_%H_%M")
# 通过运行脚本添加参数 加consul 地址 token传进来
#consul_token='8dc1eb67-1f5f-4e10-ad9d-5e58b047647c'
#http_addr='consul.hrfax.local'
http_addr='192.168.12.121:32000'
consul_token='8dc1eb67-1f5f-4e10-ad9d-5e58b047647c'
processName="consul_backup.sh"
processNum=`ps -ef | grep $processName | grep -v grep | wc -l`
#if [ $processName gt "3" ];then
#echo " $processName already existed!"
#exit 1
#fi
#status_dir=`pwd`/status_dir
kv_dir=`pwd`/kv_dir
#[ -d $status_dir ] || mkdir $status_dir
[ -d $kv_dir ] || mkdir $kv_dir
status_backup() {
`pwd`/consul snapshot save -token=${consul_token} -http-addr=${http_addr} $status_dir/consul_state_${ts}.snap
}
kv_backup() {
`pwd`/consul kv export -token=${consul_token} -http-addr=${http_addr} > $kv_dir/consul_kv_${ts}_${http_addr}.json
}
kv_restore() {
date=$1
tar xPf $kv_dir/consul_kv_${date}.tar.gz -C /
ls $kv_dir/consul_kv_${date}.json > /dev/null
if [ $? == 0 ];then
`pwd`/consul kv import -token=${consul_token} -http-addr=${http_addr} @$kv_dir/consul_kv_${date}.json
fi
}
kv_backup
update_consul.sh
#!/bin/bash
cd /data/deployment_mediums;git pull
sleep 5
consul_address='192.168.12.121:32000'
consul_token='8dc1eb67-1f5f-4e10-ad9d-5e58b047647c'
cd /data/deployment_mediums/$1
for yaml in `ls consul/*`;do
consul_key=`cat $yaml |head -1 |sed 's/#//g' |sed 's/ //g'`
consul_data=`cat $yaml | grep -v $consul_key`
curl --location --request PUT "http://${consul_address}/v1/kv/${consul_key}?dc=hrfax" --header "X-Consul-Token: ${consul_token}" --header "Content-Type: text/plain" --data "${consul_data}"
done
if [[ $? -eq 0 ]];then
echo "更新consul成功"
fi
[root@bastion option-consul]# cat update_consul.sh
#!/bin/bash
cd /data/deployment_mediums;git pull
sleep 5
consul_address='192.168.12.121:32000'
consul_token='8dc1eb67-1f5f-4e10-ad9d-5e58b047647c'
cd /data/deployment_mediums/$1
for yaml in `ls consul/*`;do
consul_key=`cat $yaml |head -1 |sed 's/#//g' |sed 's/ //g'`
consul_data=`cat $yaml | grep -v $consul_key`
curl --location --request PUT "http://${consul_address}/v1/kv/${consul_key}?dc=hrfax" --header "X-Consul-Token: ${consul_token}" --header "Content-Type: text/plain" --data "${consul_data}"
done
if [[ $? -eq 0 ]];then
echo "更新consul成功"
fi
标签:http,--,consul,token,导入,kv,备份,dir
From: https://blog.51cto.com/u_14035463/7689626