Use flock correctly. Use $0 not $BASH_SOURCE.

This commit is contained in:
Darren 'Tadgy' Austin 2023-10-29 18:24:31 +00:00
commit d0cf34c101

View file

@ -1,5 +1,5 @@
#!/bin/bash
# Version: 0.1.0
# Version: 0.1.1
# Copyright (c) 2023:
# Darren 'Tadgy' Austin <darren (at) afterdark.org.uk>
# Licensed under the terms of the GNU General Public License version 3.
@ -10,11 +10,11 @@
BACKUP_MOUNTPOINT="/localdata"
RSYNC_OPTIONS=( '-a' '-H' '-A' '--timeout=300' '--partial' '--partial-dir=.rsync-tmp' '--delete-delay' '--delay-updates' )
RSYNC_OPTIONS_VERBOSE=( '--verbose' '--stats' '--human-readable' )
RSYNC_LOG="/tmp/${BASH_SOURCE[0]##*/}-$$.log"
RSYNC_LOG="/tmp/${0##*/}-$$.log"
# Sanity checks.
(( $# != 1 )) || [[ -z "$1" ]] || [[ "$1" =~ ^(-h|-help|--help)$ ]] && {
printf "%s: %s <%s>\\n" "Usage" "${BASH_SOURCE[0]}" "backup definition" >&2
printf "%s: %s <%s>\\n" "Usage" "$0" "backup definition" >&2
exit 1
}
@ -52,7 +52,7 @@ case "$1" in
RSYNC_OPTIONS+=( '--contimeout=30' )
;;
*)
printf "%s: %s: %s\\n" "${BASH_SOURCE[0]##*/}" "$1" "unknown backup definition" >&2
printf "%s: %s: %s\\n" "${0##*/}" "$1" "unknown backup definition" >&2
exit 1
;;
esac
@ -60,15 +60,21 @@ esac
# Only allow one copy of the script to run at any time.
# shellcheck disable=SC2154
if [[ "$FLOCK" != "$0" ]]; then
exec env FLOCK="$0" flock -e -n "$0" "$0" "$@" || {
printf "%s: %s\\n" "${BASH_SOURCE[0]##*/}" "flock execution error" >&2
# shellcheck disable=SC2093
exec env FLOCK="$0" flock -E 10 -e -n "$0" "$0" "$@"
ERR="$?"
if (( ERR == 10 )); then
# File is locked, exit now.
exit 0
elif (( ERR > 0 )); then
printf "%s: %s\\n" "${0##*/}" "flock execution error" >&2
exit 1
}
fi
fi
# Source the mail configuration.
source /etc/mail.conf "backups" 2>/dev/null || {
printf "%s: %s\\n" "${BASH_SOURCE[0]##*/}" "Failed to source /etc/mail.conf" >&2
printf "%s: %s\\n" "${0##*/}" "Failed to source /etc/mail.conf" >&2
exit 1
}