From f34e8591817f2a011bfd12ebc8f5defeb4600ed6 Mon Sep 17 00:00:00 2001 From: ub1x Date: Sun, 30 Jan 2022 14:01:37 +0100 Subject: [PATCH] CheckBackup added and readme.md updated --- CheckBackup.sh | 171 +++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 1 + 2 files changed, 172 insertions(+) create mode 100755 CheckBackup.sh diff --git a/CheckBackup.sh b/CheckBackup.sh new file mode 100755 index 0000000..cc4bac3 --- /dev/null +++ b/CheckBackup.sh @@ -0,0 +1,171 @@ +#!/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 diff --git a/README.md b/README.md index 5aea551..e86e847 100644 --- a/README.md +++ b/README.md @@ -20,3 +20,4 @@ See also at [LUG-VS-Wiki Nützliche Scripte](https://lug-vs.org//lugvswiki/index |[utf8.sh](utf8.sh)|Convert text from e.g. Windows latin1 to utf8 text (e.g. in READMEs or other text (not for office documents))| |[wmf2svg.sh](wmf2svg.sh)|Example script converts WMF files in SVG to share it easily on the internet| |[write-exif-comment.sh](write-exif-comment.sh)|Write JPEG EXIF comment in all *.jpg files| +|[CheckBackup.sh](CheckBackup.sh)|Check if the anacron tasks and give the status of the next backup|