From 7021b8e53ffe620c91571263f37a2f6dba84e64d Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Fri, 11 Feb 2022 14:47:27 +0100
Subject: [PATCH] shorten output if no service is available

---
 localdump.sh | 252 +++++++++++++++++++++++++++------------------------
 1 file changed, 133 insertions(+), 119 deletions(-)

diff --git a/localdump.sh b/localdump.sh
index 2d8be9d..ac46b9a 100755
--- a/localdump.sh
+++ b/localdump.sh
@@ -68,8 +68,9 @@
   # ------------------------------------------------------------
   # cleanup a backup dir: remove old files and delete empty dirs
   function cleanup_backup_target(){
-    h3 "CLEANUP ${BACKUP_TARGETDIR} older $BACKUP_KEEP_DAYS days ..."
     if [ -d "${BACKUP_TARGETDIR}" ]; then
+      h3 "CLEANUP ${BACKUP_TARGETDIR} older $BACKUP_KEEP_DAYS days ..."
+
       echo find "${BACKUP_TARGETDIR}" -mtime +$BACKUP_KEEP_DAYS -delete -print
       color cmd
       find "${BACKUP_TARGETDIR}" -mtime +$BACKUP_KEEP_DAYS -delete -print
@@ -79,8 +80,6 @@
         echo "INFO: the directory is empty - deleting it"
         rm -rf "${BACKUP_TARGETDIR}"
       fi
-    else
-      echo "SKIP: directory does not exist (no error)"
     fi
   }
 
@@ -137,9 +136,9 @@
   # show used space and count of files and dirs
   function show_info_backup_target(){
 
-    h3 "INFO about backup target ${BACKUP_TARGETDIR}"
-
     if [ -d "${BACKUP_TARGETDIR}" ]; then
+      h3 "INFO about backup target ${BACKUP_TARGETDIR}"
+
       echo -n "used space: "
       du -hs "${BACKUP_TARGETDIR}"
 
@@ -151,11 +150,8 @@
 
       echo -n "free space: "
       df -h "${BACKUP_TARGETDIR}" | tail -1 | awk '{ print $4 }'
-    else
-      echo "SKIP: directory does not exist (no error)"
+      echo
     fi
-    echo
-
   }
 
 # ----------------------------------------------------------------------
@@ -225,11 +221,15 @@
     echo "`basename $0` [[operation]] [Name_of_service] [[more services]]"
     echo "`basename $0` restore  [Name_of_service] [file-to-restore]"
     echo
-    echo "  operation       - one of backup|restore; optional parameter; default is backup"
+    echo "  operation       - one of check|backup|restore; optional parameter; default is backup"
+    echo "                      check    show info only if the service is available"
+    echo "                      backup   dump all databases/ schemes of a given service"
+    echo "                      restore  import a dump into same or new database"
+    echo "                               Without a filename it starts an interactive mode"
     echo "  Name_of_service - name of database service"
     echo "                    You get a list of all available services without parameter"
     echo "                    Use ALL for bulk command"
-    echo "  file            - filename of db dump to restore"
+    echo "  file            - filename of db dump to restore to origin database scheme"
   }
 
 
@@ -237,12 +237,12 @@
 # INIT
 # ----------------------------------------------------------------------
 
+  # ----- checks
+
   # . /usr/local/bin/inc_cronfunctions.sh
   j_requireUser "root"
 
   h1 `date` IML BACKUP :: LOCALDUMP :: $*
-  echo Start `date`
-  echo
 
   if [ $# -eq 0 ]; then
     color error
@@ -257,126 +257,140 @@
   fi
 
   mode="backup"
-  if [ "$1" = "backup" -o "$1" = "restore" ]; then
-    mode=$1
-    shift 1
-  fi
-
-
-  if [ "$mode" = "backup" ]; then
-
-    if [ "$1" = "ALL" ]; then
-       services=`get_services`
-       echo AUTO: calling local backup scripts for all known services
-       echo $services
-       echo
-    else
-       services=$*
-    fi
-
-    # ----- check all params
-    for SERVICENAME in $services
-    do
-      BACKUP_SCRIPT=$( get_service_script ${SERVICENAME} )
-      if [ ! -f $BACKUP_SCRIPT ]; then
-        color error
-        echo ERROR: parameter $SERVICENAME seems to be wrong.
-        echo The backup script does not exist: $BACKUP_SCRIPT
-        color reset
+  case "$1" in
+    backup|check|restore|shell)
+      mode=$1
+      shift 1
+      ;;
+  esac
+
+  export SERVICENAME=$1
+  BACKUP_TARGETDIR=${BACKUP_BASEDIR}/${SERVICENAME}
+  BACKUP_SCRIPT=$( get_service_script ${SERVICENAME} )
+
+  case "$mode" in
+    # ------------------------------------------------------------
+    check)
+      . $BACKUP_SCRIPT $mode
+      ;;
+    # ------------------------------------------------------------
+    backup)
+      if [ "$SERVICENAME" = "ALL" ]; then
+        services=$(get_services)
+        echo AUTO: calling local backup scripts for all known services
+        echo $services
         echo
-        echo services in this folder are:
-        get_services
-        exit 2
+      else
+        services=$*
       fi
