From 077f1f1201dde4af2a604715d03b9737fe1b4bfb Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Thu, 1 May 2025 15:04:01 +0200
Subject: [PATCH] suport backup of single database or given databases

---
 localdump.sh               | 33 ++++++++++++++++++++++++---------
 plugins/localdump/pgsql.sh | 19 ++++++++++++++++---
 2 files changed, 40 insertions(+), 12 deletions(-)

diff --git a/localdump.sh b/localdump.sh
index ca336a6..232d891 100755
--- a/localdump.sh
+++ b/localdump.sh
@@ -20,6 +20,7 @@
 # 2024-03-18  ah     fix for db detection from file and cli restore
 # 2024-10-02  ah     reset $rc before calling db plugin
 # 2024-12-13  ah     set gzip compression from 9 to 4
+# 2025-05-01  ah     suport backup of single database or given databases
 # ======================================================================
 
 # --- variables:
@@ -341,9 +342,10 @@ OPTIONS:
     -h|--help   show this help
 
 PARAMETERS:"
-    operation   - one of check|backup|restore; optional parameter
+    operation   - one of backup|check|restore; optional parameter
                       backup   dump all databases/ schemes of a given service
                       check    show info only if the service is available
+                      check    show info only if the service is available
                       restore  import a dump into same or new database
                                Without a filename it starts an interactive mode
     profile     - name of database profiles
@@ -354,17 +356,23 @@ PARAMETERS:"
 EXAMPLES:
     $_self backup
     $_self backup ALL
-                 Backup all databases of all found services
+                Backup all databases of all found services
     $_self backup mysql
-                 Backup all Mysql databases.
+                Backup all Mysql databases.
+
+    $_self backup mysql wordpress
+                Backup all Mysql database "wordpress" only
+
+    $_self check
+                Show debug info of profile detection for avalable databases
 
     $_self restore
-                 Start interactive restore of a database of any service.
+                Start interactive restore of a database of any service.
     $_self restore sqlite
-                 Start interactive restore of an sqlite database.
+                Start interactive restore of an sqlite database.
     $_self restore <file-to-restore> [<database-name>]
-                 Restore a given dump file to the origin database scheme or
-                 to a new/ other database with the given name.
+                Restore a given dump file to the origin database scheme or
+                to a new/ other database with the given name.
 
 EOH
     }
@@ -494,8 +502,10 @@ EOH
                 h3 "BACKUP [${PROFILENAME}] -> ${SERVICENAME}"
                 rcbak=$rc
                 rc=0
-                . $BACKUP_SCRIPT $mode
-  
+
+                databaselist="$( sed "s#^$SERVICENAME##" <<< $*)"
+                . $BACKUP_SCRIPT $mode $databaselist
+
                 test $rc -gt 0 && j_notify "db ${SERVICENAME}" "$BACKUP_SCRIPT $mode was finished with rc=$rc" $rc
                 _j_runHooks "230-after-db-service" "$rc"
   
@@ -507,6 +517,11 @@ EOH
                 show_info_backup_target
 
                 rc=$rc+$rcbak
+
+                if grep -q "." <<< "$databaselist"; then
+                    echo "Stopping here after handling $SERVICENAME and do not process other profiles." 
+                    break
+                fi
     
             else
   
diff --git a/plugins/localdump/pgsql.sh b/plugins/localdump/pgsql.sh
index b3ce015..3a79779 100755
--- a/plugins/localdump/pgsql.sh
+++ b/plugins/localdump/pgsql.sh
@@ -18,6 +18,7 @@
 # 2024-12-16  ah     v1.7  on snapshot mode it can fallback for single database to normal pg_dump of origin
 # 2024-12-17  ah     v1.8  handle env variables to disable snapshots or set times for disconnect
 # 2025-02-03  ah     v1.9  pg_dump in temp directory; dump file will be moved to backup dir
+# 2025-05-01  ah     v1.10 suport backup of single database or given databases
 # ================================================================================
 
 if [ -z "$BACKUP_TARGETDIR" ]; then
@@ -38,6 +39,12 @@ fi
 # FUNCTION
 # --------------------------------------------------------------------------------
 
+# get a list of existing databases
+function pgsql.list(){
+  sSqlGetDblist="select datname from pg_database where not datistemplate and datallowconn order by datname;"
+  su ${PGUSER} -c "psql ${BACKUP_PARAMS} -At -c '$sSqlGetDblist' postgres" 2>/dev/null | grep -v "^$snapshotprefix"
+}
+
 # backup all databases
 function pgsql.backup(){
 
@@ -77,7 +84,7 @@ function pgsql.backup(){
     bUseSnapshot=0
   else
     # Detect a readonly or writeable postgres host.
-    TESTDB="imlbackup_createtest"
+    TESTDB="imlbackup_createtest_${snapshotprefix}"
     su ${PGUSER} -c "dropdb ${TESTDB}" >/dev/null 2>&1
     if su ${PGUSER} -c "psql ${BACKUP_PARAMS} -c \"CREATE DATABASE ${TESTDB};\"" >/dev/null 2>&1
     then
@@ -99,8 +106,14 @@ function pgsql.backup(){
     fi
   fi
 
-  sSqlGetDblist="select datname from pg_database where not datistemplate and datallowconn order by datname;"
-  for DATABASE in $( su ${PGUSER} -c "psql ${BACKUP_PARAMS} -At -c '$sSqlGetDblist' postgres" 2>/dev/null | grep -v "^$snapshotprefix" )
+  sDBList="$*"
+  if [ -z "$sDBList" ]; then 
+    sDBList="$( pgsql.list )"
+    echo "INFO: found databases:" 
+    sed "s#^#      - #g" <<< "$sDBList"
+  fi
+
+  for DATABASE in $sDBList
   do
     echo -n "__DB__${SERVICENAME} backup $DATABASE ... "
 
-- 
GitLab