From 0d715cf595d0f687c164a19c4ea204aaeccf271d Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Fri, 8 Mar 2024 11:44:09 +0100
Subject: [PATCH] update localdump

---
 localdump.sh | 260 ++++++++++++---------------------------------------
 1 file changed, 58 insertions(+), 202 deletions(-)

diff --git a/localdump.sh b/localdump.sh
index 09691c3..d5e9334 100755
--- a/localdump.sh
+++ b/localdump.sh
@@ -35,6 +35,8 @@
   . $(dirname $0)/jobhelper.sh
   . $(dirname $0)/inc_bash.sh
   . $(dirname $0)/vendor/ini.class.sh
+  . $(dirname $0)/includes/dbdetect.class.sh
+
   # if [ -r ~/.backup.conf ]; then
   #   . ~/.backup.conf
   # fi
@@ -51,8 +53,6 @@
   BACKUP_BASEDIR=
   BACKUP_PLUGINDIR=
 
-  DBD_BASEDIR=plugins/localdump/profiles
-  declare -A DBD_PARAMS
   DBD_DEBUG=0
 
   # Cleanup local dumps older N days
@@ -60,175 +60,6 @@
 
   BACKUP_DATE=
 
-# --------------------------------------------------------------------
-#
-# --------------------------------------------------------------------
-
-# ---------- GETTER
-
-# write debug output if DBD_DEBUG is enabled
-# param  string  text to show
-function dbdetect._wd(){
-    test "$DBD_DEBUG" -eq "1" && echo "DEBUG: $*" >&2
-}
-
-# get a list of all config files
-# param  string  full path of ini file
-function dbdetect.getConfigs(){
-    find ${DBD_BASEDIR} -type f -name "*.ini"
-}
-
-# get type from given ini file
-# param  string  full path of ini file
-function dbdetect.getType(){
-    basename "$1" | cut -d "_" -f 1 | sed "s,\.ini$,,"
-}
-
-# get profile from given ini file
-# param  string  full path of ini file
-function dbdetect.getProfile(){
-    basename "$1" | cut -d "_" -f 1- | sed "s,\.ini$,,"
-}
-
-# check if the requirements for a database match
-# param  string  full path of ini file to check
-function dbdetect.exists(){
-    local _config="$1"
-
-    DBD_PARAMS=
-
-    local _found=0
-
-    # set file and inisection we read values from
-    ini.set "$_config" "detect"
-    
-    # --- check tcp
-    local tcpport; tcpport=$( ini.value "tcp" )
-    if [ -n "$tcpport" ]; then
-      local tcptarget; tcptarget=$( ini.value "tcp-target" )
-      tcptarget=${tcptarget:-localhost}
-      if { ! >/dev/tcp/$tcptarget/$tcpport; } > /dev/null 2>&1; then
-          # echo "No port tcp $tcpport available"
-          dbdetect._wd "... No port tcp $tcpport available on $tcptarget"
-          return 1
-      fi
-      dbdetect._wd "... Found tcp $tcpport on $tcptarget."
-    fi
-
-    # --- check tcp process
-    local tcpprocess; tcpprocess=$( ini.value "tcp-process" )
-    if [ -n "$tcpprocess" ]; then
-        if ! netstat -tulpen 2>/dev/null | grep "^tcp.*:${tcpport} .*/${tcpprocess}" >/dev/null; then
-            # echo "No port tcp $tcpport available"
-            dbdetect._wd "... $tcpprocess not found for tcp ${tcpport}"
-            return 1
-        fi
-        dbdetect._wd "... tcp $tcpport is used by $tcpprocess."
-    fi
-
-    # --- check binaries
-    local binary; binary=$( ini.value "binary" )
-    if [ -n "${binary}" ]; then
-        for mybinary in $( echo "${binary}" | tr "," " " ); do
-            if ! j_requireBinary "$mybinary" 1 >/dev/null 2>&1; then
-                dbdetect._wd "... Missing binary: ${mybinary}"
-                return 1
-            fi
-            dbdetect._wd "... Binary: ${mybinary} was found"
-        done
-    fi
-
-    # --- check process
-    local process; process=$( ini.value "process" )
-    if [ -n "${process}" ]; then
-        if ! j_requireProcess "${process}" 1 >/dev/null; then
-            dbdetect._wd "... Missing process: ${process}"
-            return 1
-        fi
-        # if ! ps -eo command | grep -E "${process}" >/dev/null 2>&1; then
-        #     dbdetect._wd "... Process ${process} was not found"
-        #     return 1
-        # fi
-        dbdetect._wd "... Process ${process} was found"
-    fi
-
-    # --- check db files
-    local filetype; filetype=$( ini.value "type" )
-
-    if [ -n "${filetype}" ]; then
-      local myfiles; declare -a myfiles
-      for myfile in $( ini.value "file[]" )
-      do
-        if ! file -b "${myfile}" | grep -i "$filetype" >/dev/null; then
-          dbdetect._wd "... File ${myfile} is no type $filetype"
-          return 1
-        fi
-        dbdetect._wd "... File ${myfile} is type $filetype"
-        myfiles+="${myfile}|"
-      done
-    fi
-
-    # --- OK, everything was found ... we initialize it
-    dbdetect._wd "OK, match: $_config"
-
-    ini.set "$_config" "set"
-    local value
-    local dbuser=$( ini.value "dbuser" )
-    local dbpassword=$( ini.value "dbpassword" )
-
-
-    for mykey in su env params
-    do
-        value="$( ini.value "$mykey" )"
-        value="${value//\{\{tcp\}\}/$tcpport}"
-        value="${value//\{\{tcp-target\}\}/$tcptarget}"
-        value="${value//\{\{dbuser\}\}/$dbuser}"
-        value="${value//\{\{dbpassword\}\}/$dbpassword}"
-        value="${value//\{\{file\[\]\}\}/$myfiles}"
-        
-        DBD_PARAMS[$mykey]="$value"
-        dbdetect._wd ">>> $mykey = $value"
-    done
-    dbdetect._wd ">>> files = $myfiles"
-    DBD_PARAMS[files]="$myfiles"
-
-    return 0
-}
-
-# set a profile name
-function dbdetect.setProfile(){
-  dbdetect.exists "${DBD_BASEDIR}/${PROFILENAME}.ini"
-}
-
-function dbdetect.getParams(){
-    echo "${DBD_PARAMS['params']}"
-}
-
-
-# for backup scripts: get checked files from [detect] -> file[]
-# 
-function dbdetect.getFiles(){
-    echo "${DBD_PARAMS['files']}" | tr "|" "\n"
-}
-
-# set variables in [set] -> env = ...
-# USAGE:
-# eval $( dbdetect.setenv )
-function dbdetect.setenv(){
-    echo "${DBD_PARAMS['env']}"
-}
-
-# unset variables from [set] -> env = ...
-# USAGE:
-# eval $( dbdetect.unsetenv )
-function dbdetect.unssetenv(){
-    echo "${DBD_PARAMS['env']}" | grep -o '[a-z0-9]*=' | tr -d '=' | sed "s,^,unset ,"
-}
-
-function dbdetect.runas(){
-    echo "${DBD_PARAMS['su']}"
-}
-
 # ----------------------------------------------------------------------
 # FUNCTIONS 4 DB-WRAPPER
 # ----------------------------------------------------------------------
