#!/bin/bash
# ======================================================================
#
# check backup status on storage
#
# ----------------------------------------------------------------------
# 2016-12-02  created  hahn
# 2018-02-14  v1.1     hahn   configure time limit
# 2019-10-20  v1.3     hahn   source inc_config.sh
# 2020-12-07  v1.4     hahn   deactivate backup
# 2021-06-16  v1.5     hahn   handle multiple backup tools
# ----------------------------------------------------------------------


. `dirname $0`/inc_config.sh

cd ${sBackupBasedir} || exit 1

sVersion=1.5
typeset -i iErrors=0
typeset -i iHours=36
typeset -i iMinutes=$iHours*60

typeset -i iKeepBackups=180
sInactiveFile=inactive.txt

# ----------------------------------------------------------------------
#FUNCTIONS
# ----------------------------------------------------------------------

  # ------------------------------------------------------------
  # color text
  # ------------------------------------------------------------
  function color(){
    sColorcode=""
    case $1 in
      "reset") sColorcode="0"
               ;;
      "head")  sColorcode="33" # yellow
               ;;
      "cmd")   sColorcode="94" # light blue
               ;;
      "input") sColorcode="92" # green
               ;;
      "ok") sColorcode="92" # green
               ;;
      "error") sColorcode="91" # red
               ;;
    esac
    if [ ! -z ${sColorcode} ]; then
      echo -ne "\e[${sColorcode}m"
    fi
  }

# get age of a file in sec
# param  string  filename to test
#
function _getFileAge(){
    echo $(($(date +%s) - $(date +%s -r "$1")))
}

function getServers(){
        # ls -1 | fgrep "iml.unibe.ch"
        ls -1d */* | grep -Ev "/(_active|#recycle|SOMETHINGELSE)"
}



function _checkDir(){
        typeset -i iCount

        mydir=$1

        if [ -L $mydir ]; then
            mydir="`ls -l $mydir | awk '{print $11}'`"
            echo "INFO: Link detected to $mydir"
        fi

        if [ -d $mydir ]; then

                iCount=`find $mydir -type f -cmin -$iMinutes | wc -l `

                if [ $iCount -eq 0 ]; then
                        color error
                fi

                echo -ne "$iCount\t"
                echo -ne "`du -hs $mydir #| awk ' { print $1 } '`\t"

                if [ $iCount -eq 0 ]; then
                        echo -n "<<< WARNING: No changed files detected !!! rc=1"
                        color reset
                        iErrors=$iErrors+1
                fi
                echo

        else
                echo "SKIP: $mydir won't be scanned. It isn't a directory."
        fi
}

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

echo
color head
echo "===== BACKUP STATUS $sVersion ====="
color reset
echo



echo "time             : `date`"
echo "backup directory : `pwd`"
# echo "used space       : `du -hs $mydir | awk ' { print $1 } '`"
echo "free space       : `df -h . | tail -1 |  awk ' { print $4 } '`"
echo
echo "changed files last $iHours hours and used diskspace:"
echo
# _checkDir .
# echo
# echo "... and by server and backup sets:"
# echo
for myserver in `getServers`
do
        color head
        echo ----- SERVER: $myserver
        color reset
        echo
        if [ -f $myserver/$sInactiveFile ]; then
                du -hs $myserver
                echo
                echo "INFO: server is marked as inactive."
                ls -l $myserver/$sInactiveFile
                cat $myserver/$sInactiveFile
                typeset -i iAge=`_getFileAge "$myserver/$sInactiveFile"`
                typeset -i iDays=$iKeepBackups-$iAge/60/60/24
                # echo Age: $iAge s
                if [ $iDays -lt 0 ]; then
                        color error
                        echo "ERROR: Outdated backup was found! You can delete data for $myserver."
                        color reset
                        iErrors=$iErrors+1
                else
                        echo "INFO: keeping it $iKeepBackups d .. $iDays d left."
                fi
        else
                _checkDir $myserver
                echo $myserver | grep "restic-backup" > /dev/null || for subdir in `ls -1 $myserver`
                do
                        _checkDir $myserver/$subdir
                done
        fi
        echo
done


# --- show result
echo -n "Status: "
if [ $iErrors -eq 0 ]; then
        color ok
        echo "OK"
else
        color error
        echo "Found errors: $iErrors"
fi
color reset

echo rc=$iErrors
exit $iErrors
