Skip to content

Instantly share code, notes, and snippets.

@KMurphs
Created July 9, 2022 11:23
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 KMurphs/48c4064537f88a3b201fcdc614db5921 to your computer and use it in GitHub Desktop.
Save KMurphs/48c4064537f88a3b201fcdc614db5921 to your computer and use it in GitHub Desktop.
Script to export Grafana Dashboards and Datasources
#!/usr/bin/env bash
# SOURCE VARIABLES
export token=xxxxx#admin token
export grafanaurl=http://xxx.xxx.xxx.xxx:3000/api
rm -rf backups
mkdir -p backups
cd backups
datasources=$(curl -s -H "Authorization: Bearer $token" -X GET $grafanaurl/datasources)
for uid in $(echo $datasources | jq -r '.[] | .uid'); do
uid="${uid/$'\r'/}" # remove the trailing '/r'
curl -s -H "Authorization: Bearer $token" -X GET "$grafanaurl/datasources/uid/$uid" | jq > grafana-datasource-$uid.json
slug=$(cat grafana-datasource-$uid.json | jq -r '.name')
mv grafana-datasource-$uid.json grafana-datasource-$uid-$slug.json # rename with datasource name and id
echo "Datasource $uid exported"
done
dashboards=$(curl -s -H "Authorization: Bearer $token" -X GET $grafanaurl/search?folderIds=0&query=&starred=false)
for uid in $(echo $dashboards | jq -r '.[] | .uid'); do
uid="${uid/$'\r'/}" # remove the trailing '/r'
curl -s -H "Authorization: Bearer $token" -X GET "$grafanaurl/dashboards/uid/$uid" | jq > grafana-dashboard-$uid.json
slug=$(cat grafana-dashboard-$uid.json | jq -r '.meta.slug')
mv grafana-dashboard-$uid.json grafana-dashboard-$uid-$slug.json # rename with dashboard name and id
echo "Dashboard $uid exported"
done
# https://www.ifconfig.it/hugo/2021/12/backup-grafana-dashboards-with-api-and-jq/
# https://gist.github.com/crisidev/bd52bdcc7f029be2f295
#!/usr/bin/env bash
# DESTINATION VARIABLES
export token=xxxxxx #admin token
export grafanaurl=http://xxx.xxx.xxx.xxx:3000/api
cd backups
for FILE in *dashboard*; do
echo Importing Dashboard: $FILE
cat $FILE | jq '. * {overwrite: true, dashboard: {id: null}}' | curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $token" "$grafanaurl/dashboards/db" -d @- ;
done
for FILE in *datasource*; do
echo Importing Dashboard: $FILE
cat $FILE | curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $token" "$grafanaurl/datasources/" -d @- ;
done
# https://www.ifconfig.it/hugo/2021/12/backup-grafana-dashboards-with-api-and-jq/
# https://gist.github.com/crisidev/bd52bdcc7f029be2f295
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment