3 Commits e293415ac7 ... eedfa5d1f0

Author SHA1 Message Date
  Emmanuel Bouthenot eedfa5d1f0 Add MIN_DUMP_SIZE configuration variable to raise a warning when a backup file size is below this limit 1 year ago
  Emmanuel Bouthenot 0b3081df05 Fix stderr capture and check return code while running pg_dump/pg_dumpall commands 1 year ago
  Emmanuel Bouthenot c7e019ff51 Add separate configuration settings for pg_dump and pg_dumpall 1 year ago
3 changed files with 53 additions and 13 deletions
  1. 6 0
      Changelog.md
  2. 13 1
      Documentation.md
  3. 34 12
      autopostgresqlbackup

+ 6 - 0
Changelog.md

@@ -1,5 +1,11 @@
 # Changelog
 
+## 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))
+* Fix stderr capture and check return code while running pg_dump/pg_dumpall commands
+* Add `MIN_DUMP_SIZE` configuration variable to raise a warning when a backup file size is below this limit (Closes: [#15](https://github.com/k0lter/autopostgresqlbackup/issues/15))
+
 ## Version 2.0
 
 * Huge code cleanup and refactoring (Closes: [#2](https://github.com/k0lter/autopostgresqlbackup/issues/2))

+ 13 - 1
Documentation.md

@@ -128,12 +128,18 @@ COMP="zstd"
 COMP_OPTS="-f -c"
 ```
 
-### `OPT`
+### `PGDUMP_OPTS`
 
 Options string for use with pg_dump (see [pg_dump](https://www.postgresql.org/docs/current/app-pgdump.html) manual page).
 
 **default**: `""` (empty)
 
+### `PGDUMPALL_OPTS`
+
+Options string for use with pg_dumpall (see [pg_dumpall](https://www.postgresql.org/docs/current/app-pg-dumpall.html) manual page).
+
+**default**: `""` (empty)
+
 ### `EXT`
 
 Backup files extension
@@ -146,6 +152,12 @@ Backup files permission
 
 **default**: `600`
 
+### `MIN_DUMP_SIZE`
+
+Minimum size (in bytes) for a dump/file (compressed or not). File size below this limit will raise an warning.
+
+**default**: `256`
+
 ### `ENCRYPTION`
 
 Enable encryption (asymmetric) with GnuPG.

+ 34 - 12
autopostgresqlbackup

@@ -104,7 +104,10 @@ COMP="gzip"
 COMP_OPTS=
 
 # Options string for use with pg_dump (see pg_dump manual page).
-OPT=
+PGDUMP_OPTS=
+
+# Options string for use with pg_dumpall (see pg_dumpall manual page).
+PGDUMPALL_OPTS=
 
 # Backup files extension
 EXT="sql"
@@ -112,6 +115,10 @@ EXT="sql"
 # Backup files permission
 PERM=600
 
+# Minimum size (in bytes) for a dump/file (compressed or not).
+# File size below this limit will raise an warning.
+MIN_DUMP_SIZE=256
+
 # Enable encryption (asymmetric) with GnuPG.
 ENCRYPTION="no"
 
@@ -171,10 +178,17 @@ DEBUG="no"
 GPG_HOMEDIR=
 
 # pg_dump options
-if [ -n "${OPT}" ]; then
-    IFS=" " read -r -a PG_OPTIONS <<< "${OPT}"
+if [ -n "${PGDUMP_OPTS}" ]; then
+    IFS=" " read -r -a PGDUMP_ARGS <<< "${PGDUMP_OPTS}"
+else
+    PGDUMP_ARGS=()
+fi
+
+# pg_dumpall options
+if [ -n "${PGDUMPALL_OPTS}" ]; then
+    IFS=" " read -r -a PGDUMPALL_ARGS <<< "${PGDUMPALL_OPTS}"
 else
-    PG_OPTIONS=()
+    PGDUMPALL_ARGS=()
 fi
 
 # Create required directories
@@ -291,9 +305,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
     )
 
@@ -329,14 +345,15 @@ dbdump () {
     local db_name cmd_prog cmd_args pg_args
 
     db_name="${1}"
-    pg_args="${PG_OPTIONS[*]}"
 
     if [ "${db_name}" = "${GLOBALS_OBJECTS}" ]; then
         cmd_prog="pg_dumpall"
         cmd_args=(--globals-only)
+        pg_args="${PGDUMPALL_ARGS[*]}"
     else
         cmd_prog="pg_dump"
         cmd_args=("${db_name}")
+        pg_args="${PGDUMP_ARGS[*]}"
         if [ "${CREATE_DATABASE}" = "yes" ]; then
             pg_args+=(--create)
         fi
@@ -351,9 +368,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
 }
 # }}}
@@ -425,8 +444,11 @@ dump() {
     if [ -f "${dump_file}" ]; then
         log_debug "Fixing permissions (${PERM}) on '${dump_file}'"
         chmod "${PERM}" "${dump_file}"
+        fsize=$(stat -c '%s' "${dump_file}")
         if [ ! -s "${dump_file}" ]; then
-            log_error "Something went wrong '${dump_file}' is empty (no space left on device?)"
+            log_error "Something went wrong '${dump_file}' is empty"
+        elif [ "${fsize}" -lt "${MIN_DUMP_SIZE}" ]; then
+            log_warn "'${dump_file}' (${fsize} bytes) is below the minimum required size (${MIN_DUMP_SIZE} bytes)"
         fi
     else
         log_error "Something went wrong '${dump_file}' does not exists (error during dump?)"