Simple dashboard backup after deprecation of db/:slug

Slowly climbing the learning curve. :slightly_smiling_face:
Just lost “heureka”-solution in a dashboard - by accidentialy hitting a browser button :face_with_spiral_eyes:

Desperate for a simple backup solution - one that I can understand as a Grafana newbie.
“Wizzy” seems not maintained any more, documentation and examples point to nirwana.

There was a simple skript out there

which looked liked it might fulfil my purpose.

After tearing my hair off, I found that it was broken due to a “Verschlimmbesserung” (sorry, english ‘disimprovement’ lacks the ironic subtility)

Old api url are deprecated.
Replacing uri by uid breaks readable directory.
Fixing both together breaks simplicity of the bash loop.

Anybody out who repaired this already? Or similiar?

a crude “works for the moment” solution:

  • rewrite the downloader to use uri urls
  • save under the cryptic uri (keeping the simple bash loop)
  • add a ‘map’ file by a simple grep, so in desperation for a backup, one can manually lookup the uri pertaining to a dashboard name
  • tar-zip the whole to a time stamped archive, so the script can be called repeatedly without much thinking to prouduce skd of poor-man’s versioning
$ cat grafana-dashboard-exporter.sh 
#!/usr/bin/env bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# KEY=$(<~/.grafanakey)
KEY=$(< ./.grafanakey)

# HOST="https://mdmdev.cloudtrust.rocks"
HOST="http://localhost:3000"

if [ ! -d $SCRIPT_DIR/dashboards ] ; then
    mkdir -p $SCRIPT_DIR/dashboards
fi

# for dash in $(curl -k -H "Authorization: Bearer $KEY" $HOST/api/search\?query\=\& | jq -r '.[] | .uri'); do
#   curl -k -H "Authorization: Bearer $KEY" $HOST/api/dashboards/$dash | sed 's/"id":[0-9]\+,/"id":null,/' | sed 's/\(.*\)}/\1,"overwrite": true}/' | jq . > dashboards/$(echo ${dash} |cut -d\" -f 4 |cut -d\/ -f2).json
# done


for dash in $(curl -k -H "Authorization: Bearer $KEY" $HOST/api/search\?query\=\& | jq -r '.[] | .uid' ); do
  # dash=`echo ${json} | jq -r '.[] | .uri'`
  # uid=`echo ${json} | jq -r '.[] | .uid'`
  # echo ${json}
  URL=$HOST/api/dashboards/uid/$dash
  echo $URL
  curl -k -H "Authorization: Bearer $KEY" $URL | sed 's/"id":[0-9]\+,/"id":null,/' | sed 's/\(.*\)}/\1,"overwrite": true}/' | jq . > dashboards/$(echo ${dash} |cut -d\" -f 4 |cut -d\/ -f2).json
done


grep  '"slug": ' dashboards/*.json > dashboards/map

DT_NOW=`date +"%F_%H%M%S"`
TGZ=dashboards_${DT_NOW}.tgz
echo $TGZ
tar -cvzf $TGZ dashboards/

few other options

  • just simply backup the grafana.db
  • use provisioning and save your provision yaml to github/gitlab

Thanks for the hints.
Considered those, but

  • db backup does not allow dedicated restore of selected items (e.g. dahboard, panel or query). JSON is at least “a little bit” human readable.
  • as far as I understand, the YAML apporach breaks interactive UI development, so I think it is more apt to Grafana pros who know what they do.

Maybe i’m wrong, of course.

1 Like