Add running flag to precent concurrent calls to the backup scripts

This commit is contained in:
Christian Wolf 2024-09-30 13:51:56 +02:00
parent ef68552243
commit 16c34607cb

View File

@ -5,10 +5,12 @@ repoFile="$(dirname "$0")/repos"
dryRun='' dryRun=''
preventSleep='' preventSleep=''
suspend='' suspend=''
removeRunning=''
varDir=/var/lib/bup-backup-trigger varDir=/var/lib/bup-backup-trigger
triggerFile="$varDir/trigger" triggerFile="$varDir/trigger"
lockFile="$varDir/lock" lockFile="$varDir/lock"
runningFile="$varDir/running"
log="/var/log/bup-sync" log="/var/log/bup-sync"
mkdir -p "$varDir" mkdir -p "$varDir"
@ -41,6 +43,9 @@ do
log="$2" log="$2"
shift shift
;; ;;
--rmove-running)
removeRunning=y
;;
*) *)
echo "Cannot understand parameter \"$1\". Aborting." >&2 echo "Cannot understand parameter \"$1\". Aborting." >&2
exit 1 exit 1
@ -54,6 +59,13 @@ then
exec >> "$log" exec >> "$log"
fi fi
if [ -n "$removeRunning" ]
then
echo "Removing running file"
rm -f "$runningFile"
exit
fi
# Do some safety checks # Do some safety checks
if [ ! -r "$repoFile" ] if [ ! -r "$repoFile" ]
@ -133,9 +145,19 @@ then
exit 0 exit 0
fi fi
if [ -f "$runningFile" ]
then
echo "The script is already running. Exiting"
echo "If this is not the case, delete the $runningFile file. Use the --remove-running CLI argument to the script."
exit 0
fi
echo "The backup script is triggered." echo "The backup script is triggered."
date date
echo "Creating $runningFile"
date > "$runningFile"
errored= errored=
while read line while read line
@ -143,11 +165,16 @@ do
echo "Processing line $line" echo "Processing line $line"
if [ -n "$dryRun" ] if [ -n "$dryRun" ]
then then
echo "Shipping to call script $line as in dry mode." echo "Skipping to actually call script $line. I am running in dry mode."
else else
tmp=$(mktemp) tmp=$(mktemp)
$line 2> "$tmp" && echo "Done" || { (
echo "Command failed: $line" $line 2>&1
ret=$?
echo "Done"
exit $ret
) > "$tmp" && echo "Command was succeddful." || {
echo "Command failed."
echo "Logs:" echo "Logs:"
cat "$tmp" cat "$tmp"
errored=y errored=y
@ -162,6 +189,7 @@ date
if [ -n "$errored" ] if [ -n "$errored" ]
then then
echo "An error had happened. Aborting" echo "An error had happened. Aborting"
rm -f "$runningFile"
exit 1 exit 1
fi fi
@ -171,6 +199,7 @@ then
echo "Sleep mode entry is skipped as well." echo "Sleep mode entry is skipped as well."
else else
updateTriggerFile updateTriggerFile
rm -f "$runningFile"
if [ -n "$preventSleep" ] if [ -n "$preventSleep" ]
then then