Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ############################################################ IDENT(1)
- #
- # $Title: Script to make backup of Grafana dashboards $
- # $Copyright: 2019-2022 Devin Teske. All rights reserved. $
- # $FrauBSD$
- #
- ############################################################ INFORMATION
- #
- # Accesses a remote Grafana instance via HTTPS using an API key (created by an
- # administrator of said Grafana instance) to download the JSON models of all
- # dashboards, saved to the current working directory (CWD).
- #
- # Each time `make' is run, each Grafana dashboard is downloaded and compared to
- # the existing JSON model (if previously downloaded) in CWD. If the model has
- # changed, the message is printed:
- #
- # U dashboard.json
- # U dashboard.meta.json
- #
- # NB: The `U' stands for `updated' (familiar to users of sccs/rcs/cvs)
- #
- # Where `dashboard' is the name of the dashboard (specifically its internal
- # ``slug'' name) and dashboard.meta contains the metadata properties.
- #
- # This backup script requires (sorted alphabetically):
- # cat [coreutils]
- # cmp [diffutils]
- # curl
- # jq
- # make
- # mv [coreutils]
- # rm [coreutils]
- # sh (any flavor)
- # xargs [findutils]
- #
- # NB: RedHat/CentOS package listed in brackets if different than utility name.
- #
- ############################################################ CONFIGURATION
- #
- # Grafana to backup
- # NB: HOST must be fully qualified
- # NB: Access is via HTTPS on configured PORT
- #
- HOST = ip_or_hostname
- PORT = 3000
- #
- # API Key for accessing the above Grafana
- # NB: Required
- #
- API_KEY = unquoted_api_key_here
- ############################################################ FUNCTIONS
- DIE = sh -c 'fmt="$$1"; \
- if [ "$$fmt" ]; then \
- shift 1; \
- printf "$$fmt\n" "$$@" >&2; \
- fi; \
- exit 1; \
- ' sh
- API_GET = sh -c 'uri="$$1" output="$${2:-/dev/stdout}"; \
- curl -sLo- -H "Authorization: Bearer $(API_KEY)" \
- "https://$(HOST):$(PORT)/api/$$uri" > "$$output"; \
- ' sh
- JSON_PUT = sh -c 'input="$$1"; \
- json=$$( cat "$$input" ); \
- slug=$$( echo "$$json" | jq -r .meta.slug ); \
- output1=".$$slug.json"; \
- output2=".$$slug.meta.json"; \
- echo "$$json" | jq .dashboard > "$$output1"; \
- echo "$$json" | jq .meta > "$$output2"; \
- rm -f "$$input"; \
- echo "$$slug"; \
- ' sh
- ############################################################ TARGETS
- all:
- @set -e; \
- json=$$( $(API_GET) search ); \
- case "$$json" in \
- "") $(DIE) "API search failed" ;; \
- {*}) msgtype=$$( echo "$$json" | jq -r '.message|type' ); \
- [ "$$msgtype" = "null" ] || $(DIE) "%s: %s" \
- "$(HOST)" "$$( echo "$$json" | jq -r .message )"; \
- esac; \
- uids=$$( echo "$$json" | \
- jq -r 'map(select(.type=="dash-db"))|.[].uid' ); \
- [ "$$uids" ] || $(DIE) "No dashboards found"; \
- echo "$$uids" | \
- xargs -P99 -rn1 -I%%% $(API_GET) dashboards/uid/%%% .%%%.tmp; \
- trap "rm -f .*.tmp" EXIT; \
- slugs=$$( echo "$$uids" | \
- xargs -P99 -rn1 -I%%% $(JSON_PUT) .%%%.tmp ); \
- trap "rm -f .*.json" EXIT; \
- for slug in $$slugs; do \
- ! cmp -s ".$$slug.json" "$$slug.json" 2> /dev/null || \
- continue; \
- echo "U $$slug.json"; \
- mv -f ".$$slug.json" "$$slug.json"; \
- echo "U $$slug.meta.json"; \
- mv -f ".$$slug.meta.json" "$$slug.meta.json"; \
- done
- ################################################################################
- # END
- ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement