2 Commit-ok 9c904a75ff ... f341e5afce

Szerző SHA1 Üzenet Dátum
  Emmanuel Bouthenot f341e5afce Improve portability (thanks to Dennis Eriksen) 8 hónapja
  Emmanuel Bouthenot 817c773ac3 Re-formating code 8 hónapja
1 módosított fájl, 77 hozzáadás és 65 törlés
  1. 77 65
      autopostgresqlbackup

+ 77 - 65
autopostgresqlbackup

@@ -161,7 +161,7 @@ DNOW="$(date '+%u')"                # Day number of the week 1 to 7 where 1 repr
 DNOM="$(date '+%d')"                # Date of the Month e.g. 27
 LOG_DIR="${BACKUPDIR}"              # Directory where the main log is saved
 # Fix day of month (left padding with 0)
-DOMONTHLY="$(echo "${DOMONTHLY}" | sed -r 's/^[0-9]$/0\0/')"
+DOMONTHLY="$(printf '%.2i' "${DOMONTHLY}")"
 
 # Using a shared memory filesystem (if available) to avoid
 # issues when there is no left space on backup storage
@@ -177,20 +177,6 @@ DEBUG="no"
 # Encryption prerequisites
 GPG_HOMEDIR=
 
-# pg_dump options
-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
-    PGDUMPALL_ARGS=()
-fi
-
 # Create required directories
 if [ ! -e "${BACKUPDIR}" ]; then         # Check Backup Directory exists.
     mkdir -p "${BACKUPDIR}"
@@ -208,18 +194,18 @@ if [ ! -e "${BACKUPDIR}/monthly" ]; then # Check Monthly Directory exists.
     mkdir -p "${BACKUPDIR}/monthly"
 fi
 
-# Hostname for LOG information and
-# pg_dump{,all} connection settings
-if [ "${DBHOST}" = "localhost" ]; then
-    HOST="$(hostname --fqdn)"
-    PG_CONN=()
-else
-    HOST="${DBHOST}:${DBPORT}"
-    PG_CONN=(--host "${DBHOST}" --port "${DBPORT}")
+# Hostname
+HOSTNAME="$(uname -n)"
+if [[ "${HOSTNAME}" != *.* ]]; then
+    HOSTNAME="$(hostname --fqdn)"
 fi
-if [ -n "${USERNAME}" ]; then
-    PG_CONN+=(--username "${USERNAME}")
+
+HOST="${HOSTNAME}"
+if [ "${DBHOST}" != "localhost" ]; then
+    HOST="${HOSTNAME}:${DBPORT}"
 fi
+
+CONN_ARGS=()
 # }}}
 
 # {{{ log{,ger,_info,_debug,_warn,_error}()
@@ -287,19 +273,50 @@ gpg_setup() {
     GPG_HOMEDIR="$(mktemp --quiet --directory -t "${NAME}.XXXXXX")"
     chmod 700 "${GPG_HOMEDIR}"
     log_debug "With encryption enabled creating a temporary GnuPG home in ${GPG_HOMEDIR}"
-    gpg --quiet --homedir "${GPG_HOMEDIR}" --quick-gen-key --batch --passphrase-file /dev/null "root@$(hostname --fqdn)"
+    gpg --quiet --homedir "${GPG_HOMEDIR}" --quick-gen-key --batch --passphrase-file /dev/null "root@${HOSTNAME}"
+}
+# }}}
+
+# {{{ encryption()
+encryption() {
+    log_debug "Encrypting using public key ${ENCRYPTION_PUBLIC_KEY}"
+    gpg --homedir "${GPG_HOMEDIR}" --encrypt --passphrase-file /dev/null --recipient-file "${ENCRYPTION_PUBLIC_KEY}" 2> >(logger "err" "error")
 }
 # }}}
 
