Created
July 9, 2022 11:23
-
-
Save KMurphs/48c4064537f88a3b201fcdc614db5921 to your computer and use it in GitHub Desktop.
Script to export Grafana Dashboards and Datasources
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
#!/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 |
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
#!/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