Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export Grafana dashboard via API

I have a question about Grafana API.

I need to export the JSON models of all my dashboards that were made with the GUI in order to import them in another Grafana instance. I tried with the dashboard API - api/dashboards/ using curl with the dashboard uuid or uri (db/), but for some reason I always get the message not found

The uids and uris I found with

$URL/api/search?query=&

Then I tried to get the models or any data

curl -k -H “Authorization: Bearer $KEY” $URL/api/dashboards/db/$dash_name

or

curl -k -H “Authorization: Bearer $KEY” $URL/api/dashboards/uid/$uid

the result is the same.

Does anyone know why is that? I couldn’t find any info anywhere else.

Thanks in advance.

like image 611
MetalHead Avatar asked Sep 19 '25 22:09

MetalHead


1 Answers

The solution is taken from: https://gist.github.com/crisidev/bd52bdcc7f029be2f295#gistcomment-3975489

#!/bin/bash

HOST='http://localhost:3000'
KEY="<add-valid-key>"
DIR="grafana_dashboards"

# Iterate through dashboards using the current API Key
for dashboard_uid in $(curl -sS -H "Authorization: Bearer $KEY" $HOST/api/search\?query\=\& | jq -r '.[] | select( .type | contains("dash-db")) | .uid'); do
    url=$(echo $HOST/api/dashboards/uid/$dashboard_uid | tr -d '\r')
    dashboard_json=$(curl -sS -H "Authorization: Bearer $KEY" $url)
    dashboard_title=$(echo $dashboard_json | jq -r '.dashboard | .title' | sed -r 's/[ \/]+/_/g')
    dashboard_version=$(echo $dashboard_json | jq -r '.dashboard | .version')
    folder_title="$(echo $dashboard_json | jq -r '.meta | .folderTitle')"

    echo "Creating: ${DIR}/${folder_title}/${dashboard_title}_v${dashboard_version}.json"
    mkdir -p "${DIR}/${folder_title}"
    echo ${dashboard_json} | jq -r {meta:.meta}+.dashboard > "${DIR}/${folder_title}/${dashboard_title}_v${dashboard_version}.json"
done
like image 95
MetalHead Avatar answered Sep 23 '25 12:09

MetalHead