my-scripts/CheckBackup.sh

171 lines
8.6 KiB
Bash
Executable file

#!/bin/bash
#
# Check if anarcon backup was done and give an estimation about the next run
# (c) GPL by Ulf Bartolomäus ub1x@gmx.net
VERSION="Version 0.6.0 from 30.01.2022"
## History
# 0.6.0
# Initial Version
# Remark, w/o parameter the script checks only if there is a reboot or shutdown required
# Put the script w/o parameter in the hourly cronjob /etc/cron.hourly/$0
doReboot='/root/doReboot';
doShutdown='/root/doShutdown';
wakeTime='23:30'; # start system for next update at this local time default 23:30 o'clock
rebootDelay=$(( 0 )); # delay in loops
stopBackup='/root/stopBackup';
BackupLock='/tmp/Backup.lock';
LogFile="/var/log/$( basename $0 .sh ).log"
#[ -f /var/spool/anacron/cron.daily ] && NextDaily=$( date -d $(cat /var/spool/anacron/cron.daily) --date="next day" +%F ) || NextDaily=$( date --date="next day" +%F )
#[ -f /var/spool/anacron/cron.weekly ] && NextWeekly=$( date -d $(cat /var/spool/anacron/cron.weekly) --date="next week" +%F ) || NextWeekly=$( date --date="next sunday" +%F )
#[ -f /var/spool/anacron/cron.monthly ] && NextMonthly=$( date -d $(cat /var/spool/anacron/cron.monthly) --date="next month" +%F ) || NextMonthly=$( date -d "$(date +%Y%m01) +1 month" +%F )
[ -f /var/spool/anacron/cron.daily ] && LastDaily="$(cat /var/spool/anacron/cron.daily)" || LastDaily="Unbekannt"
[ -f /var/spool/anacron/cron.weekly ] && LastWeekly="$(cat /var/spool/anacron/cron.weekly)" || LastWeekly="Unbekannt"
[ -f /var/spool/anacron/cron.monthly ] && LastMonthly="$(cat /var/spool/anacron/cron.monthly)" || LastMonthly="Unbekannt"
[ -f /var/spool/anacron/cron.daily ] && NextDaily=$( date -d "$(cat /var/spool/anacron/cron.daily) +1 day" +%F ) || NextDaily=$( date --date="next day" +%F )
[ -f /var/spool/anacron/cron.weekly ] && NextWeekly=$( date -d "$(cat /var/spool/anacron/cron.weekly) + 7 day" +%F ) || NextWeekly=$( date --date="next sunday" +%F )
[ -f /var/spool/anacron/cron.monthly ] && NextMonthly=$( date -d "$(cat /var/spool/anacron/cron.monthly) + 1 month" +%F ) || NextMonthly=$( date -d "$(date +%Y%m01) +1 month" +%F )
## Print the last $1 lines of the LogFile
ShowTheLog () {
LogLines=$(( $1 ));
if [ ${LogLines} -gt 0 ] ; then
echo -e "\t***** Zeige ${LogLines} Zeilen des Logbuchs <${LogFile}> *****"
tail -n${LogLines} ${LogFile}
echo -e "\t***** *****"
fi
}
## Input parameter do the action else doReboot or doShutdown
if [ $# -gt 0 ] ; then
case "$1" in
stopBackup)
[ -e ${stopBackup} ] || touch ${stopBackup}
echo -e "\tstopBackup"
;;
doReboot)
[ -e ${stopBackup} ] || touch ${stopBackup}
[ -e ${doReboot} ] || touch ${doReboot}
echo -e "\tdoReboot"
[ $# -gt 1 ] && rebootDelay=$(( $2 )) && echo "${rebootDelay}" > ${doReboot} && echo -en " nach ${rebootDelay}:00 Uhr"
echo "";
;;
doShutdown)
[ -e ${stopBackup} ] || touch ${stopBackup}
[ -e ${doShutdown} ] || touch ${doShutdown}
echo -en "\tdoShutdown"
[ $# -gt 1 ] && rebootDelay=$(( $2 )) && echo "${rebootDelay}" > ${doShutdown} && echo -en " nach ${rebootDelay}:00 Uhr"
echo "";
;;
startBackup)
[ -e ${doReboot} ] && rm ${doReboot}
[ -e ${stopBackup} ] && rm ${stopBackup}
[ -e ${doShutdown} ] && rm ${doShutdown}
echo -e "\tstartBackup"
;;
undoReboot)
[ -e ${doReboot} ] && rm ${doReboot}
echo -e "\tundoReboot"
;;
undoShutdown)
[ -e ${doShutdown} ] && rm ${doShutdown}
echo -e "\tundoShutdown"
;;
checkFlag)
echo -e "\nBackup Status:"
echo -ne "\tBackup \t\t\t"
[ -e ${BackupLock} ] && echo "läuft" || echo "läuft nicht"
echo "-"
echo -ne "\tstopBackup \t\t\t"
[ -e ${stopBackup} ] && echo "gesetzt" || echo "nicht gesetzt"
echo -ne "\tdoReboot \t\t\t"
[ -f ${doReboot} ] && echo -n "gesetzt " || echo -n "nicht gesetzt"
[ -s ${doReboot} ] && echo -e " - nach $( cat ${doReboot} ):00 Uhr" || echo ""
echo -ne "\tdoShutdown \t\t\t"
[ -f ${doShutdown} ] && echo -n "gesetzt " || echo -n "nicht gesetzt"
[ -s ${doShutdown} ] && echo -e " - nach $( cat ${doShutdown} ):00 Uhr" || echo ""
echo "-"
echo -e "\tCronjob: \t\tAusgeführt\t-> Geplant\t=> Vorhandene Programme"
echo -e "\t\tDaily: \t${LastDaily}\t-> ${NextDaily}\t=> $(ls /etc/cron.daily | tr '\n' ' ')"
echo -e "\t\tWeekly: \t${LastWeekly}\t-> ${NextWeekly}\t=> $(ls /etc/cron.weekly | tr '\n' ' ')"
echo -e "\t\tMonthly:\t${LastMonthly}\t-> ${NextMonthly}\t=> $(ls /etc/cron.monthly | tr '\n' ' ')"
echo "-"
[ $# -gt 1 ] && ShowTheLog $2
;;
showLog)
[ $# -gt 1 ] && ShowTheLog $2 || ShowTheLog 5
;;
-v)
echo -e "\n\t*** $(basename $0) - ${VERSION} ***\n"
;;
*)
echo -e "\nUsage $( basename $0 ) [option]"
echo -e "\t\toption\tcheckFlag [n]\tshows flag status and if n defined also the last n-Log-lines"
echo -e "\t\toption\tshowLog [n]\tshow Log n-lines (Default = 5)"
echo -e "\t\toption\tstopBackup\tset stopBackup flag to stop starting all Backups"
echo -e "\t\toption\tstartBackup\treset stopBackup, reboot and shutdown flags"
echo -e "\t\toption\tdoReboot [t]\tset doReboot flag to reboot when all Backups are finshed and t define start hour"
echo -e "\t\toption\tundoReboot\treset doReboot flag to reboot when all Backups are finshed"
echo -e "\t\toption\tdoShutdown [t]\tset doShutdown flag to go to off till ${wakeTime} o'clock when all Backups are finshed and t define start hour"
echo -e "\t\toption\tundoShutdown\treset doShutdown flag to go to off till ${wakeTime} o'clock when all Backups are finshed and t define start hour"
echo -e "\n"
;;
esac
exit 0
fi
# if stopBackup exists and Backup don't run anymore do Reboot or Shutdown according setting
if [ -e ${stopBackup} ] ; then
if [ -e ${BackupLock} ] ; then
echo -e "\n\n\n\t*** Start $(date +%F' '%T) - $( basename $0) - ${VERSION} (PID:$$)" &>> ${LogFile}
echo -e "\t*** Backup läuft noch" &>> ${LogFile}
else
# if doReboot set
if [ -f ${doReboot} ] ; then
# if time houre exists (file bigger than 0 Byte)
if [ -s ${doReboot} ] ; then
rebootDelay=$(( $( cat ${doReboot} ) ))
echo -e "\t\t*** 1. RebootDelay vorhanden nach ${rebootDelay}:00 Uhr" &>> ${LogFile}
else
rebootDelay=$(( $( date +%_H ) ))
echo -e "\t\t*** 1. RebootDelay nicht vorhanden = jetzt (${rebootDelay}:00 Uhr)" &>> ${LogFile}
fi
if [ $(( $( date +%_H ) )) -eq ${rebootDelay} ] ; then
echo -e "\n\t\t*** 2. entferne doReboot wieder" &>> ${LogFile}
rm ${doReboot} &>> ${LogFile}
echo -e "\t\t*** 3. entferne stopBackup wieder" &>> ${LogFile}
rm ${stopBackup} &>> ${LogFile}
echo -e "\t\t*** 4. starte reboot" &>> ${LogFile}
/sbin/reboot &>> ${LogFile}
else
echo -e "\t\t*** 0. $( date +%_H ):00 Uhr, warte bis ${rebootDelay}:00 Uhr" &>> ${LogFile}
fi
fi
if [ -f ${doShutdown} ] ; then
# if time houre exists (file bigger than 0 Byte)
if [ -s ${doShutdown} ] ; then
rebootDelay=$(( $( cat ${doShutdown} ) ))
echo -e "\t\t*** 1. ShutdownDelay vorhanden nach ${rebootDelay}:00 Uhr" &>> ${LogFile}
else
rebootDelay=$(( $( date +%_H ) ))
echo -e "\t\t*** 1. ShutdownDelay nicht vorhanden = jetzt (${rebootDelay}:00 Uhr)" &>> ${LogFile}
fi
if [ $(( $( date +%_H ) )) -eq ${rebootDelay} ] ; then
# Check if NextDaily or NextWeekly or NextMonthly -> then wait
echo -e "\n\t\t*** 2. entferne doShutdown wieder" &>> ${LogFile}
rm ${doShutdown} &>> ${LogFile}
echo -e "\t\t*** 3. entferne stopBackup wieder" &>> ${LogFile}
rm ${stopBackup} &>> ${LogFile}
echo -en "\t\t*** 4. starte Shutdown:\t" &>> ${LogFile}
rtcwake -m off -t $(date -d "$(date +%F) ${wakeTime}" +%s) &>> ${LogFile}
else
echo -e "\t\t*** 0. $( date +%_H ):00 Uhr, warte bis ${rebootDelay}:00 Uhr" &>> ${LogFile}
fi
fi
fi
fi