Browse Source

Fix stderr capture and check return code while running pg_dump/pg_dumpall commands

Emmanuel Bouthenot 1 year ago
parent
commit
0b3081df05
2 changed files with 12 additions and 7 deletions
  1. 2 1
      Changelog.md
  2. 10 6
      autopostgresqlbackup

+ 2 - 1
Changelog.md

@@ -2,7 +2,8 @@
 
 ## Version 2.1
 
-Variable `OPT` (used with pg_dump) is renamed to `PGDUMP_OPTS` and a new variable `PGDUMPALL_OPTS` is available (used for dump globals with pg_dumpall) (Closes: [#12](https://github.com/k0lter/autopostgresqlbackup/issues/12))
+* Variable `OPT` (used with pg_dump) is renamed to `PGDUMP_OPTS` and a new variable `PGDUMPALL_OPTS` is available (used for dump globals with pg_dumpall) (Closes: [#12](https://github.com/k0lter/autopostgresqlbackup/issues/12))
+* Fix stderr capture and check return code while running pg_dump/pg_dumpall commands
 
 ## Version 2.0
 

+ 10 - 6
autopostgresqlbackup

@@ -301,9 +301,11 @@ dblist () {
     log_debug "Running command: ${cmd_prog} ${cmd_args[*]}"
     raw_dblist=$(
         if [ -n "${SU_USERNAME}" ]; then
-            su - "${SU_USERNAME}" -l -c "${cmd_prog} ${cmd_args[*]}"
-        else
-            "${cmd_prog}" "${cmd_args[@]}"
+            if ! su - "${SU_USERNAME}" -c "${cmd_prog} ${cmd_args[*]}" 2> >(logger "err" "error"); then
+                log_error "Running (as user '${SU_USERNAME}' command '${cmd_prog} ${cmd_args[*]}' has failed"
+            fi
+        elif ! "${cmd_prog}" "${cmd_args[@]}" 2> >(logger "err" "error"); then
+            log_error "Running command '${cmd_prog} ${cmd_args[*]}' has failed"
         fi
     )
 
@@ -362,9 +364,11 @@ dbdump () {
 
     log_debug "Running command: ${cmd_prog} ${cmd_args[*]}"
     if [ -n "${SU_USERNAME}" ]; then
-        su - "${SU_USERNAME}" -l -c "${cmd_prog} ${cmd_args[*]}"
-    else
-        "${cmd_prog}" "${cmd_args[@]}"
+        if ! su - "${SU_USERNAME}" -c "${cmd_prog} ${cmd_args[*]}" 2> >(logger "err" "error"); then
+            log_error "Running (as user '${SU_USERNAME}' command '${cmd_prog} ${cmd_args[*]}' has failed"
+        fi
+    elif ! "${cmd_prog}" "${cmd_args[@]}" 2> >(logger "err" "error"); then
+        log_error "Running command '${cmd_prog} ${cmd_args[*]}' has failed"
     fi
 }
 # }}}