From d0cf34c1011768c097c2a3bc5416c297daa5471f Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Sun, 29 Oct 2023 18:24:31 +0000 Subject: [PATCH] Use flock correctly. Use $0 not $BASH_SOURCE. --- do-backup | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/do-backup b/do-backup index 4851024..171cec0 100755 --- a/do-backup +++ b/do-backup @@ -1,5 +1,5 @@ #!/bin/bash -# Version: 0.1.0 +# Version: 0.1.1 # Copyright (c) 2023: # Darren 'Tadgy' Austin # 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 }