diff --git a/send-zabbix-backup-info.sh b/send-zabbix-backup-info.sh new file mode 100755 index 0000000..e3e9ea0 --- /dev/null +++ b/send-zabbix-backup-info.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +if [ $# -eq 0 ] +then + echo "No argument supplied. Use this script with:" + echo "send-zabbix-backup-info.sh " + exit 0 +fi + +SERVICE_NAME=$1 +DURATION_MS=$2 +FILES_CREATED=$3 +BYTES_SENT=$4 +END_DATE=$5 + +if ! command -v zabbix_sender >/dev/null 2>&1 +then + echo "zabbix_sender could not be found. Is the package zabbix-sender installed?" + exit 1 +fi +echo " == Sending backup information to zabbix" +# send completed +zabbix_sender -s "$(hostname)" -k "backup.status[$SERVICE_NAME] -o 1" +zabbix_sender -s "$(hostname)" -k "backup.duration[$SERVICE_NAME] -o $DURATION_MS" +zabbix_sender -s "$(hostname)" -k "backup.files[$SERVICE_NAME] -o $FILES_CREATED" +zabbix_sender -s "$(hostname)" -k "backup.bytes[$SERVICE_NAME] -o $BYTES_SENT" +zabbix_sender -s "$(hostname)" -k "backup.date[$SERVICE_NAME] -o '$END_DATE'" \ No newline at end of file diff --git a/tar-and-backup-service.sh b/tar-and-backup-service.sh index 88d923a..12c8393 100755 --- a/tar-and-backup-service.sh +++ b/tar-and-backup-service.sh @@ -5,7 +5,8 @@ if [ $# -eq 0 ] then echo "No argument supplied. Use this script with:" - echo "tar-and-backup-service.sh " + echo "tar-and-backup-service.sh [-z]" + echo "Use the -z flag to also send information about the backup to zabbix" exit 0 fi @@ -13,10 +14,34 @@ SERVICE_NAME=$1 BACKUP_FOLDER=$2 TAR_FILE=$SERVICE_NAME.tar.gz +START_MS=$(date +%s%3N) + echo " == Backing up $SERVICE_NAME to $TAR_FILE" echo " == Creating tar.gz file" tar czvf $TAR_FILE $BACKUP_FOLDER echo " == transfering tar file" -rsync -avz --stats --progress $TAR_FILE sem@10.10.100.52:/home/sem/$SERVICE_NAME/ + +TMPFILE=$(mktemp) +rsync -avz --stats --progress $TAR_FILE sem@10.10.100.52:/home/sem/$SERVICE_NAME/ | tee "$TMPFILE" +RSYNC_OUTPUT="$(cat $TMPFILE)" +rm "$TMPFILE" + echo " == Removing local tar file" -rm $TAR_FILE \ No newline at end of file +rm $TAR_FILE + +BYTES_SENT=$(echo "$RSYNC_OUTPUT" | grep "Total transferred file size" | cut -d: -f2 | cut -d' ' -f2 | tr -d ' ' | tr -d ',') +FILES_CREATED=$(echo "$RSYNC_OUTPUT" | grep "Number of regular files transferred" | cut -d: -f2 | tr -d ' ') + +END_MS=$(date +%s%3N) +DURATION_MS=$((END_MS - START_MS)) +END_DATE=$(date "+%d-%m-%Y %H:%M:%S") + +echo " == Backup completed in $DURATION_MS ms" +echo " == Total bytes sent: $BYTES_SENT" +echo " == Total files created: $FILES_CREATED" + +if [ "$3" == "-z" ] +then + SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + "$SCRIPT_DIR/send-zabbix-backup-info.sh" $SERVICE_NAME $DURATION_MS $FILES_CREATED $BYTES_SENT "$END_DATE" +fi \ No newline at end of file diff --git a/update-zabbix-backup-status.sh b/update-zabbix-backup-status.sh new file mode 100755 index 0000000..8d94ab3 --- /dev/null +++ b/update-zabbix-backup-status.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +# script to send info about a backup to zabbix. +# Usage: +# update-zabbix-backup-status.sh [-sf] HOSTNAME SERVICE_NAME [DURATION] [SIZE] +# parameters: +# -s Backup succeeded (required if not failed) +# -f Backup failed (required if not succeeded) +# HOSTNAME hostname of the host that performed the backup +# SERVICE_NAME name of the service that was backed up +# DURATION duration of the backup +# SIZE Size in bytes of the transfered files + +function print_help +{ + echo "script to send info about a backup to zabbix." + echo "Usage:" + echo " update-zabbix-backup-status.sh [-sf] HOSTNAME SERVICE_NAME [DURATION] [SIZE]" + echo "parameters:" + echo " -s Backup succeeded (required if not failed)" + echo " -f Backup failed (required if not succeeded)" + echo " -h Show this message" + echo " HOSTNAME hostname of the host that performed the backup" + echo " SERVICE_NAME name of the service that was backed up" + echo " DURATION duration of the backup. Only needed if succeeded" + echo " SIZE Size in bytes of the transfered files. Only needed if succeeded" +} + +if [ -z $1 ] +then + echo "Please provide arguments. Use -h to show help" + exit 0 +fi + +if [ "$1" = "-h" ] +then + print_help +fi + +if ! command -v zabbix_sender >/dev/null 2>&1 +then + echo "zabbix_sender could not be found. Is it and the zabbix_agent2 installed?" + exit 1 +fi + +# TODO implement sending items to zabbix +# Use zabbix_sender directly? (for some reason not installed on games vm) https://www.zabbix.com/documentation/current/en/manual/concepts/sender + +# Or create python script and use python library? https://www.zabbix.com/documentation/current/en/devel/python +# if using python script I can run the rsync backup command using subprocess, and catch the output of that +# then I can check the bytes sent to calculate the size + \ No newline at end of file