3 Commits f341e5afce ... ef88f5bbe9

Author SHA1 Message Date
  Emmanuel Bouthenot ef88f5bbe9 feat: display errors on stdout when MAILADDR is empty 8 months ago
  Emmanuel Bouthenot f1d8deda92 fix: fix the return code and do not send a report by email if $MAILADDR is not set 8 months ago
  Emmanuel Bouthenot 30e760454c fix: Use the specified port on socket connection 8 months ago
2 changed files with 32 additions and 20 deletions
  1. 3 3
      Documentation.md
  2. 29 17
      autopostgresqlbackup

+ 3 - 3
Documentation.md

@@ -4,7 +4,7 @@
 
 ### `MAILADDR`
 
-Email Address to send errors to
+Email Address to send errors to. If empty errors are displayed on stdout.
 
 **default**: `root`
 
@@ -26,13 +26,13 @@ Username to access the PostgreSQL server
 
 ### `DBHOST`
 
-Host name (or IP address) of PostgreSQL server
+Host name (or IP address) of PostgreSQL server. Use `localhost` for socket connection or `127.0.0.1` to force TCP connection.
 
 **default**: `localhost`
 
 ### `DBPORT`
 
-Port of PostgreSQL server (only used if `${DBHOST} != localhost`).
+Port of PostgreSQL server. It is also used if `${DBHOST}` is `localhost` (socket connection) as socket name contains port.
 
 **default**: `5432`
 

+ 29 - 17
autopostgresqlbackup

@@ -24,7 +24,7 @@
 
 # {{{ Variables
 
-# Email Address to send errors to
+# Email Address to send errors to. If empty errors are displayed on stdout.
 MAILADDR="root"
 
 # By default, on Debian systems (and maybe others), only postgres user is
@@ -49,10 +49,13 @@ USERNAME="postgres"
 # replace hostname with the value of ${DBHOST}, dbuser with the value of
 # ${USERNAME} and dbpass with the password.
 
-# Host name (or IP address) of PostgreSQL server
+# Host name (or IP address) of PostgreSQL server.
+# Use 'localhost' for socket connection or '127.0.0.1' to force TCP connection
 DBHOST="localhost"
 
-# Port of PostgreSQL server (only used if ${DBHOST} != localhost).
+# Port of PostgreSQL server.
+# It is also used if ${DBHOST} is localhost (socket connection) as socket name
+# contains port
 DBPORT="5432"
 
 # List of database(s) names(s) to backup If you would like to backup all
@@ -169,7 +172,9 @@ if [ -w "/dev/shm" ]; then
     LOG_DIR="/dev/shm"
 fi
 
-LOG_FILE="${LOG_DIR}/${NAME}_${DBHOST//\//_}-$(date '+%Y-%m-%d_%Hh%Mm').log"
+LOG_PREFIX="${LOG_DIR}/${NAME}_${DBHOST//\//_}-$(date '+%Y-%m-%d_%Hh%Mm')"
+LOG_FILE="${LOG_PREFIX}.log"
+LOG_REPORT="${LOG_PREFIX}.report"
 
 # Debug mode
 DEBUG="no"
@@ -200,9 +205,9 @@ if [[ "${HOSTNAME}" != *.* ]]; then
     HOSTNAME="$(hostname --fqdn)"
 fi
 
-HOST="${HOSTNAME}"
-if [ "${DBHOST}" != "localhost" ]; then
-    HOST="${HOSTNAME}:${DBPORT}"
+HOST="${DBHOST}:${DBPORT}"
+if [ "${DBHOST}" = "localhost" ]; then
+    HOST="${HOSTNAME}:${DBPORT} (socket)"
 fi
 
 CONN_ARGS=()
@@ -299,8 +304,9 @@ compression () {
 
 # {{{ pgdb_init()
 pgdb_init () {
+    CONN_ARGS=(--port "${DBPORT}")
     if [ "${DBHOST}" != "localhost" ]; then
-        CONN_ARGS=(--host "${DBHOST}" --port "${DBPORT}")
+        CONN_ARGS+=(--host "${DBHOST}")
     fi
     if [ -n "${USERNAME}" ]; then
         CONN_ARGS+=(--username "${USERNAME}")
@@ -639,7 +645,13 @@ exec 2>&7 7>&-      # Restore stdout and close file descriptor #7.
 # }}}
 
 # {{{ Reporting
-if [ "${DEBUG}" = "no" ] && grep -q '^err|' "${LOG_FILE}" ; then
+if grep -q '^err|' "${LOG_FILE}"; then
+    rc=1
+else
+    rc=0
+fi
+
+if [ "${DEBUG}" = "no" ] && [ ${rc} = 1 ]; then
     (
         printf "*Errors/Warnings* (below) reported during backup on *%s*:\n\n" "${HOST}"
         grep '^err|' "${LOG_FILE}" | cut -d '|' -f 3- | \
@@ -656,24 +668,24 @@ if [ "${DEBUG}" = "no" ] && grep -q '^err|' "${LOG_FILE}" ; then
             fi
         done
         printf "\nFor more information, try to run %s in debug mode, see \`%s -h\`\n" "${NAME}" "$(basename "$0")"
-    ) | mail -s "${NAME} issues on ${HOSTNAME}" "${MAILADDR}"
+    ) > "${LOG_REPORT}"
+
+    if [ -n "${MAILADDR}" ]; then
+        mail -s "${NAME} issues on ${HOSTNAME}" "${MAILADDR}" < "${LOG_REPORT}"
+    else
+        cat "${LOG_REPORT}"
+    fi
 fi
 # }}}
 
 # {{{ Cleanup and exit()
-if [ -s "${LOGERR}" ]; then
-    rc=1
-else
-    rc=0
-fi
-
 # Cleanup GnuPG home dir
 if [ -d "${GPG_HOMEDIR}" ]; then
     rm -rf "${GPG_HOMEDIR}"
 fi
 
 # Clean up log files
-rm -f "${LOG_FILE}"
+rm -f "${LOG_FILE}" "${LOG_REPORT}"
 
 exit ${rc}
 # }}}