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=''
preventSleep=''
suspend=''
removeRunning=''
varDir=/var/lib/bup-backup-trigger
triggerFile="$varDir/trigger"
lockFile="$varDir/lock"
runningFile="$varDir/running"
log="/var/log/bup-sync"
mkdir -p "$varDir"
@ -41,6 +43,9 @@ do
log="$2"
shift
;;
--rmove-running)
removeRunning=y
;;
*)
echo "Cannot understand parameter \"$1\". Aborting." >&2
exit 1
@ -54,6 +59,13 @@ then
exec >> "$log"
fi
if [ -n "$removeRunning" ]
then
echo "Removing running file"
rm -f "$runningFile"
exit
fi
# Do some safety checks
if [ ! -r "$repoFile" ]
@ -133,9 +145,19 @@ then
exit 0
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."
date
echo "Creating $runningFile"
date > "$runningFile"
errored=
while read line
@ -143,11 +165,16 @@ do
echo "Processing line $line"
if [ -n "$dryRun" ]
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
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:"
cat "$tmp"
errored=y
@ -162,6 +189,7 @@ date
if [ -n "$errored" ]
then
echo "An error had happened. Aborting"
rm -f "$runningFile"
exit 1
fi
@ -171,6 +199,7 @@ then
echo "Sleep mode entry is skipped as well."
else
updateTriggerFile
rm -f "$runningFile"
if [ -n "$preventSleep" ]
then