Skip to content

Instantly share code, notes, and snippets.

@ilude
Created September 10, 2021 16:52
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 ilude/644c3d9324b1f5059eba9e3983c3c79a to your computer and use it in GitHub Desktop.
Save ilude/644c3d9324b1f5059eba9e3983c3c79a to your computer and use it in GitHub Desktop.
Export Grafana Dashboards using the API
#!/bin/bash
set -o errexit
set -o pipefail
# import .env variables
set -o allexport
source .env
set +o allexport
if [ -z "${GRAFANA_HOST}" ]; then
echo "GRAFANA_HOST is unset or set to the empty string. Please add GRAFANA_HOST=<hostname> to your .env file"
fi
if [ -z "${GRAFANA_API_KEY}" ]; then
echo "GRAFANA_API_KEY is unset or set to the empty string. Please add GRAFANA_API_KEY=<api key> to your .env file"
fi
API_URL="https://api_key:$GRAFANA_API_KEY@$GRAFANA_HOST"
set -o nounset
echo "Exporting Grafana dashboards from $GRAFANA_HOST"
rm -rf dashboards
mkdir -p dashboards
for dash in $(curl -s "$API_URL/api/search?query=&" | jq -r '.[] | select(.type == "dash-db") | .uid'); do
echo "curl -s \"$API_URL/api/dashboards/uid/$dash\" | jq ."
dashboard_json=$(curl -s "$API_URL/api/dashboards/uid/$dash" | jq .)
slug=$(echo "$dashboard_json" | jq -r '.meta.slug')
dashboard_dir=$(echo "$dashboard_json" | jq -r '.meta.folderTitle')
mkdir -p dashboards/$dashboard_dir
echo "$dashboard_json" | jq '.dashboard.id = null' | jq '.dashboard' > dashboards/$dashboard_dir/${slug}-${dash}.json
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment