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
@sfranzis
Copy link

sfranzis commented Jul 22, 2023

  • Open UI Grafana, dashboard was recreated at "General" folder. (Expected to appear in the "Test" folder)

Can anyone help with this problem? The documentation says that the JSON file should have "folderId or the folderUid property". Link: https://grafana.com/docs/grafana/latest/developers/http_api/dashboard/#create--update-dashboard In JSON file i have all tags, because grafana make it itself. Tryin to reduce nubmer of it , move from meta, but it doesnt work.

You have to put the folderUid into the root object.
See my solution here: https://medium.com/@stefan4all/grafana-export-import-of-dashboards-39cc502faa2c

@gutocarvalho
Copy link

Hello guys!

I created this script https://gist.github.com/aeciopires/e1adfd808097ee9260a419263ccae099 based in work of @KMurphs in this comment: https://gist.github.com/crisidev/bd52bdcc7f029be2f295?permalink_comment_id=4197302#gistcomment-4197302

The script can export datasources and dashboards of Grafana, but I only tested with Grafana 6.6.x

Example of execution script:

chmod +x exportGrafanaDashboadsAndDatasources.sh
./exportGrafanaDashboadsAndDatasources.sh

[INFO] Datasource AWS_Cloudwatch_PRD-125 exported
[INFO] Encrypting the file ./backup_grafana_dashboards_20230321_1347/datasources/grafana-datasource-AWS_Cloudwatch_PRD-125.json...
[..]
[INFO] Dashboard alerts-prometheus-kGqyea3Zz exported
[..]

do you have anything to export grafana alerts?

@gutocarvalho
Copy link

A simple script to export grafana rules/alerts

#!/bin/bash

GRAFANA_DOMAIN=$1
GRAFANA_TOKEN=$2

curl -X GET -H "Authorization: Bearer ${GRAFANA_TOKEN}" \
	     "https://${GRAFANA_DOMAIN}/api/ruler/grafana/api/v1/rules" | jq > './alerts.json'

@isidhux
Copy link

isidhux commented Aug 25, 2023

hey, I want help in exporting data used by grafana for a dashboard. Does someone know anything related to how to do it?

@8763mb
Copy link

8763mb commented Nov 20, 2023

Hello guys!

I created this script https://gist.github.com/aeciopires/e1adfd808097ee9260a419263ccae099 based in work of @KMurphs in this comment: https://gist.github.com/crisidev/bd52bdcc7f029be2f295?permalink_comment_id=4197302#gistcomment-4197302

The script can export datasources and dashboards of Grafana, but I only tested with Grafana 6.6.x

Example of execution script:

chmod +x exportGrafanaDashboadsAndDatasources.sh
./exportGrafanaDashboadsAndDatasources.sh

[INFO] Datasource AWS_Cloudwatch_PRD-125 exported
[INFO] Encrypting the file ./backup_grafana_dashboards_20230321_1347/datasources/grafana-datasource-AWS_Cloudwatch_PRD-125.json...
[..]
[INFO] Dashboard alerts-prometheus-kGqyea3Zz exported
[..]

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

@isaqueprofeta
Copy link

Just trowing here some recent contribution from "Gonzalo Di Biase" on Grafana Slack

export KEY=XXXXXXXXXXXXX
export HOST=https://mygrafana.mydomain

for dash in $(curl -s -k -H "Authorization: Bearer ${KEY}" ${HOST}/api/search\?query\=\& | jq -r '.[] | select(.type == "dash-db") | .uid'); do
  curl -s -k -H "Authorization: Bearer ${KEY}" ${HOST}/api/dashboards/uid/$dash \
    | jq '. |= (.folderUid=.meta.folderUid) | del(.meta) |del(.dashboard.id) + {overwrite: true}' \
    > "${SCRIPT_DIR}/export/${NAME}/dashboards/${dash}.json"
  echo "Dashboard: ${NAME} - ${dash} saved."
done

@saibug
Copy link

saibug commented Mar 15, 2024

.meta.folderTitle

it doesn't work wuth grafana v7.3.1 API :(

It's seems like folderUid doesn't exist in meta block ..

    "meta": {
        "type": "db",
        "canSave": true,
        "canEdit": true,
        "canAdmin": true,
        "canStar": true,
        "slug": "tcp-status",
        "url": "/d/YCaAbWJIk/tcp-status",
        "expires": "0001-01-01T00:00:00Z",
        "created": "2024-03-15T11:24:21+01:00",
        "updated": "2024-03-15T11:57:33+01:00",
        "updatedBy": "admin",
        "createdBy": "admin",
        "version": 3,
        "hasAcl": false,
        "isFolder": false,
        "folderId": 27,
        "folderTitle": "system",
        "folderUrl": "/dashboards/f/Oe6cfZ1Iz/system",
        "provisioned": false,
        "provisionedExternalId": ""
    }

@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

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