-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvault-export-import.sh
executable file
·68 lines (54 loc) · 1.98 KB
/
vault-export-import.sh
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
#!/usr/bin/env bash
set -e -u -o pipefail
usage() {
echo "Usage: ./export-import.sh -s c-still-bush-3704 -d c-amazeeio-test1 -t XXXXXX -a XXXXXX"
echo "Options:"
echo " -s <SOURCE CLUSTER ID> #required, cluster id of the source VAULT"
echo " -d <DESTINATION CLUSTER ID> #required, cluster id of the destination VAULT"
echo " -t <SOURCE VAULT TOKEN> #required, token of the source SYN"
echo " -a <DESTINATION VAULT TOKEN> #required, token of the destination SYN"
exit 1
}
if [[ ! $@ =~ ^\-.+ ]]
then
usage
fi
while getopts ":s:d:t:a:" opt; do
case ${opt} in
s ) # process option s
SOURCE_CLUSTER_ID=$OPTARG;;
d ) # process option d
DESTINATION_CLUSTER_ID=$OPTARG;;
t ) # process option t
SOURCE_VAULT_TOKEN=$OPTARG;;
a ) # process option a
DESTINATION_VAULT_TOKEN=$OPTARG;;
h )
usage;;
*)
usage;;
esac
done
mkdir -p $SOURCE_CLUSTER_ID
SOURCE_TENANT_ID=t-ja3px4
SOURCE_VAULT_ADDR=https://vault-prod.syn.vshn.net
VAULT_FORMAT=json
vault login -address=$SOURCE_VAULT_ADDR $SOURCE_VAULT_TOKEN > /dev/null
vault_base="clusters/kv/${SOURCE_TENANT_ID}/${SOURCE_CLUSTER_ID}"
vault kv list -format=json -address=$SOURCE_VAULT_ADDR "$vault_base" | \
jq -r '.[]' | \
grep -v -e '^steward$' | \
while read -r key; do
echo "exporting $SOURCE_CLUSTER_ID/$key"
vault kv get -format=json -address=$SOURCE_VAULT_ADDR "${vault_base}/${key}" | \
jq -r '.data.data' > "$SOURCE_CLUSTER_ID/${key}.json"
done
DESTINATION_TENANT_ID=t-amazeeio
DESTINATION_VAULT_ADDR=https://vault.syn.amazeeio.cloud > /dev/null
vault login -address=$DESTINATION_VAULT_ADDR $DESTINATION_VAULT_TOKEN
vault_base="clusters/kv/${DESTINATION_TENANT_ID}/${DESTINATION_CLUSTER_ID}"
find $SOURCE_CLUSTER_ID -maxdepth 1 -type f -name "*.json" -exec basename -s .json "{}" \; | \
while read -r key; do
echo "importing ${DESTINATION_CLUSTER_ID}/$key"
vault kv put -address=$DESTINATION_VAULT_ADDR "${vault_base}/${key}" "@$SOURCE_CLUSTER_ID/${key}.json"
done