-
-
Save crisidev/bd52bdcc7f029be2f295 to your computer and use it in GitHub Desktop.
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 |
Used @chipironcin to create dashboards in folders
KEY="my_key_here"
HOST="https://my_instance.com"
GRAFANA_DASHBOARDS_FOLDER=".grafana/dashboards"
echo "Exporting Grafana dashboards from $HOST"
mkdir -p "${GRAFANA_DASHBOARDS_FOLDER}"
for dash in $(curl -sSL -k -H "Authorization: Bearer ${KEY}" "${HOST}/api/search?query=&" | jq -r '.[] | select(.type == "dash-db") | .uid'); do
curl -sSL -k -H "Authorization: Bearer ${KEY}" "${HOST}/api/dashboards/uid/$dash" | jq -r . >${GRAFANA_DASHBOARDS_FOLDER}/${dash}.json
slug=$(cat ${GRAFANA_DASHBOARDS_FOLDER}/${dash}.json | jq -r '.meta.slug')
folder=$(cat ${GRAFANA_DASHBOARDS_FOLDER}/${dash}.json | jq -r '.meta.folderTitle')
mkdir -p "${GRAFANA_DASHBOARDS_FOLDER}/${folder}"
mv ${GRAFANA_DASHBOARDS_FOLDER}/${dash}.json "${GRAFANA_DASHBOARDS_FOLDER}/${folder}/${slug}.json"
done
I used this to export dashboards and datasources
#!/usr/bin/env bash
# SOURCE VARIABLES
export token=xxxxx#admin token
export grafanaurl=http://xxx.xxx.xxx.xxx:3000/api
rm -rf backups
mkdir -p backups
cd backups
datasources=$(curl -s -H "Authorization: Bearer $token" -X GET $grafanaurl/datasources)
for uid in $(echo $datasources | jq -r '.[] | .uid'); do
uid="${uid/$'\r'/}" # remove the trailing '/r'
curl -s -H "Authorization: Bearer $token" -X GET "$grafanaurl/datasources/uid/$uid" | jq > grafana-datasource-$uid.json
slug=$(cat grafana-datasource-$uid.json | jq -r '.name')
mv grafana-datasource-$uid.json grafana-datasource-$uid-$slug.json # rename with datasource name and id
echo "Datasource $uid exported"
done
dashboards=$(curl -s -H "Authorization: Bearer $token" -X GET $grafanaurl/search?folderIds=0&query=&starred=false)
for uid in $(echo $dashboards | jq -r '.[] | .uid'); do
uid="${uid/$'\r'/}" # remove the trailing '/r'
curl -s -H "Authorization: Bearer $token" -X GET "$grafanaurl/dashboards/uid/$uid" | jq > grafana-dashboard-$uid.json
slug=$(cat grafana-dashboard-$uid.json | jq -r '.meta.slug')
mv grafana-dashboard-$uid.json grafana-dashboard-$uid-$slug.json # rename with dashboard name and id
echo "Dashboard $uid exported"
done
# https://www.ifconfig.it/hugo/2021/12/backup-grafana-dashboards-with-api-and-jq/
# https://gist.github.com/crisidev/bd52bdcc7f029be2f295
And i used this to re-import all dashboards and datasources on my second server.
Obviously, datasources are exported without passwords, and although the script below will re-import them, you have to manually re-set the datasource passwords.
#!/usr/bin/env bash
# DESTINATION VARIABLES
export token=xxxxxx #admin token
export grafanaurl=http://xxx.xxx.xxx.xxx:3000/api
cd backups
for FILE in *dashboard*; do
echo Importing Dashboard: $FILE
cat $FILE | jq '. * {overwrite: true, dashboard: {id: null}}' | curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $token" "$grafanaurl/dashboards/db" -d @- ;
done
for FILE in *datasource*; do
echo Importing Dashboard: $FILE
cat $FILE | curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $token" "$grafanaurl/datasources/" -d @- ;
done
# https://www.ifconfig.it/hugo/2021/12/backup-grafana-dashboards-with-api-and-jq/
# https://gist.github.com/crisidev/bd52bdcc7f029be2f295
We also needed to export Grafana dashboards, tried the code posted by @KMurphs (thanks) but it only exported some but not all of the dashboards, so changed the code a bit.
#!/usr/bin/env bash
#SOURCE VARIABLES
export token=xxxxx#admin token
export grafanaurl=http://xxx.xxx.xxx.xxx:3000/api
#rm -rf backup
BACKUPDIR=backup_$(date +%Y%m%d_%H%M)
mkdir -p $BACKUPDIR
cd $BACKUPDIR
datasources=$(curl -s -H "Authorization: Bearer $token" -X GET $grafanaurl/datasources)
for uid in $(echo $datasources | jq -r '.[] | .uid'); do
uid="${uid/$'\r'/}" # remove the trailing '/r'
curl -s -H "Authorization: Bearer $token" -X GET "$grafanaurl/datasources/uid/$uid" | jq > grafana-datasource-$uid.json
slug=$(cat grafana-datasource-$uid.json | jq -r '.name')
mv grafana-datasource-$uid.json grafana-datasource-$uid-$slug.json # rename with datasource name and id
echo "Datasource $uid exported"
done
#dashboards=$(curl -s -H "Authorization: Bearer $token" -X GET $grafanaurl/search?folderIds=0&query=&starred=false)
dashboards=$(curl -s -H "Authorization: Bearer $token" -X GET $grafanaurl/search)
for uid in $(echo $dashboards | jq -r '.[] | .uid'); do
uid="${uid/$'\r'/}" # remove the trailing '/r'
curl -s -H "Authorization: Bearer $token" -X GET "$grafanaurl/dashboards/uid/$uid" | jq > grafana-dashboard-$uid.json
slug=$(cat grafana-dashboard-$uid.json | jq -r '.meta.slug')
mv grafana-dashboard-$uid.json grafana-dashboard-$uid-$slug.json # rename with dashboard name and id
echo "Dashboard $uid exported"
done
cd ..
# https://www.ifconfig.it/hugo/2021/12/backup-grafana-dashboards-with-api-and-jq/
# https://gist.github.com/crisidev/bd52bdcc7f029be2f295```
Yet another iteration with folders structure, and token auth
usage ./export.sh https://grafana.local MY_API_TOKEN
#!/bin/bash
set -o errexit
set -o pipefail
set -x
FULLURL="$1"
headers="Authorization: Bearer $2"
in_path=dashboards_raw
set -o nounset
echo "Exporting Grafana dashboards from $FULLURL"
mkdir -p $in_path
for dash in $(curl -H "$headers" -s "$FULLURL/api/search?query=&" | jq -r '.[] | select(.type == "dash-db") | .uid'); do
dash_path="$in_path/$dash.json"
curl -H "$headers" -s "$FULLURL/api/dashboards/uid/$dash" | jq -r . > $dash_path
slug=$(cat $dash_path | jq -r '.meta.slug')
folder="$(cat $dash_path | jq -r '.meta.folderTitle')"
mkdir -p "$folder"
mv -f $dash_path $folder/${dash}-${slug}.json
done
@senz
Dashboard folders can have spaces, so replace second to last line with
mv -f $dash_path "$folder/${dash}-${slug}.json"
Quick Question @senz
How do you import with the folder structure?
Hi all. In order to make the exported dashboards "importable" I had to remove the "dashboard" object that was including the dashboard itself. The json included a meta and a dashboard objects. Please change the 2nd jq to the example below to make it work:
FROM: jq -r . > dashboards/${dash}.json
TO: jq -r {meta:.meta}+.dashboard > dashboards/${dash}.json
I hope it helps!!!
This did help my situation on Grafana 8.3.3 to 8.5.2
@merlian2 did you got the solution to import dashboards in folder structure ??
Also needed to export a bunch of Dashboards adjusted the code of @senz a bit to get the proper dashboard names as the filename and to get rid of the dashboards_raw folder.
#!/bin/bash
set -o errexit
set -o pipefail
# set -x
FULLURL="$1"
headers="Authorization: Bearer $2"
in_path=dashboards_raw
set -o nounset
echo "Exporting Grafana dashboards from $FULLURL"
mkdir -p $in_path
for dash in $(curl -H "$headers" -s "$FULLURL/api/search?query=&" | jq -r '.[] | select(.type == "dash-db") | .uid'); do
curl -H "$headers" -s "$FULLURL/api/search?query=&" 1>/dev/null
dash_path="$in_path/$dash.json"
curl -H "$headers" -s "$FULLURL/api/dashboards/uid/$dash" | jq -r . > $dash_path
jq -r .dashboard $dash_path > $in_path/dashboard.json
title=$(jq -r .dashboard.title $dash_path)
folder="$(jq -r '.meta.folderTitle' $dash_path)"
mkdir -p "$folder"
mv -f $in_path/dashboard.json "$folder/${title}.json"
echo "exported $folder/${title}.json"
done
rm -r $in_path
@merlian2 @mintuguptha To import with folder structure, I make use of dashboard provisioning. See Grafana Docs
mkdir -p "$folder"
@GreenieT87 I had sub folders, so I made the following diff to your script.
Feel free to edit your post if you wish.
21c21,26
< mkdir -p "$folder"
---
> if [[ $title =~ / ]]
> then
> mkdir -p "$folder/${title%/*}"
> else
> mkdir -p "$folder"
> fi
Hi all, I want to know is it possible to export all the dashboards by only using the organizational account with username & password with Editor permissions ?
Is it even possible because I have only seen examples using either admin accounts or API Token.
Anybody have an idea what can be done in this case if there is no API Key available?
@tedraymond I have found the exact solution which I was looking for here: https://github.com/Beam-Connectivity/grafana-dashboard-manager
. It works without api token and even an account with Viewer/Editor role.
Avoid moving files
#!/bin/bash
set -o errexit
set -o pipefail
set -x
FULLURL="$1"
headers="Authorization: Bearer $2"
in_path="$3"
set -o nounset
echo "Exporting Grafana dashboards from $FULLURL"
mkdir -p $in_path
cd $in_path
for dash in $(curl -H "$headers" -s "$FULLURL/api/search?query=&" | jq -r '.[] | select(.type == "dash-db") | .uid'); do
dash_data=$(curl -H "$headers" "$FULLURL/api/dashboards/uid/$dash" )
slug=$(printf '%s' $dash_data | jq -r '.meta.slug')
folder="$(printf '%s' $dash_data | jq -r '.meta.folderTitle')"
mkdir -p "$folder"
printf '%s' $dash_data | jq -r . > $folder/${dash}-${slug}.json
done
Thanks autokilla47. Solution works still in v9.1.7.
PS. I've used API Keys, not sure if it works with new Grafana Service Accounts
I have created my API keys and a few of my systems keep giving me 'invalid authorization code'
It's the same script that runs on all the other systems without problems.
Even when I just run it manually and export the KEY it fails with this error
export KEY='eyJrIjoiSnp6N0N2WGJwZFJoYTB0OVpORW5uaGFaamwzWHo5WjQiLCJuIjoiYmFja3VwcyIsImlkIjoxfQ=='
aamlxgmp003 mckenns /home/mckenns $ echo $KEY
eyJrIjoiSnp6N0N2WGJwZFJoYTB0OVpORW5uaGFaamwzWHo5WjQiLCJuIjoiYmFja3VwcyIsImlkIjoxfQ==
aamlxgmp003 mckenns /home/mckenns $ echo $HOST
http://localhost:3000
aamlxgmp003 mckenns /home/mckenns $ curl -sS -H "Authorization: Bearer ${KEY}" $HOST/api/search?query=& | jq -r '.[] '
invalid API key
00000000000000000000000000000000
#Use bash binary to run script: bash grafana-dashboard-exporter.sh
#Generate unique API key for individual organization with admin rights with no expiry date.
#Change organization in grafana to generate API key
#For exporting dashboards and data sources only
#Grafana 8.5
#!/usr/bin/env bash
ORGS=(
"1ORGName:KEY"
"2ndORGName:KEY"
"3rdORGName:KEY")
HOST="http://0.0.0.0:3000"
#query start date
export YDATE=`date +%Y`
export MDATE=`date +%b`
FILE_DIR=/Backup/$YDATE/$MDATE
mkdir -p $FILE_DIR
fetch_fields() {
echo $(curl -sSL -f -k -H "Authorization: Bearer ${1}" "${HOST}/api/${2}" | jq -r "if type==\"array\" then .[] else . end | .${3}")
}
if [ ! -d "$FILE_DIR" ] ; then
mkdir -p "$FILE_DIR"
fi
for row in "${ORGS[@]}" ; do
ORG=${row%%:*}
KEY=${row#*:}
DIR="$FILE_DIR/$ORG"
if [ ! -d "$DIR" ] ; then
mkdir -p "$DIR"
fi
if [ ! -d "$DIR/dashboards" ] ; then
mkdir -p "$DIR/dashboards"
fi
if [ ! -d "$DIR/datasources" ] ; then
mkdir -p "$DIR/datasources"
fi
for dash in $(fetch_fields $KEY 'search?type=dash-db&query=&' 'uid'); do
uid=$(echo ${dash})
DB=$(echo $(fetch_fields $KEY "dashboards/uid/${uid}") | jq -r "if type==\"array\" then .[] else . end | .meta.slug").json
echo $DB
echo $uid
curl -f -k -H "Authorization: Bearer ${KEY}" "${HOST}/api/dashboards/uid/${uid}" > "$DIR/dashboards/$DB"
done
for id in $(fetch_fields $KEY 'datasources' 'id'); do
DS=$(echo $(fetch_fields $KEY "datasources/${id}" 'name')|sed 's/ /-/g').json
echo $DS
curl -f -k -H "Authorization: Bearer ${KEY}" "${HOST}/api/datasources/${id}" | jq '.id = null' | jq '.orgId = null' > "$DIR/datasources/$DS"
done
done
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
[..]
Also needed to export a bunch of Dashboards adjusted the code of @senz a bit to get the proper dashboard names as the filename and to get rid of the dashboards_raw folder.
#!/bin/bash set -o errexit set -o pipefail # set -x FULLURL="$1" headers="Authorization: Bearer $2" in_path=dashboards_raw set -o nounset echo "Exporting Grafana dashboards from $FULLURL" mkdir -p $in_path for dash in $(curl -H "$headers" -s "$FULLURL/api/search?query=&" | jq -r '.[] | select(.type == "dash-db") | .uid'); do curl -H "$headers" -s "$FULLURL/api/search?query=&" 1>/dev/null dash_path="$in_path/$dash.json" curl -H "$headers" -s "$FULLURL/api/dashboards/uid/$dash" | jq -r . > $dash_path jq -r .dashboard $dash_path > $in_path/dashboard.json title=$(jq -r .dashboard.title $dash_path) folder="$(jq -r '.meta.folderTitle' $dash_path)" mkdir -p "$folder" mv -f $in_path/dashboard.json "$folder/${title}.json" echo "exported $folder/${title}.json" done rm -r $in_path@merlian2 @mintuguptha To import with folder structure, I make use of dashboard provisioning. See Grafana Docs
thanks, this works well except if dashboard has a "/" in the name (or another character that would need escaping as a filename).
thanks, this works well except if dashboard has a "/" in the name (or another character that would need escaping as a filename).
Thanks, that works fine. Here is a tiny update based on that, using sed to replace "/" with "-".
#!/bin/bash
set -o errexit
set -o pipefail
# set -x
# e.g. http://localhost:8888 (no trailing slash)
FULLURL="$1"
# API key
# e.g. eyJr0000000000000000000000000000000000000000000000000000000000000000000000000000
headers="Authorization: Bearer $2"
in_path=dashboards_raw
set -o nounset
echo "Exporting Grafana dashboards from $FULLURL"
mkdir -p $in_path
curl -H "$headers" -s "$FULLURL/api/search?query=&" > tmp.json
for dash in $(curl -H "$headers" -s "$FULLURL/api/search?query=&" | jq -r '.[] | select(.type == "dash-db") | .uid'); do
curl -H "$headers" -s "$FULLURL/api/search?query=&" 1>/dev/null
dash_path="$in_path/$dash.json"
curl -H "$headers" -s "$FULLURL/api/dashboards/uid/$dash" | jq -r . > $dash_path
jq -r .dashboard $dash_path > $in_path/dashboard.json
title=$(jq -r .dashboard.title $dash_path | sed "s/\//-/g")
folder="$(jq -r '.meta.folderTitle' $dash_path | sed "s/\//-/g")"
mkdir -p "$folder"
mv -f $in_path/dashboard.json "$folder/${title}.json"
echo "exported $folder/${title}.json"
done
rm -r $in_path
Hello!
I'm trying to create automatization of export dashboards, folders, datasources from all org.
My problem:
- Trying to export dashboard from folder "Test" via api
- Delete dashboard via UI
- Trying to import dasboard via api
- Open UI Grafana, dashboard was recreated at "General" folder.
What i try to do: - Login to Grafana, create folder "Test" and in this folder dashboard "Test_dashboard".
- Login to Orgs, get list of dashboards via api
curl --silent -u $user:$pass "${grafanaurl}/api/search?query=&" | jq -r '.[] | select(.type == "dash-db") | .uid'
- Download dashboard via curl
curl --silent -u $user:$pass "${grafanaurl}/api/dashboards/uid/$dash" | jq -r . >${dash}.json
- Delete dashboard from UI Grafana.
- Import this dashboard to Grafana via curl
cat $FILE | jq '. * {overwrite: true, dashboard: {id: null}}' | curl -X POST -H "Content-Type: application/json" -u $user:$pass "$grafanaurl/api/dashboards/db" -d @- ;
- 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.
- 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
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?
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'
hey, I want help in exporting data used by grafana for a dashboard. Does someone know anything related to how to do it?
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?
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
.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": ""
}
This is awesome. The only addition I had was adding or removing the -k flag to curl depending if the Grafana instance was http or https. Thansk for sharing.