Skip to content

Instantly share code, notes, and snippets.

@alkalinecoffee
Last active April 2, 2018 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alkalinecoffee/c6b76457fcd47ddffad21d8a2b87ac2a to your computer and use it in GitHub Desktop.
Save alkalinecoffee/c6b76457fcd47ddffad21d8a2b87ac2a to your computer and use it in GitHub Desktop.
Bash script to recursively copy consul KVs from one directory to another
#/bin/bash
# Recursively copies keys from KEYS_TO_COPY and places them under TO_DIR directory, ie
# /alertmanager => /services/alertmanager
CONSUL_HOST=http://consul.example.com:443
KEYS_TO_COPY=$(cat <<-END
alertmanager
consul
grafana
prometheus
END
)
TO_DIR="/services"
while read -r line; do
curl $CONSUL_HOST/v1/kv/$line?recurse 2>/dev/null | jq -r '.[] | [.Key, .Value] | join(" ")' |
while read line; do
KEY=$TO_DIR/$(echo $line | awk '{print $1}')
VAL=$(echo $line | awk '{print $2}' | base64 --decode)
echo Writing: $KEY
curl -XPUT -d "$VAL" "$CONSUL_HOST/v1/kv/$KEY"
echo
done;
done <<< "$KEYS_TO_COPY"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment