Skip to content

Instantly share code, notes, and snippets.

@thorian93
Created June 23, 2021 16:34
Show Gist options
  • Save thorian93/745ee8a7eb32d247f06de9d27b904ec8 to your computer and use it in GitHub Desktop.
Save thorian93/745ee8a7eb32d247f06de9d27b904ec8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Written by: Robin Gierse - info@thorian93.de - on 20200911
#
# Purpose:
# This is a check script for borg backups as seen from the backup client.
#
# Version: 0.1 on 20200911
# Version: 0.2 on 20200913
#
# Usage:
# ./checkmk_check_borg_local.sh
borg_repository='$BORG_REPOSITORY'
_initialize() {
today="$(date +%Y-%m-%d)"
yesterday="$(date +%Y-%m-%d --date=yesterday)"
borg_backups="$(borg list $borg_repository | cut -d ' ' -f 1)"
borg_repo_usage="$(borg info $borg_repository | grep 'All archives:' | tr -s '[:space:]')"
}
_check_state() {
for backup in $borg_backups
do
day="$(echo $backup | cut -d '-' -f 5,6,7 | cut -d 'T' -f 1)"
retention="$(echo $backup | cut -d '-' -f 4)"
finished="$(echo $backup | cut -d 'T' -f 2)"
server="$(echo $backup | cut -d '-' -f 1,2,3)"
if [ "$day" == "$today" ]
then
check_rc='0'
check_state='OK'
check_output="Last Backup was today on $day"
elif [ "$day" == "$yesterday" ]
then
check_rc='1'
check_state='WARN'
check_output="Last Backup was yesterday on $day"
elif [ "$day" > "$yesterday" ]
then
check_rc='2'
check_state='CRIT'
check_output="Last Backup was on $day!"
else
check_rc='3'
check_state='UNKN'
fi
done
}
_check_perfdata() {
orig_size="$(echo $borg_repo_usage | cut -d ' ' -f 3)"
orig_unit="$(echo $borg_repo_usage | cut -d ' ' -f 4)"
comp_size="$(echo $borg_repo_usage | cut -d ' ' -f 5)"
comp_unit="$(echo $borg_repo_usage | cut -d ' ' -f 6)"
dedup_size="$(echo $borg_repo_usage | cut -d ' ' -f 7)"
dedup_unit="$(echo $borg_repo_usage | cut -d ' ' -f 8)"
original_size="'original_size'=$orig_size[$orig_unit]"
compressed_size="'compressed_size'=$comp_size[$comp_unit]"
deduplicated_size="'deduplicated_size'=$dedup_size[$dedup_unit]"
}
_print_state() {
echo -e "$check_state: $check_output - Usage: $dedup_size$dedup_unit | $original_size $compressed_size $deduplicated_size\nOriginal Size: $orig_size$orig_unit Compressed Size: $comp_size$comp_unit Deduplicated Size: $dedup_size$dedup_unit"
exit "$check_rc"
}
_initialize
_check_state
_check_perfdata
_print_state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment