Skip to content

Instantly share code, notes, and snippets.

@artishan
Created April 20, 2015 03:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artishan/e6ca8dd7f9668ed939a4 to your computer and use it in GitHub Desktop.
Save artishan/e6ca8dd7f9668ed939a4 to your computer and use it in GitHub Desktop.
Grafana - infulxdb dahsboad data backup & restore
#!/bin/bash
# Grafana infulxdb dahsboad data backup & restore
# example usages >> ./grafana_backup.sh dump grafana -u backup -p pasword -h 127.0.0.1:8086
function parse_options {
function usage() {
echo -e >&2 "Usage: $0 dump DATABASE [options...]
\t-u USERNAME\t(default: backup)
\t-p PASSWORD\t(default: pasword)
\t-h HOST\t\t(default: localhost:8086)
\t-s\t\t(use HTTPS)"
}
if [ "$#" -lt 2 ]; then
usage; exit 1;
fi
username=bakcup
password=backup
host=localhost:8086
https=0
shift
database=$1
shift
while getopts u:p:h:s opts
do case "${opts}" in
u) username="${OPTARG}";;
p) password="${OPTARG}";;
h) host="${OPTARG}";;
s) https=1;;
?) usage; exit 1;;
esac
done
if [ "${https}" -eq 1 ]; then
scheme="https"
else
scheme="http"
fi
}
function dump {
parse_options $@
curl -s -k -G "${scheme}://${host}/db/${database}/series?u=${username}&p=${password}&time_precision=s" \
--data-urlencode "q=select id, time, title, tags, dashboard from /grafana.dashboard_.*/ where title =~ /.*.*/i" \
| python -mjson.tool > backup/${database}.json
echo 'DUMP :: Grafana dashboard data'
commit $@;
exit
}
function commit {
parse_options $@
commit_date=$(date +%Y-%m-%d_%H:%M:%S)
git commit -q -a -m DUMP:grafana_dahsboard_$commit_date
git push origin
exit
}
function restore {
parse_options $@
curl -X POST --data-binary @backup/${database}.json \
-H "Content-Type: application/json" "${scheme}://${host}/db/${database}/series?u=${username}&p=${password}"
echo 'RESTORE :: Grafana dashboard data'
exit
}
case "$1" in
dump) dump $@;;
restore) restore $@;;
commit) commit $@;;
*) echo >&2 "Usage: $0 [dump|restore|commit] ..."
exit 1;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment