From ccd2f6fcd915f00f094ce233e129852c708cd74e Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Wed, 13 Mar 2024 16:10:36 +0100
Subject: [PATCH] add selector fore restore

---
 localdump.sh | 48 +++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 39 insertions(+), 9 deletions(-)

diff --git a/localdump.sh b/localdump.sh
index 85c5878..dff4439 100755
--- a/localdump.sh
+++ b/localdump.sh
@@ -46,6 +46,7 @@
 
   LOCALDUMP_LOADED=1
 
+  ARCHIVE_BASEDIR=
   BACKUP_BASEDIR=
   BACKUP_PLUGINDIR=
 
@@ -183,7 +184,10 @@
   # restore: show profiles from that exist backups
   # global  string  BACKUP_BASEDIR  base directory of all backups
   function listBackupedServices(){
-    find "${BACKUP_BASEDIR}" -mindepth 1 -maxdepth 1 -type d | sed "s#^${BACKUP_BASEDIR}/##g" | sort | grep "^[a-z]"
+    (
+      find "${BACKUP_BASEDIR}" -mindepth 1 -maxdepth 1 -type d -exec basename {} \;
+      test -n "${ARCHIVE_BASEDIR}" && find "${ARCHIVE_BASEDIR}" -mindepth 1 -maxdepth 1 -type d -exec basename {} \;
+    ) | sort -u
   }
 
   # ------------------------------------------------------------
@@ -341,7 +345,7 @@
   fi
 
 
-  # ----- db init    
+  # ----- init vars
   BACKUP_BASEDIR=$(_j_getvar "${JOBFILE}" "dir-localdumps")
 
   # check
@@ -350,6 +354,8 @@
     echo There must be an entry dir-localdumps in ${JOBFILE}
     exit 1
   fi
+  ARCHIVE_BASEDIR=$(_j_getvar "${JOBFILE}" dir-dbarchive)
+
   BACKUP_PLUGINDIR=$(dirname $0)/plugins/localdump
   DBD_BASEDIR=$BACKUP_PLUGINDIR/profiles
 
@@ -411,7 +417,7 @@
           BACKUP_PARAMS=$( dbdetect.getParams )
 
           BACKUP_TARGETDIR=${BACKUP_BASEDIR}/${PROFILENAME}
-          ARCHIVE_DIR=$(_j_getvar "${JOBFILE}" dir-dbarchive)/${PROFILENAME}
+          ARCHIVE_DIR=${ARCHIVE_BASEDIR}/${PROFILENAME}
           BACKUP_SCRIPT=$( get_service_script ${SERVICENAME} )
   
 
@@ -456,10 +462,10 @@
 
       h1 "RESTORE DATABASE"
 
-          if ! listBackupedServices; then
-            color.echo error "ERROR: No database dump was found in [${BACKUP_BASEDIR}]."
-            exit 1
-          fi
+      if ! listBackupedServices | grep -q . ; then
+        color.echo error "ERROR: No database dump was found in [${BACKUP_BASEDIR}] nor [${ARCHIVE_BASEDIR}]."
+        exit 1
+      fi
 
       if [ -z $1 ] || [ ! -f "$1" ]; then
 
@@ -468,6 +474,14 @@
         # ----- interactive selections
 
         h2 "Select profile that has a dump"
+
+        if [ -z "${parService}" ]; then
+          if [ "$( listBackupedServices | wc -l )" -eq 1 ]; then
+            parService="$( listBackupedServices )"
+            echo "Selecting the only existing profile: $parService"
+          fi
+        fi
+
         if [ -z "${parService}" ]; then
           listBackupedServices
           color.print input "Restore for profile name >"
@@ -482,15 +496,30 @@
           color.echo error "ERROR: profile [${parService}] is not known here (or database service is stopped)."
           echo
           echo "Existing database profiles:"
-          get_database_profiles
+          get_database_profiles | nl
+          echo
           exit 1
         fi
+
+        # ----- check if dump exists in archive and in backup
+        if [ -d "${BACKUP_BASEDIR}/${parService}" ] && [ -d "${ARCHIVE_BASEDIR}/${parService}" ]; then
+          echo
+          echo "Backup   ${BACKUP_BASEDIR}"
+          echo "Archive  ${ARCHIVE_BASEDIR}"
+          color.print input "Select a source directory >"
+          read -r backupsource
+          test -z "$backupsource" && exit 1
+          BACKUP_BASEDIR="${backupsource}"
+        else
+          # just one test needed because BACKUP_BASEDIR is BACKUP_BASEDIR
+          test -d "${ARCHIVE_BASEDIR}/${parService}" && BACKUP_BASEDIR="${ARCHIVE_BASEDIR}/${parService}"
+        fi
+
         if [ ! -d "${BACKUP_BASEDIR}/${parService}" ]; then
           color.echo error "ERROR: profile ${parService} has no backup yet."
           exit 1
         fi
 
-
         BACKUP_TARGETDIR="${BACKUP_BASEDIR}/${parService}"
 
         h2 "Select database"
@@ -541,6 +570,7 @@
 
             BACKUP_PARAMS=$( dbdetect.getParams )
             eval $( dbdetect.setenv )
+            h2 $BACKUP_SCRIPT $mode "${sDumpfile}" "${sTargetDb}"
             . $BACKUP_SCRIPT $mode "${sDumpfile}" "${sTargetDb}"
 
             if [ $? -ne 0 -o $rc -ne 0 ]; then
-- 
GitLab