#!/bin/sh
VERSION=260130
SCRIPT=$(readlink -f $0)
SCRIPTNAME=$(basename $SCRIPT)
SCRIPTDIR=$(dirname $SCRIPT)
if [ "$1" = "-v" ] ; then echo "$VERSION $SCRIPTNAME $SCRIPTDIR";exit 0; fi
if [ -e "$SCRIPTDIR/esc.sh" ] ; then . "$SCRIPTDIR/esc.sh"; fi
LOG=$LOGDIR/log-$SCRIPTNAME.log

BASEDIR=$(dirname $SCRIPTDIR)
if [ -e "$SCRIPTDIR/settings.sh" ] ; then source "$SCRIPTDIR/settings.sh"; fi
if [ -e "$SCRIPTDIR/settings.local" ] ; then source "$SCRIPTDIR/settings.local"; fi

#echo $bkpdir

echo "searching for running ghetto and vmkfstools processes..."
#https://www.xinux.net/index.php/GhettoVCB
ps -c | grep ghettoVCB | grep -vE "grep|abort"
for pid in $(ps -c | grep ghettoVCB | grep -vE "grep|abort" | awk {'print $1'})
do
        echo -n "stopping ghetto process $pid : "
        kill -9 $pid
        if [ $? == 0 ] ; then echo "OK"; else echo "FAILED"; fi
done

ps -c | grep vmkfstools | grep -v grep
for pid in $(ps -c | grep vmkfstools | grep -v grep | awk {'print $1'})
do
        echo -n "stopping vmkfs process $pid : "
        kill -9 $pid
        if [ $? == 0 ] ; then echo "OK"; else echo "FAILED"; fi
done

echo "searching for incomplete backups..."
sts=$(date +"%Y-%m-%d")
bkps=$(find $bkpdir -mtime 0 -name STATUS.ok)
count=$(echo "$bkps" | wc -l)
vms=$($SCRIPTDIR/list.sh -c) # get the count of all to be backupped VMs
#echo "ts: $ts bkpvol: $bkpdir count: $count vms: $vms"
if [ $count != 0 ] ; then
        echo "[ERROR] count does not match! $count/$vms"
fi

for vm in $($SCRIPTDIR/list.sh show)
do
        echo -n "vm:$vm backup:"
        #ls -1 $bkpdir/$vm/$vm-$sts*
        tdir=$(find $bkpdir/$vm/$vm-$sts* -maxdepth 0 -type d 2>/dev/null)
        if [ $? != 0 ] ; then echo "notfound"; continue; fi
        #echo tdir:$tdir
        if [ -z "$tdir" ] ; then continue; fi
        if [ ! -e "$tdir/STATUS.ok" ] ; then
                echo "error"
                echo -e "\tdeleting:$tdir"
                rm -rf $tdir
        else
                echo "ok"
        fi
done

exit

