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 |
This comment has been minimized.
This comment has been minimized.
thanks for this script,saved me a bunch of time another thing is , i found it useful to save the JSONs in human readable format by piping them through |python -m json.tool |
This comment has been minimized.
This comment has been minimized.
Here is an upgraded version for the latest version of grafana : KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXX
HOST="https://my.super.duper.grafana.com"
if [ !-d dashboards ] ; then
mkdir -p dashboards
fi
for dash in $(curl -sSL -k -H "Authorization: Bearer $KEY" $HOST/api/search\?query\=\& | jq '.' |grep -i uri|awk -F '"uri": "' '{ print $2 }'|awk -F '"' '{print $1 }'); do
curl -sSL -k -H "Authorization: Bearer ${KEY}" "${HOST}/api/dashboards/${dash}" > dashboards/$(echo ${dash}|sed 's,db/,,g').json
done |
This comment has been minimized.
This comment has been minimized.
I hope you don't mind that I've used your scripts for my repo: https://github.com/hagen1778/grafana-import-export |
This comment has been minimized.
This comment has been minimized.
Thanks all for the precious scripts, the export was working but then the import script is always rejected the dashboards for me in Grafana 3.1.1. Here is the code in the export script so the import script can run smoothly.
|
This comment has been minimized.
This comment has been minimized.
I found that the previous script did not capture the first dashboard. This version fixes the problem:
|
This comment has been minimized.
This comment has been minimized.
Can anyone please let me what's KEY and HOST here in the script ? |
This comment has been minimized.
This comment has been minimized.
@shashanks93 |
This comment has been minimized.
This comment has been minimized.
Was able to achieve this by this script
|
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
@SebastianUA, can you please share the restore script as well which can be used to restore dashboards backup taken by your script. |
This comment has been minimized.
This comment has been minimized.
how did you create a new dashboard with exported json file? curl -k -H "Content-Type: application/json" -H "Authorization: Bearer $KEY" -X POST -d dashboards/$DASHBOARD.json $HOST:$PORT/api/dashboards/db [{"classification":"DeserializationError","message":"invalid character 'd' looking for beginning of value"},{"fieldNames":["Dashboard"],"classification":"RequiredError","message":"Required"}] |
This comment has been minimized.
This comment has been minimized.
Please add the following to the end of json file.
|
This comment has been minimized.
This comment has been minimized.
Thank you for creating.
|
This comment has been minimized.
This comment has been minimized.
Updating on @SebastianUA's solution to work with grafana 5.0 (access dashboards by uid)
|
This comment has been minimized.
This comment has been minimized.
I played with a couple of these scripts and I did get dashboards. But I noticed that all my templated dashboards do not have their variables. Which is most of our dashboards. Has anyone had experience pulling a dashboard with template variables? |
This comment has been minimized.
This comment has been minimized.
I've lost track of this gist. It is awesome to see such engagement and improvements! |
This comment has been minimized.
This comment has been minimized.
For Grafana 5.0.0 I had to remove the
|
This comment has been minimized.
This comment has been minimized.
Made a few updates:
|
This comment has been minimized.
This comment has been minimized.
Made a a couple tweaks:
|
This comment has been minimized.
This comment has been minimized.
Hi, seems need to add special key to disable ssl verification which add --insecure key to curl. |
This comment has been minimized.
This comment has been minimized.
#/bin/bash
#
# add the "-x" option to the shebang line if you want a more verbose output
#
# set some colors for status OK, FAIL and titles
SETCOLOR_SUCCESS="echo -en \\033[0;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"
SETCOLOR_TITLE_PURPLE="echo -en \\033[0;35m" # purple
# usage log "string to log" "color option"
function log_success() {
if [ $# -lt 1 ]; then
${SETCOLOR_FAILURE}
echo "Not enough arguments for log function! Expecting 1 argument got $#"
exit 1
fi
timestamp=$(date "+%Y-%m-%d %H:%M:%S %Z")
${SETCOLOR_SUCCESS}
printf "[${timestamp}] $1\n"
${SETCOLOR_NORMAL}
}
function log_failure() {
if [ $# -lt 1 ]; then
${SETCOLOR_FAILURE}
echo "Not enough arguments for log function! Expecting 1 argument got $#"
exit 1
fi
timestamp=$(date "+%Y-%m-%d %H:%M:%S %Z")
${SETCOLOR_FAILURE}
printf "[${timestamp}] $1\n"
${SETCOLOR_NORMAL}
}
function log_title() {
if [ $# -lt 1 ]; then
${SETCOLOR_FAILURE}
log_failure "Not enough arguments for log function! Expecting 1 argument got $#"
exit 1
fi
${SETCOLOR_TITLE_PURPLE}
printf "|-------------------------------------------------------------------------|\n"
printf "|$1|\n";
printf "|-------------------------------------------------------------------------|\n"
${SETCOLOR_NORMAL}
}
function init() {
# Check if hostname and key are provided
if [ $1 -lt 2 ]; then
${SETCOLOR_FAILURE}
log_failure "Not enough command line arguments! Expecting two: \$HOSTNAME and \$KEY. Recieved only $1."
exit 1
fi
DASH_DIR=$(echo $HOST | awk -F[/:] '{print $4}')
if [ ! -d "${DASH_DIR}" ]; then
mkdir "${DASH_DIR}"
else
log_title "----------------- A $DASH_DIR directory already exists! -----------------"
fi
}
HOST=$1
KEY=$2
init $# $HOST $KEY
counter=0
for dashboard_uid in $(curl -sS -H "Authorization: Bearer $KEY" $HOST/api/search\?query\=\& | jq -r '.[] | select( .type | contains("dash-db")) | .uid'); do
counter=$((counter + 1))
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')
mkdir -p "$DASH_DIR/$folder_title"
echo $dashboard_json > "$DASH_DIR/$folder_title/${dashboard_title}_v${dashboard_version}.json"
log_success "Dashboard has been saved\t\t title=\"${dashboard_title}\", uid=\"${dashboard_uid}\", path=\"${DASH_DIR}/$folder_title/${dashboard_title}_v${dashboard_version}.json\"."
done
log_title "${counter} dashboards were saved";
log_title "------------------------------ FINISHED ---------------------------------";``` |
This comment has been minimized.
This comment has been minimized.
Another tool to export dashboards, now with PHP -> https://github.com/wnasich/grafana_dd |
This comment has been minimized.
This comment has been minimized.
It's possible that I was doing something wrong, but just in case, I'm adding to this discussion to save others the hassle that I've just been through. Whilst this explains a way of exporting the JSON from the dashboards, the format it comes out in will not directly import back in. After playing around, I found that the simply piping out to:
Created a JSON file that was one level too high. When I attempted to import the resulting file back into Grafana, it created the dashboard completely empty. Instead, if we do:
Then you get a JSON file in the format that Grafana wants. I have tested this by creating several dashboards, using the script to export them (with the above fix), deleting them and then re-importing them. All now works fine. |
This comment has been minimized.
This comment has been minimized.
If running on a Mac, the antiquated BSD version of sed included with OSX does not support extended regex. Therefore, you first need to install
becomes
|
This comment has been minimized.
This comment has been minimized.
Also, if you're looking for a batch import dashboard script to go along with this (perhaps you're migrating Grafana servers like I am), I've written a gist based on this one to batch import JSON dashboard files. Grafana Import Dashboard Gist: https://gist.github.com/thedoc31/628beeee934f9c84648c108d4ad89f05 |
This comment has been minimized.
This comment has been minimized.
Wow!!! I was not expecting this kind of engagement from this ;) |
This comment has been minimized.
This comment has been minimized.
Just want to give my two cents on this. I have multiple orgs in Grafana (hence multiple api keys) so I did the following modifications to the scripts above to backup dashboards for each org.
|
This comment has been minimized.
This comment has been minimized.
Simple modification of the original script that works with Grafana 6.2.5 (and needs
|
This comment has been minimized.
This comment has been minimized.
Improved version which stores the dashboards as pretty JSON (easier to review diffs in git) and renames the files to #!/bin/bash
set -o errexit
set -o pipefail
HOST=https://your.host.name
[ -n "$KEY" ] || ( echo "No API key found. Get one from $HOST/org/apikeys and run 'KEY=<API key> $0'"; exit 1)
set -o nounset
echo "Exporting Grafana dashboards from $HOST"
rm -rf dashboards
mkdir -p dashboards
for dash in $(curl -s -H "Authorization: Bearer $KEY" "$HOST/api/search?query=&" | jq -r '.[] | select(.type == "dash-db") | .uid'); do
curl -s -H "Authorization: Bearer $KEY" "$HOST/api/dashboards/uid/$dash" | jq -r > dashboards/${dash}.json
slug=$(cat dashboards/${dash}.json | jq -r '.meta.slug')
mv dashboards/${dash}.json dashboards/${dash}-${slug}.json
done |
This comment has been minimized.
This comment has been minimized.
thanks. Working. |
This comment has been minimized.
This comment has been minimized.
mmmm... which script to use :D |
This comment has been minimized.
This comment has been minimized.
hi, I tried the one from @magiconair (this one) which is working pretty well so far, on importing it to another grafana instance (like copypaste content of the exportet .json file on import via GUI)
any suggestions, how to export all dashboards (at once) formatted like the ones you get by clicking "Download JSON" on https://grafana.com/grafana/dashboards ? =P thanks in advance & best regards :) |
This comment has been minimized.
This comment has been minimized.
fwiw some versions of jq have a "feature" that breaks pipes. If you;re having trouble, make sure you use |
This comment has been minimized.
This comment has been minimized.
Yet another iteration of this idea:
|
This comment has been minimized.
This comment has been minimized.
Can someone patch grafana-cli to export dashboards to a single (as a most simple task) JSON ? :) |
This comment has been minimized.
This comment has been minimized.
Used @magiconair script with a slight modification to use username/password and a fix to the second 'jq' call. Here's the modified script and a container I ran this into.
|
This comment has been minimized.
Thanks, was looking for something simple like this👍