Package:     backup2l
Subject:     add --remove-old in remote
Author:      Gundolf Kiefer <gkiefer@users.noreply.github.com>
Origin:      upstream, https://github.com/gkiefer/backup2l/commit/496f0124ac201a2f43040368de8b4cbf8074addb
Forwarded:   not needed
Last-Update: 2024-02-20

Add '--remove-old' operation to remove old backups in remote locations.
---

diff -urN s22/backup2l s23/backup2l
--- s22/backup2l	2025-04-25 16:06:02.793559086 +0200
+++ s23/backup2l	2025-04-25 16:11:50.608093083 +0200
@@ -344,7 +344,7 @@
             exit 3
         fi
     fi
-    echo $$ > $BACKUP_DIR/$VOLNAME.lock
+    echo $$ > $BACKUP_DIR/$VOLNAME.lock 2>/dev/null
     if [[ "`cat $BACKUP_DIR/$VOLNAME.lock 2>/dev/null`" != "$$" ]]; then
       echo "ERROR: Failed to lock backup volume."
       echo
@@ -618,6 +618,9 @@
 
 compute_level_and_bids ()
 {
+    # Input: Selected level (optional)
+    # Output: $LEVEL, $BASE_BID, $NEW_BID
+
     # Determine level and base BID for new backup...
     if [ ! -f $VOLNAME.1.list.gz ]; then
         LEVEL="0";
@@ -729,26 +732,19 @@
 }
 
 
-do_backup ()
+remove_old_backups ()
 {
-    require_tools $COMMON_TOOLS
-    require_drivers $CREATE_DRIVER
-
-    # Print time stamp & mount backup drive...
-    date
-    echo
-    mount_dev
+    # Input: Selected level (optional)
+    # Output: $LEVEL, $BASE_BID, $NEW_BID
 
-    # Run pre-backup
-    echo "Running pre-backup procedure..."
-    PRE_BACKUP
+    require_tools $COMMON_TOOLS
 
     # Operate in destination directory
+    mount_dev
     cd $BACKUP_DIR
     rm -fr $TMP.*
 
     # Remove old backups...
-    echo
     echo "Removing old backups..."
     name_cleanup  # should not do anything in normal cases
     compute_level_and_bids $1
@@ -786,6 +782,30 @@
             done
         done
     fi
+}
+
+
+do_backup ()
+{
+    require_tools $COMMON_TOOLS
+    require_drivers $CREATE_DRIVER
+
+    # Print time stamp & mount backup drive...
+    date
+    echo
+    mount_dev
+
+    # Run pre-backup
+    echo "Running pre-backup procedure..."
+    PRE_BACKUP
+
+    # Operate in destination directory
+    cd $BACKUP_DIR
+    rm -fr $TMP.*
+
+    # Remove old backups...
+    echo
+    remove_old_backups $1
 
     # Prepare backup...
     echo
@@ -1257,7 +1277,8 @@
 
   <command>:
     -h | --help                    : Help
-    -b | --backup [<level>]        : Create new backup
+
+    -b | --backup [<level>]        : Create new backup (implies --remove-old)
     -e | --estimate [<level>]      : Like -b, but nothing is really done
     -s | --get-summary             : Show backup summary
 
@@ -1266,6 +1287,7 @@
     -r | --restore [<pattern>]     : Restore active files matching <pattern> into current directory
 
     -p | --purge <BID-list>        : Remove the specified backup archive(s) and all depending backups
+    -o | --remove-old [<level>]    : Remove backups older than specified by GENERATIONS
 
     -v | --verify [<BID-list>]     : Verify the specified / all backup archive(s)
     -m | --make-check [<BID-list>] : Create md5 checksum file for the specified archive(s) / wherever missing
@@ -1365,14 +1387,14 @@
 
 # Go ahead...
 case $1 in
-    -e | --estimate)
-        banner
-        show_backup_estimates "$2"
-        ;;
     -b | --backup)
         banner
         do_backup "$2"
         ;;
+    -e | --estimate)
+        banner
+        show_backup_estimates "$2"
+        ;;
     -s | --get-summary)
         banner
         mount_dev
@@ -1398,6 +1420,11 @@
         shift
         do_purge "$@"
         ;;
+    -o | --remove-old)
+        banner
+        shift
+        remove_old_backups "$@"
+        ;;
     -v | --verify)
         banner
         shift
