Skip to content

Instantly share code, notes, and snippets.

@vigindian
Last active May 3, 2023 23:20
Show Gist options
  • Save vigindian/b100a316bb596ce1f33019d51c6f299e to your computer and use it in GitHub Desktop.
Save vigindian/b100a316bb596ce1f33019d51c6f299e to your computer and use it in GitHub Desktop.
Grafana dashboards export script
#!/bin/bash
#####################################################
# Script to export Grafana dashboards
#
# Vignesh Narasimhulu
#
#####################################################
#get today's date
today=$(date +%Y%m%d)
#Grafana server details
GHOST="grafana-demo"
GPORT="3000"
#Grafana api key
GKEY="your-grafana-api-key"
EXPORT_DIR="./dashboards"
mkdir ${EXPORT_DIR} 2>/dev/null
HOST="http://${!GHOST}:${!GPORT}" #grafana url
#get dashboard names and store it locally as json files
for dash in $(curl -k -H "Authorization: Bearer ${!GKEY}" $HOST/api/search\?query\=\& |tr ',' '\n' |grep uri|cut -d\" -f 4|tr '\n' ' ')
do
echo "exporting dashboard ${dash}..."
#export in json format
curl -k -H "Authorization: Bearer ${!GKEY}" $HOST/api/dashboards/$dash > ${EXPORT_DIR}/$dash.json
done
#compressing the file
tar czvf grafana_dashboards_${today}.tgz ${EXPORT_DIR}
echo "The grafana dashboards has been exported to grafana_dashboards_${today}.tgz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment