Skip to content
Snippets Groups Projects
check_clientbackup.sh 3.25 KiB
#!/bin/bash
# ==============================================================================
#
# check status of last performed backup
#
# This script checks the last logfile and scans for errors
# An error is a returncode <> 0 or a too old logfile
#
# ------------------------------------------------------------------------------
#
# ah - Axel Hahn <axel.hahn@iml.unibe.ch>
# ds - Daniel Schueler <daniel.schueler@iml.unibe.ch>
#
# 2016-12-09  ah,ds  v1.0
# 2022-01-19  ah     v1.1  fixes with shellcheck
# ==============================================================================

. $(dirname $0)/jobhelper.sh

# ------------------------------------------------------------------------------
# CONFIG
# ------------------------------------------------------------------------------

typeset -i iOK=0

# limit when to warn if no backup was started
typeset -i iMaxAgeInHours=96

typeset -i iAge
typeset -i iAge2
typeset -i iError

logdir=$(dirname "$0")/logs


# ------------------------------------------------------------------------------
# MAIN
# ------------------------------------------------------------------------------

sShort="Client Backup -"

ls "$logdir"/*.log >/dev/null
if [ $? -ne 0 ]; then
  sShort="$sShort logs were not found. Backup was never executed"
  iError=1
else

  # iMaxAgeInHours=`j_getLastBackupAge`
  # echo verify last backup: $iMaxAgeInHours h ago

  for logfile in $(ls -1t "$logdir"/inc*.log "$logdir"/full*.log "$logdir"/auto*.log 2>/dev/null | head -1)
  do
    echo LAST BACKUP: "$logfile"
    grep "final.*rc=" "$logfile"
    echo

    # --- SENSU Statusline
    if [ -f "$(dirname $0)"/transfer.sh ]; then
      sShort="$sShort WITH"
    else
      sShort="$sShort NO"
    fi
    sShort="$sShort transfer -"

    # --- count returncodes
    iOK=$(grep -c "final.*rc=0$" "$logfile" )
    iError=$(grep "final.*rc=" "$logfile" | grep -cv "rc=0$")
    echo "OK: $iOK ... Errors: $iError"

    # --- rc=0 must be here
    if [ $iOK -eq 0 ]; then
      echo "ERROR: no OK message was found. Something is messed up :-/"
      iError=$iError+1
    fi

    # --- check age
    iAge=$( date +%s )-$( date +%s -r "$logfile" )
    iAge2=$iAge/60/60
    echo "age: $iAge sec ... $iAge2 h"
    sShort="$sShort last backup log: $(basename "$logfile") ($iAge2 h ago) "
    if [ $iAge2 -gt $iMaxAgeInHours ]; then
      echo "Error: the last backup is older than $iMaxAgeInHours hours"
      sShort="$sShort ERROR: backup is older than $iMaxAgeInHours hours "
      iError=$iError+1
    else
      echo "OK: last backup is younger $iMaxAgeInHours hours"
      sShort="$sShort OK: backup is younger than $iMaxAgeInHours hours "
    fi

    # --- changes (incremental backups only) and backup status infos
    echo
    sSearch="Args:|ElapsedTime|SourceFiles|SourceFileSize|RawDeltaSize"
    echo "$logfile" | grep -F "inc" >/dev/null
    if [ $? -eq 0 ]; then
      sSearch="[ADM]\ |${sSearch}"
    fi
    echo --- changes:
    grep -E "^(${sSearch})" "$logfile"

    echo
    echo --- summary of backed up directories:
    grep "DIR\ " "$logfile"
  
    sShort="$sShort - OK: $iOK ... Errors: $iError"

  done


fi


echo
echo "MONITORINFO: $sShort"
echo "STATUS $0 - final returncode rc=$iError"
exit "$iError"

# ------------------------------------------------------------------------------