]> gitweb.fluxo.info Git - borger.git/commitdiff
Lockfile implementation
authorSilvio Rhatto <rhatto@riseup.net>
Fri, 29 Mar 2019 18:25:38 +0000 (15:25 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Fri, 29 Mar 2019 18:25:38 +0000 (15:25 -0300)
borger

diff --git a/borger b/borger
index 4992bcf6c36b6adf5dc6e0a48087b2c368e7038b..a481593d4265d199901043f58a513ee0d1f84670 100755 (executable)
--- 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