2

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.

Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73
MetalHead
  • 181
  • 1
  • 15

2 Answers2

5

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
Dharman
  • 30,962
  • 25
  • 85
  • 135
MetalHead
  • 181
  • 1
  • 15
1

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
Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73