From b765763cd75dd1f0dd571b25e7062983e27f744f Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Fri, 29 Mar 2019 15:25:38 -0300 Subject: Lockfile implementation --- borger | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/borger b/borger index 4992bcf..a481593 100755 --- a/borger +++ b/borger @@ -35,11 +35,14 @@ function borger_usage { exit 1 } -# Process configuration -function borger_config { +# Check if in multiple mode +function borger_check_multiple { if [ ! -e "$CONFIG" ]; then fatal "No such config $CONFIG" + exit 1 elif [ -d "$CONFIG" ]; then + MULTIPLE="yes" + info "Multiple destination \"$DESTINATION\" found. Processing each subconfig..." # Config is a folder, so we iterate over all items @@ -54,6 +57,14 @@ function borger_config { #exit wait fi +} + +# Process configuration +function borger_config { + # If running on multiple mode, stop here + if [ "$MULTIPLE" == "yes" ]; then + exit + fi # Ensure we have an username if [ -z "$USER" ]; then @@ -82,6 +93,9 @@ function borger_config { export BORG_REPO_DIR="/var/backups/users/$USER/borg" export BORG_REPO="ssh://$SSH_SERVER:$SSH_PORT/$BORG_REPO_DIR" fi + + # Lockfile location + LOCKFILE="$TMP/$BASENAME/$DESTINATION.lock" } # List @@ -181,9 +195,49 @@ function borger_prune { #fi } +# Create lockfile +function borger_set_lockfile { + if [ ! -z "$LOCKFILE" ]; then + mkdir -p `dirname $LOCKFILE` + if ( set -o noclobber; echo "$$" > "$LOCKFILE" ) &> /dev/null; then + trap 'borger_unset_lockfile' INT TERM EXIT + else + echo "Could not create lockfile $LOCKFILE, exiting" + exit 1 + fi + fi +} + +# Remove lockfile +function borger_unset_lockfile { + if [ ! -z "$LOCKFILE" ]; then + rm -f $LOCKFILE || echo "Could not remove lockfile $LOCKFILE" + fi +} + +# Check lockfile +function borger_check_lockfile { + local pid process + + if [ ! -z "$LOCKFILE" ] && [ -f "$LOCKFILE" ]; then + pid="`cat $LOCKFILE`" + process="`ps --no-headers -o comm $pid`" + if [ "$?" == "0" ] && [ "`ps --no-headers -o comm $$`" == "$process" ]; then + echo "Another program is running for $LOCKFILE, skipping run" + exit + else + echo "Found old lockfile $LOCKFILE, removing it" + borger_unset_lockfile + fi + fi +} + # Main backup procedure function borger_run { + borger_check_multiple borger_config + borger_check_lockfile + borger_set_lockfile borger_trap borger_init borger_create -- cgit v1.2.3