Created
December 12, 2018 16:02
-
-
Save alloydwhitlock/11b05cfbf46fccc42083ebe94870e715 to your computer and use it in GitHub Desktop.
Simple Grafana backup script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Name: Grafana dashboard backup script | |
# Version: 0.1 | |
# Requires: jq | |
display_usage() { | |
echo "Usage: $0 HOSTNAME KEY DIR" | |
echo "Backup all Grafana dashboards using Grafana API key" | |
echo "Requires 'jq' to parse JSON" | |
echo "" | |
echo "Example: $0 http://grafana.host.com:3000 reallysuperlongapikey== /var/tmp/grafana" | |
exit 1 | |
} | |
jq_check() { | |
command -v "jq" | |
if [[ $? != 0 ]] | |
then | |
echo "'jq' is not installed. Please install 'jq'." | |
exit 1 | |
fi | |
} | |
main() { | |
if [[ (($# < 2) ) ]] | |
then | |
display_usage | |
fi | |
jq_check | |
HOST="$1" | |
KEY="$2" | |
DASH_DIR="$3" | |
for uid in $(curl -sS -H "Authorization: Bearer $KEY" $HOST/api/search\?query\=\& | jq -r '.[] | select( .type | contains("dash-db")) | .uid'); do | |
DS_JSON="$(curl -sS -H "Authorization: Bearer $KEY" $HOST/api/dashboards/uid/$uid)" | |
DS_TITLE="$(echo ${DS_JSON} | jq -r '.dashboard | .title')" | |
FOLDER_NAME="$(echo ${DS_JSON} | jq -r '.meta | .folderTitle')" | |
mkdir -p "${DASH_DIR}/${FOLDER_NAME}" | |
echo ${DS_JSON} > "${DASH_DIR}/${FOLDER_NAME}/${DS_TITLE}.json" | |
done | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment