MetalHead
MetalHead

Reputation: 221

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.

Upvotes: 4

Views: 6075

Answers (3)

KUL
KUL

Reputation: 491

For google - PowerShell Grafana saves/exports all dashboards

A note to myself.

Sometimes you need to search for source data in reports, but you don't want to write a script every time.

$_KEY="xxx"
$_HOST="https://xxx"
$headers = @{Authorization="Bearer $_KEY"}
$responseData = (Invoke-WebRequest -Uri $_HOST/api/search -Method Get -Headers $headers -UseBasicParsing -ContentType "application/json; charset=UTF-8").Content | ConvertFrom-Json
for ($i = 0; $i -lt $responseData.Length; $i++)
{
    if ($responseData[$i].type -ne "dash-db") { continue }
    ((Invoke-WebRequest -Uri "$_HOST/api/dashboards/uid/$($responseData[$i].uid)" -Method Get -Headers $headers -UseBasicParsing -ContentType "application/json; charset=UTF-8").Content | ConvertFrom-Json).dashboard | ConvertTo-Json -depth 100 | Out-File -Encoding utf8 -FilePath "$($responseData[$i].uid).json"
}

Upvotes: 1

Ivan Aracki
Ivan Aracki

Reputation: 5361

The working solution in Grafana v9.1.7


#!/bin/bash

HOST='http://<YOUR_GRAFANA_URL>:3000'
DASH_DIR=./

# Declare a list with the api keys using as a prefix the organization name plus "_" character
declare -a StringArray=("<YOUR_ORG_NAME>_<API_KEY>")

# Iterate through api keys:
for API_KEY in "${StringArray[@]}"; do
    ORG=$(echo $API_KEY | cut -d "_" -f1) # Name of the organization based on the prefix
    KEY=$(echo $API_KEY | cut -d "_" -f2) # API Key for that organization after removing the prefix

    # 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')"

        # You can export the files like this to keep them organized by organization:
        mkdir -p "$DASH_DIR/$ORG/$folder_title/dashboards_$ORG"
        echo $dashboard_json | jq -r {meta:.meta}+.dashboard  > $DASH_DIR/$ORG/$folder_title/dashboards_$ORG/${dashboard_title}_v${dashboard_version}.json
    done
done

Upvotes: 1

MetalHead
MetalHead

Reputation: 221

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

Upvotes: 6

Related Questions