Skip to content

Instantly share code, notes, and snippets.

@aeciopires
Created March 21, 2023 17:00
Show Gist options
  • Save aeciopires/e1adfd808097ee9260a419263ccae099 to your computer and use it in GitHub Desktop.
Save aeciopires/e1adfd808097ee9260a419263ccae099 to your computer and use it in GitHub Desktop.
Export datasources and dashboards of Grafana.
#!/bin/bash
#------------------------
# Authors: Aecio dos Santos Pires <http://aeciopires.com>
# Date: 21 mar 2023
#
# Objective: Export datasources and dashboards of Grafana.
# Tested with Grafana 6.6.x
#
# References:
# https://gist.github.com/crisidev/bd52bdcc7f029be2f295
# https://gist.github.com/crisidev/bd52bdcc7f029be2f295?permalink_comment_id=4197302#gistcomment-4197302
#---------------------------
# Variables
#---------------------------
PROGPATHNAME=$0
PROGFILENAME=$(basename "$PROGPATHNAME")
PROGDIRNAME=$(dirname "$PROGPATHNAME")
BACKUPDIR=backup_grafana_dashboards_$(date +%Y%m%d_%H%M)
BACKUPDIR_DATASOURCES="${PROGDIRNAME}/${BACKUPDIR}/datasources"
BACKUPDIR_DASHBOARDS="${PROGDIRNAME}/${BACKUPDIR}/dashboards"
DEBUG=false
# Grafana
export API_TOKEN=CHANGE_HERE # Grafana admin API token
export GRAFANA_URL="https://CHANGE_HERE/api" # or "http://CHANGE_HERE:3000/api"
#----------------------------
#---------------------------
# Main
#---------------------------
mkdir -p "$BACKUPDIR_DATASOURCES"
mkdir -p "$BACKUPDIR_DASHBOARDS"
# Exporting datasources...
if [ "$DEBUG" == true ]; then
echo -E "
[DEBUG] Command to get datasource list...
curl -s -H \"Authorization: Bearer $API_TOKEN\" ${GRAFANA_URL}/datasources\"
"
fi
datasources=$(curl -s -H "Authorization: Bearer $API_TOKEN" ${GRAFANA_URL}/datasources)
for uid in $(echo "$datasources" | jq -r '.[] | .id'); do
uid="${uid/$'\r'/}" # remove the trailing '/r'
if [ "$DEBUG" == true ]; then
echo -E "
[DEBUG] Command to export datasource...
curl -s -H \"Authorization: Bearer $API_TOKEN\" -X GET \"${GRAFANA_URL}/datasources/${uid}\"
"
fi
curl -s -H "Authorization: Bearer $API_TOKEN" -X GET "${GRAFANA_URL}/datasources/${uid}" | jq > "${BACKUPDIR_DATASOURCES}/grafana-datasource-${uid}.json"
slug=$(cat "${BACKUPDIR_DATASOURCES}/grafana-datasource-${uid}.json" | jq -r '.name' | tr -s ' ' | tr ' ' '_')
mv "${BACKUPDIR_DATASOURCES}/grafana-datasource-${uid}.json" "${BACKUPDIR_DATASOURCES}/grafana-datasource-${slug}-${uid}.json" # rename with datasource name and id
echo "[INFO] Datasource ${slug}-${uid} exported"
echo "[INFO] Encrypting the file ${BACKUPDIR_DATASOURCES}/grafana-datasource-${slug}-${uid}.json..."
done
# Exporting dashboards...
if [ "$DEBUG" == true ]; then
echo -E "
[DEBUG] Command to get dashboard list...
curl -s -H \"Authorization: Bearer $API_TOKEN\" ${GRAFANA_URL}/search\"
"
fi
dashboards=$(curl -s -H "Authorization: Bearer $API_TOKEN" ${GRAFANA_URL}/search)
for uid in $(echo "$dashboards" | jq -r '.[] | .uid'); do
uid="${uid/$'\r'/}" # remove the trailing '/r'
if [ "$DEBUG" == true ]; then
echo -E "
[DEBUG] Command to export dashboard...
curl -s -H \"Authorization: Bearer $API_TOKEN\" -X GET \"${GRAFANA_URL}/dashboards/uid/${uid}\"
"
fi
curl -s -H "Authorization: Bearer $API_TOKEN" -X GET "${GRAFANA_URL}/dashboards/uid/${uid}" | jq > "${BACKUPDIR_DASHBOARDS}/grafana-dashboard-${uid}.json"
slug=$(cat "${BACKUPDIR_DASHBOARDS}/grafana-dashboard-${uid}.json" | jq -r '.meta.slug' | tr -s ' ' | tr ' ' '_')
mv "${BACKUPDIR_DASHBOARDS}/grafana-dashboard-${uid}.json" "${BACKUPDIR_DASHBOARDS}/grafana-dashboard-${slug}-${uid}.json" # rename with dashboard name and id
echo "[INFO] Dashboard ${slug}-${uid} exported"
done
@8763mb
Copy link

8763mb commented Nov 20, 2023

I successfully executed the script, but how do I import it?

@kisermichael
Copy link

I have the same issue. I was able to export everything fine. Is there a way to take the dashboard output and import it back into Grafana? I tried the import feature but that is not importing it correctly. Any ideas?

@kenytux
Copy link

kenytux commented Oct 11, 2024

I have exported successfully. The dashboards seem to be ok. But when I import the dashboards, they are empty!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment