Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Grafana backup script
#!/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