-# {{{ dblist()
-dblist () {
+# {{{ compression()
+compression () {
+    if [ -n "${COMP_OPTS}" ]; then
+        IFS=" " read -r -a comp_args <<< "${COMP_OPTS}"
+        log_debug "Compressing using '${COMP} ${comp_args[*]}'"
+        "${COMP}" "${comp_args[@]}" 2> >(logger "err" "error")
+    else
+        log_debug "Compressing using '${COMP}'"
+        "${COMP}" 2> >(logger "err" "error")
+    fi
+}
+# }}}
+
+# {{{ pgdb_init()
+pgdb_init () {
+    if [ "${DBHOST}" != "localhost" ]; then
+        CONN_ARGS=(--host "${DBHOST}" --port "${DBPORT}")
+    fi
+    if [ -n "${USERNAME}" ]; then
+        CONN_ARGS+=(--username "${USERNAME}")
+    fi
+}
+# }}}
+
+# {{{ pgdb_list()
+pgdb_list () {
     local cmd_prog cmd_args raw_dblist dblist dbexcl databases
 
     cmd_prog="psql"
     cmd_args=(-t -l -A -F:)
 
-    if [ "${#PG_CONN[@]}" -gt 0 ]; then
-        cmd_args+=("${PG_CONN[@]}")
+    if [ "${#CONN_ARGS[@]}" -gt 0 ]; then
+        cmd_args+=("${CONN_ARGS[@]}")
     fi
 
     log_debug "Running command: ${cmd_prog} ${cmd_args[*]}"
@@ -315,7 +332,7 @@ dblist () {
 
     read -r -a dblist <<< "$(
         printf "%s" "${raw_dblist}" | \
-            sed -r -n 's/^([^:]+):.+$/\1/p' | \
+            sed -E -n 's/^([^:]+):.+$/\1/p' | \
             tr '\n' ' '
     )"
     log_debug "Automatically found databases: ${dblist[*]}"
@@ -340,12 +357,25 @@ dblist () {
 }
 # }}}
 
-# {{{ dbdump()
-dbdump () {
+# {{{ pgdb_dump()
+pgdb_dump () {
     local db_name cmd_prog cmd_args pg_args
 
     db_name="${1}"
 
+    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
+        PGDUMPALL_ARGS=()
+    fi
+
     if [ "${db_name}" = "${GLOBALS_OBJECTS}" ]; then
         cmd_prog="pg_dumpall"
         cmd_args=(--globals-only)
@@ -359,8 +389,8 @@ dbdump () {
         fi
     fi
 
-    if [ "${#PG_CONN[@]}" -gt 0 ]; then
-        cmd_args+=("${PG_CONN[@]}")
+    if [ "${#CONN_ARGS[@]}" -gt 0 ]; then
+        cmd_args+=("${CONN_ARGS[@]}")
     fi
     if [ "${#pg_args[@]}" -gt 0 ]; then
         cmd_args+=("${pg_args[@]}")
@@ -377,26 +407,6 @@ dbdump () {
 }
 # }}}
 
-# {{{ encryption()
-encryption() {
-    log_debug "Encrypting using public key ${ENCRYPTION_PUBLIC_KEY}"
-    gpg --homedir "${GPG_HOMEDIR}" --encrypt --passphrase-file /dev/null --recipient-file "${ENCRYPTION_PUBLIC_KEY}" 2> >(logger "err" "error")
-}
-# }}}
-
-# {{{ compression()
-compression () {
-    if [ -n "${COMP_OPTS}" ]; then
-        IFS=" " read -r -a comp_args <<< "${COMP_OPTS}"
-        log_debug "Compressing using '${COMP} ${comp_args[*]}'"
-        "${COMP}" "${comp_args[@]}" 2> >(logger "err" "error")
-    else
-        log_debug "Compressing using '${COMP}'"
-        "${COMP}" 2> >(logger "err" "error")
-    fi
-}
-# }}}
-
 # {{{ dump()
 dump() {
     local db_name dump_file comp_ext
@@ -429,16 +439,16 @@ dump() {
 
     if [ -n "${COMP}" ] && [ "${ENCRYPTION}" = "yes" ]; then
         log_debug "Dumping (${db_name}) +compress +encrypt to '${dump_file}'"
-        dbdump "${db_name}" | compression | encryption > "${dump_file}"
+        pgdb_dump "${db_name}" | compression | encryption > "${dump_file}"
     elif [ -n "${COMP}" ]; then
         log_debug "Dumping (${db_name}) +compress to '${dump_file}'"
-        dbdump "${db_name}" | compression > "${dump_file}"
+        pgdb_dump "${db_name}" | compression > "${dump_file}"
     elif [ "${ENCRYPTION}" = "yes" ]; then
         log_debug "Dumping (${db_name}) +encrypt to '${dump_file}'"
-        dbdump "${db_name}" | encryption > "${dump_file}"
+        pgdb_dump "${db_name}" | encryption > "${dump_file}"
     else
         log_debug "Dumping (${db_name}) to '${dump_file}'"
-        dbdump "${db_name}" > "${dump_file}"
+        pgdb_dump "${db_name}" > "${dump_file}"
     fi
 
     if [ -f "${dump_file}" ]; then
@@ -472,9 +482,9 @@ cleanup() {
     log_info "Rotating ${count} ${when} backups..."
     log_debug "Looking for '${db}_*' in '${dumpdir}/${when}/${db}'"
     find "${dumpdir}/${when}/${db}/" -name "${db}_*" | \
-        sed -r 's/^.+([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}h[0-9]{2}m).*$/\1 \0/' | \
+        sed -E 's/(^.+([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}h[0-9]{2}m).*$)/\2 \1/' | \
         sort -r | \
-        sed -r -n 's/\S+ //p' | \
+        sed -E -n 's/\S+ //p' | \
         tail -n "+${count}" | \
         xargs -L1 rm -fv | \
         while IFS= read -r line ; do
@@ -566,8 +576,8 @@ if [ "${ENCRYPTION}" = "yes" ]; then
                 log_warn "Encryption using openssl is no longer supported: see ${HOMEPAGE}#openssl-encryption"
             fi
         else
-            keyfp="$(echo "${keyinfo}" | sed -r -n 's/^\s*([a-z0-9]+)\s*$/\1/pi')"
-            keyuid="$(echo "${keyinfo}" | sed -r -n 's/^\s*uid\s+(\S.*)$/\1/pi' | head -n1)"
+            keyfp="$(echo "${keyinfo}" | sed -E -n 's/^\s*([a-z0-9]+)\s*$/\1/pi')"
+            keyuid="$(echo "${keyinfo}" | sed -E -n 's/^\s*uid\s+(\S.*)$/\1/pi' | head -n1)"
             log_info "Encryption public key is: 0x${keyfp} (${keyuid})"
         fi
     fi
@@ -587,9 +597,11 @@ fi
 
 # If backing up all DBs on the server
 if [ "${DBNAMES}" = "all" ]; then
-    DBNAMES="$(dblist)"
+    DBNAMES="$(pgdb_list)"
 fi
 
+pgdb_init
+
 for db in ${DBNAMES} ; do
     db="${db//%/ / }"
     log_info "Backup of Database (${period}) '${db}'"
@@ -644,7 +656,7 @@ 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 --fqdn)" "${MAILADDR}"
+    ) | mail -s "${NAME} issues on ${HOSTNAME}" "${MAILADDR}"
 fi
 # }}}