@@ -444,21 +275,33 @@ function dbdetect.runas(){
   function showhelp(){
     local _self
     _self=$( basename "$0" )
-    echo "SYNTAX: "
-    echo "$_self [[operation]] [Name_of_service] [[more services]]"
     echo
+    echo "LOCALDUMP detects existing local databases and dumps them locally."
+    echo "It is included in the backup.sh to dump all before a file backup will store them."
+    echo
+    echo "It can be started seperately for manual database backups or for restore"
+    echo
+    echo "SYNTAX:"
+    echo "  $_self [OPTIONS] <operation> <profile [more_profiles]>"
+    echo
+    echo "OPTIONS:"
+    echo "  -h|--help   show this help"
+    echo
+    echo "PARAMETERS:"
+    echo "  operation     - one of check|backup|restore; optional parameter; default is"
+    echo "                  'backup'."
+    echo "                    backup   dump all databases/ schemes of a given service"
+    echo "                    check    show info only if the service is available"
+    echo "                    restore  import a dump into same or new database"
+    echo "                             Without a filename it starts an interactive mode"
+    echo "  profile     - name of database profiles"
+    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 to origin database scheme"
+    echo
+    echo "EXAMPLES:"
     echo "$_self backup  [Name_of_service]"
     echo "$_self restore [Name_of_service] [[file-to-restore]]"
-    echo
-    echo "  operation       - one of check|backup|restore; optional parameter; default is backup"
-    echo "                      check    WIP: 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 to origin database scheme"
   }
 
 
@@ -466,6 +309,37 @@ function dbdetect.runas(){
 # INIT
 # ----------------------------------------------------------------------
 
+  while [[ "$#" -gt 0 ]]; do case $1 in
+      -h|--help)      showhelp; exit 0;;
+      *) if grep "^-" <<< "$1" >/dev/null ; then
+          echo; echo "ERROR: Unknown parameter: $1"; echo; showhelp; exit 2
+        fi
+        break;
+        ;;
+  esac; done
+
+  mode="backup"
+  case "$1" in
+    backup|check|restore|shell)
+      mode=$1
+      shift 1
+      ;;
+  esac
+
+  if [ -z "$mode" ]; then
+    color error
+    echo "ERROR: missing parameter."
+    color reset
+    echo
+    showhelp
+    echo
+    echo "On this machine working profiles (see ${BACKUP_PLUGINDIR}):"
+    get_services | sed "s#^#  #g"
+    echo
+    exit 1
+  fi
+
+
   # ----- db init    
   BACKUP_BASEDIR=$(_j_getvar ${JOBFILE} "dir-localdumps")
 
@@ -494,25 +368,7 @@ function dbdetect.runas(){
 
   h1 $(date) IML BACKUP :: LOCALDUMP :: $*
 
-  if [ $# -eq 0 ]; then
-    color error
-    echo "ERROR: missing parameter."
-    color reset
-    echo
-    showhelp
-    echo
-    echo "Known services (see ${BACKUP_PLUGINDIR}):"
-    get_services
-    exit 1
-  fi
-
-  mode="backup"
-  case "$1" in
-    backup|check|restore|shell)
-      mode=$1
-      shift 1
-      ;;
-  esac
+  echo "mode = $mode"
 
   export SERVICENAME=$1
   BACKUP_TARGETDIR=${BACKUP_BASEDIR}/${SERVICENAME}
-- 
GitLab