Skip to content

Instantly share code, notes, and snippets.

@crisidev
Created October 7, 2015 20:35
Show Gist options
  • Save crisidev/bd52bdcc7f029be2f295 to your computer and use it in GitHub Desktop.
Save crisidev/bd52bdcc7f029be2f295 to your computer and use it in GitHub Desktop.
Command to export all grafana 2 dashboard to JSON using curl
KEY=XXXXXXXXXXXX
HOST="https://metrics.crisidev.org"
mkdir -p dashboards && for dash in $(curl -k -H "Authorization: Bearer $KEY" $HOST/api/search\?query\=\& |tr ']' '\n' |cut -d "," -f 5 |grep slug |cut -d\" -f 4); do
curl -k -H "Authorization: Bearer $KEY" $HOST/api/dashboards/db/$dash > dashboards/$dash.json
done
@meSATYA
Copy link

meSATYA commented Feb 9, 2025

@merlian2 did you got the solution to import dashboards in folder structure ??

Though below script is not for import, but it exports smoothly with nested folder structure.

On Grafana 11, subfolder feature was introduced. I am able to create the folder structure and copy all the dashboards into their folders in a nested way. Here is the script.

#!/bin/bash

##### PRE-REQUISITES #####
# 1. Grafana URL & API Key for the Org
# 2. CLI Tools: curl & jq

### NOTE: DO NOT RUN ANY CURL COMMAND with POST/UPDATE USING THIS SCRIPT ###


### Update GRAFANA_URL or API_KEY or ROOT_FOLDER below as per your need ###
GRAFANA_URL="https://play.grafana.org"
API_KEY="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"    # Pass the SA token for diff. Org if folders & dashboards to be downloaded for the same
ROOT_FOLDER="./dashboards/org6"      # update this variable if changing the Org ID


### Create BackUp of the existing dashboards ROOT folder ###
if [ -d "$ROOT_FOLDER" ]; then
    mv "$ROOT_FOLDER" "$ROOT_FOLDER-bkp_$(date +%Y%m%d_%H%M)"
fi


### Fetch the list of all folders present in the Org ###
all_folders_list=($(curl -ks -H "Authorization: Bearer $API_KEY" $GRAFANA_URL/api/search\?query\=\& | jq -r '.[] | select((.type == "dash-folder")) .uid'))


### Create nested folders as in the Org Dashboards Page and save dashboard.json with timestamp to them"
for folder in ${all_folders_list[@]};
do
    folder_title=$(curl -ks -H "Authorization: Bearer $API_KEY" $GRAFANA_URL/api/folders/$folder | jq -r '.title')
    echo "folder_title:: $folder_title"

    parent_folders=$(curl -ks -H "Authorization: Bearer $API_KEY" $GRAFANA_URL/api/folders/$folder | jq -r 'select(.parents != null) .parents[].title' | tr '\n' '/')
    mkdir -p "$ROOT_FOLDER/$parent_folders/$folder_title"

    for dash in $(curl -ks -H "Authorization: Bearer $API_KEY" $GRAFANA_URL/api/search\?query\=\& | jq -r --arg folder "$folder" '.[] | select((.folderUid==$folder) and .type == "dash-db") .uid'); do
        curl -s -H "Authorization: Bearer $API_KEY" "$GRAFANA_URL/api/dashboards/uid/$dash" | jq -r . > "$ROOT_FOLDER/$parent_folders/$folder_title/${dash}.json"
        slug=$(cat "$ROOT_FOLDER/$parent_folders/$folder_title/${dash}.json" | jq -r '.meta.slug')
        mv "$ROOT_FOLDER/$parent_folders/$folder_title/${dash}.json" "$ROOT_FOLDER/$parent_folders/$folder_title/${slug}_$(date +%Y%m%d_%H%M).json"
        echo "Dashboard "${slug}" is saved at location "$ROOT_FOLDER/$parent_folders/$folder_title""
    done

done

### Only download jsons for the dashboards present at ROOT folder level ###
for dash in $(curl -ks -H "Authorization: Bearer $API_KEY" $GRAFANA_URL/api/search\?query\=\& | jq -r '.[] | select((.folderUid==null) and .type == "dash-db") .uid'); do
    curl -s -H "Authorization: Bearer $API_KEY" "$GRAFANA_URL/api/dashboards/uid/$dash" | jq -r . > "$ROOT_FOLDER/${dash}.json"
    slug=$(cat "$ROOT_FOLDER/${dash}.json" | jq -r '.meta.slug')
    mv "$ROOT_FOLDER/${dash}.json" "$ROOT_FOLDER/${slug}_$(date +%Y%m%d_%H%M).json"
    echo "Dashboard "${slug}" is saved at location "$ROOT_FOLDER/""
done

@aalzoladatadope
Copy link

aalzoladatadope commented Jul 22, 2025

Hi all,

I'm looking for a way to export the actual panel data from Grafana — not just the dashboard or panel JSON definitions.
Basically, I want to automate what we can manually do via:

Right-click panel → Inspect → Data → Download CSV

Ideally, I’d like to do this recursively for all panels in a dashboard using a script (e.g. via API).
Has anyone done something similar or found a reliable method to achieve this?

Thanks in advance!

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