consul backup restore import and export

text


During work, we must ensure that the consul cluster deployed in the production environment can provide external services safely and stably, and can quickly recover even if a system failure occurs. Here we will describe some backup and restore operations and KV import and export operations.

 

 

Backup and restore configuration files, server status


There are two main types of data that need to be backed up: consul-related configuration files and consul server status. Just use the following script to back up:

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.gzFinally , the generated data will consul_state_xxx.snapbe copied to other servers for proper storage.

Restoration is also relatively simple, just use the following script:

# 还原配置文件
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

 

Import and export of KV storage


Consul directly provides commands to import and export the data stored in KV, as follows:

$ 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=="
},
]

It is found that each key-value pair is one of the json values, where key is the name of the key-value pair, and value is the base64 encoding of the key-value pair Value. You can base64 -dsee the original value using command encoding, such as:

$ echo 'eyJ2ZXJzaW9uX3RpbWVzdGFtcCI6IC0xfQ==' | base64 -d
{"version_timestamp": -1}

 Importing is even simpler:

consul kv import --http-addr=http://10.12.142.216:8500 -token=b3a9bca3-6e8e-9678-ea35-ccb8fb272d42 @consul_kv_20180521150322.json

OVER........................................................ .................................................end scatter flowers

Guess you like

Origin blog.csdn.net/qq_34556414/article/details/132804947
Recommended