-    done
-
-    # ----- GO
-    for SERVICENAME in $services
-    do
-
-      BACKUP_TARGETDIR=${BACKUP_BASEDIR}/${SERVICENAME}
-      BACKUP_SCRIPT=$( get_service_script ${SERVICENAME} )
-
-
-      # show_info_backup_target
-
-
-      # ----- start service specific script
-      echo
-      h2 "START SCRIPT FOR ${SERVICENAME} - $BACKUP_SCRIPT"
-
-      . $BACKUP_SCRIPT $mode
-
-      # ----- post jobs: cleanup
-      cleanup_backup_target
-      show_info_backup_target
 
-    done
-  fi
-
-  if [ "$mode" = "restore" ]; then
-    SERVICENAME=$1
-    BACKUP_TARGETDIR=${BACKUP_BASEDIR}/${SERVICENAME}
-    BACKUP_SCRIPT=$( get_service_script ${SERVICENAME} )
-
-    h1 "RESTORE DATABASE"
-    if [ -z $2 ]; then
-
-      # ----- file selection
-
-      h2 "select database"
-      listBackupedDBs
-      color input
-      echo -n "name of db to restore >"
-      color reset
-      read fileprefix
-      echo
+      # ----- check all params
+      for SERVICENAME in $services
+      do
+        BACKUP_SCRIPT=$( get_service_script ${SERVICENAME} )
+        if [ ! -f $BACKUP_SCRIPT ]; then
+          color error
+          echo ERROR: parameter $SERVICENAME seems to be wrong.
+          echo The backup script does not exist: $BACKUP_SCRIPT
+          color reset
+          echo
+          echo services in this folder are:
+          get_services
+          exit 2
+        fi
+      done
+
+      # ----- GO
+      for SERVICENAME in $services
+      do
+
+        BACKUP_TARGETDIR=${BACKUP_BASEDIR}/${SERVICENAME}
+        BACKUP_SCRIPT=$( get_service_script ${SERVICENAME} )
+
+        # ----- start service specific script
+        h2 "START SCRIPT FOR ${SERVICENAME} - $BACKUP_SCRIPT"
+
+        . $BACKUP_SCRIPT $mode
+
+        # ----- post jobs: cleanup
+        cleanup_backup_target
+        show_info_backup_target
+
+      done
+      ;;
+
+    # ------------------------------------------------------------
+    restore)
+
+      h1 "RESTORE DATABASE"
+      if [ -z $2 ]; then
+
+        # ----- file selection
+
+        h2 "select database"
+        listBackupedDBs
+        color input
+        echo -n "name of db to restore >"
+        color reset
+        read fileprefix
+        echo
 
-      h2 "select a specific dump for that database"
-      listBackupedDBs $fileprefix
-      color input
-      echo -n "backupset to import >"
-      color reset
-      read dbfile
-      echo
+        h2 "select a specific dump for that database"
+        listBackupedDBs $fileprefix
+        color input
+        echo -n "backupset to import >"
+        color reset
+        read dbfile
+        echo
 
-      color input
-      sTargetDb=`guessDB ${dbfile}`
-      echo -n "new database name [$sTargetDb] >"
-      color reset
-      read sTargetDb
-      if [ -z $sTargetDb ]; then
+        color input
         sTargetDb=`guessDB ${dbfile}`
+        echo -n "new database name [$sTargetDb] >"
+        color reset
+        read sTargetDb
+        if [ -z $sTargetDb ]; then
+          sTargetDb=`guessDB ${dbfile}`
+        fi
+        echo
+
+        sDumpfile="${BACKUP_TARGETDIR}/${dbfile}"
+      else
+        sDumpfile=$2
+        sTargetDb=$3
       fi
-      echo
+      shift 2
 
-      sDumpfile="${BACKUP_TARGETDIR}/${dbfile}"
-    else
-      sDumpfile=$2
-      sTargetDb=$3
-    fi
-    shift 2
-    if [ ! -f "${sDumpfile}" ]; then
-      color error
-      echo ERROR: ${sDumpfile} is not a file
-      color reset
-      rc=$rc+1
-    else
+      # ----- start restore
 
-      . $BACKUP_SCRIPT $mode "${sDumpfile}" "${sTargetDb}"
-      if [ $? -ne 0 -o $rc -ne 0 ]; then
+      if [ ! -f "${sDumpfile}" ]; then
         color error
-        echo ERROR: $mode failed. See ouput above. :-/
+        echo ERROR: ${sDumpfile} is not a file
         color reset
+        rc=$rc+1
       else
-        color ok
-        echo OK, $mode was successful.
-        color reset
-      fi
 
-    fi
-  fi
+        . $BACKUP_SCRIPT $mode "${sDumpfile}" "${sTargetDb}"
+        if [ $? -ne 0 -o $rc -ne 0 ]; then
+          color error
+          echo ERROR: $mode failed. See ouput above. :-/
+          color reset
+        else
+          color ok
+          echo OK, $mode was successful.
+          color reset
+        fi
 
+      fi
+      ;;
+    # ------------------------------------------------------------
+    # shell)
+    #   . $BACKUP_SCRIPT $mode
+    #   myshell=$( grep "^$USER" /etc/passwd | cut -f 7 -d ":" )
+    #   echo "TYPE EXIT ... to leave $myshell"
+    #   $myshell -i
+    #   echo "Subshell was closed."
+    #   ;;
+  esac
 
   echo _______________________________________________________________________________
 
-- 
GitLab