[Debian] Grafana backup and import
#Step 01 –
#!/bin/sh
#-*- coding: utf-8 -*-
#===============================================================================
#
# Filename: backup_dash.sh
#
# USAGE: backup_dash.sh
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Oscarob Wu(oscarobwu@gmail.com),
# ORGANIZATION:
# VERSION: 1.0
# Created Time: 2021-02-25 17:22:52
# Last modified: 2021-02-25 17:24
# REVISION: ---
#===============================================================================
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
# 修改主機
HOST="https://xxx.xxx.xxx.xxx/grafana"
# 修改 API KEY
KEY="eyJrIjoiMU9kVTZiSkxzdVRiWWVQeHB0QkxxxxxxxxxxxIiLCJuIjoiQmFja3VwX2FwaSIsImlkIjoxfQ=="
# 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}')
#DASH_DIR=$(date '+%Y-%m-%d-%H-%M')-$(echo $HOST | awk -F[/:] '{print $4}')
DASH_DIR=$(echo $HOST | awk -F[/:] '{print $4}')-$(date '+%Y-%m-%d-%H-%M')
if [ ! -d "${DASH_DIR}" ]; then
mkdir "${DASH_DIR}"
else
log_title "----------------- A $DASH_DIR directory already exists! -----------------"
fi
}
#HOST=$1
#KEY=$2
#HOST="http://127.0.0.1/grafana"
#KEY="eyJrIjoid1ZmOFhSMHZzRjFKTVdsWjxxxxxxxxxZ3TXQ0MkwiLCJuIjoiYWFwcCIsImlkIjoxfQ=="
init $# $HOST $KEY
counter=0
for dashboard_uid in $(curl -sS --insecure -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 --insecure -H "Authorization: Bearer $KEY" $url | jq -r '.')
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"
curl -sS --insecure -H "Authorization: Bearer $KEY" $url | jq . > "$DASH_DIR/$folder_title/${dashboard_title}_v${dashboard_version}.json"
# echo 'jq . {$dashboard_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 ---------------------------------";
#Step 01 –
#Step 02 – import
#!/bin/bash
#-*- coding: utf-8 -*-
#===============================================================================
#
# Filename: grafana-dashboard-importer_149.sh
#
# USAGE: grafana-dashboard-importer_149.sh
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Oscarob Wu(oscarobwu@gmail.com),
# ORGANIZATION:
# VERSION: 1.0
# Created Time: 2021-02-24 14:06:50
# Last modified: 2021-02-25 18:27
# REVISION: ---
#===============================================================================
#
# add the "-x" option to the shebang line if you want a more verbose output
# 1xxxxxx
# curl -H "Authorization: Bearer eyJrIjoiVEJ1Yk9aZW41UDh3NDdIaTQwS2UwMEZOQU4xxxxxxJuIjoiYmFja3VwIiwiaWQiOjF9" http://xxxxxxxxx/grafana/api/dashboards/home
# bash grafana-dashboard-importer_A.sh -p 192.168.96.126-2021-02-24-18-13/123 -t 192.168.96.126 -k eyJrIjoiWnd2RU1hZDBVM05XZ1RQUVV5ODI0bHFVNndCaW5nZ0giLCJuIjoiQmFja3VwIiwiaWQiOjF9
#
#
OPTSPEC=":hp:t:k:f:"
show_help() {
cat << EOF
Usage: $0 [-p PATH] [-t TARGET_HOST] [-k API_KEY]
Script to import dashboards into Grafana
-p Required. Root path containing JSON exports of the dashboards you want imported.
-t Required. The full URL of the target host
-k Required. The API key to use on the target host
-f Required. The API key to use on the Folders
-h Display this help and exit.
EOF
}
###### Check script invocation options ######
#while getopts "$OPTSPEC" optchar; do
# case "$optchar" in
# h)
# show_help
# exit
# ;;
# p)
# DASH_DIR="$OPTARG";;
# t)
# HOST="https://$OPTARG";;
# k)
# KEY="$OPTARG";;
# \?)
# echo "Invalid option: -$OPTARG" >&2
# exit 1
# ;;
# :)
# echo "Option -$OPTARG requires an argument." >&2
# exit 1
# ;;
# esac
#done
while getopts "$OPTSPEC" optchar; do
case "$optchar" in
h)
show_help
exit
;;
p)
DASH_DIR="$OPTARG";;
f)
FKEY="$OPTARG";;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
#if [ -z "$DASH_DIR" ] || [ -z "$HOST" ] || [ -z "$KEY" ]; then
# show_help
# exit 1
#fi
if [ -z "$DASH_DIR" ] || [ -z "$FKEY" ] ; then
show_help
exit 1
fi
#############################################################
HOST="https://xxxx.xxxx.xxx.xxx"
KEY="eyJrIjoiWG01VkgyNFllZkNCxxxxxxxNGtxMWRYMkVtZnEwY1kiLCJuIjoiQmFja3VwIiwiaWQiOjF9"
# 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 "[%s] $1\n" "$timestamp"
${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 "[%s] $1\n" "$timestamp"
${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 "|%s|\n" "$1";
printf "|-------------------------------------------------------------------------------------|\n"
${SETCOLOR_NORMAL}
}
if [ -d "$DASH_DIR" ]; then
DASH_LIST=$(find "$DASH_DIR" -mindepth 1 -name \*.json)
if [ -z "$DASH_LIST" ]; then
log_title "----------------- $DASH_DIR contains no JSON files! -----------------"
log_failure "Directory $DASH_DIR does not appear to contain any JSON files for import. Check your path and try again."
exit 1
else
FILESTOTAL=$(echo "$DASH_LIST" | wc -l)
log_title "----------------- Starting import of $FILESTOTAL dashboards -----------------------------"
fi
else
log_title "----------------- $DASH_DIR directory not found! -----------------"
log_failure "Directory $DASH_DIR does not exist. Check your path and try again."
exit 1
fi
NUMSUCCESS=0
NUMFAILURE=0
COUNTER=0
for DASH_FILE in $DASH_LIST; do
COUNTER=$((COUNTER + 1))
echo "Import $COUNTER/$FILESTOTAL: $DASH_FILE..."
RESULT=$(cat "$DASH_FILE" | jq '. * {overwrite: true, dashboard: {id: null}, folderId: '$FKEY'}' | curl -ks -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $KEY" "$HOST"/grafana/api/dashboards/db -d @-)
if [[ "$RESULT" == *"success"* ]]; then
log_success "$RESULT"
NUMSUCCESS=$((NUMSUCCESS + 1))
else
log_failure "$RESULT"
NUMFAILURE=$((NUMFAILURE + 1))
fi
done
log_title "Import complete. $NUMSUCCESS dashboards were successfully imported. $NUMFAILURE dashboard imports failed.";
log_title "------------------------------ FINISHED ---------------------------------";
沒有留言:
張貼留言