|
@@ -177,20 +177,6 @@ DEBUG="no"
|
|
# Encryption prerequisites
|
|
# Encryption prerequisites
|
|
GPG_HOMEDIR=
|
|
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
|
|
# Create required directories
|
|
if [ ! -e "${BACKUPDIR}" ]; then # Check Backup Directory exists.
|
|
if [ ! -e "${BACKUPDIR}" ]; then # Check Backup Directory exists.
|
|
mkdir -p "${BACKUPDIR}"
|
|
mkdir -p "${BACKUPDIR}"
|
|
@@ -208,18 +194,18 @@ if [ ! -e "${BACKUPDIR}/monthly" ]; then # Check Monthly Directory exists.
|
|
mkdir -p "${BACKUPDIR}/monthly"
|
|
mkdir -p "${BACKUPDIR}/monthly"
|
|
fi
|
|
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
|
|
fi
|
|
-if [ -n "${USERNAME}" ]; then
|
|
|
|
- PG_CONN+=(--username "${USERNAME}")
|
|
|
|
|
|
+
|
|
|
|
+HOST="${HOSTNAME}"
|
|
|
|
+if [ "${DBHOST}" != "localhost" ]; then
|
|
|
|
+ HOST="${HOSTNAME}:${DBPORT}"
|
|
fi
|
|
fi
|
|
|
|
+
|
|
|
|
+CONN_ARGS=()
|
|
# }}}
|
|
# }}}
|
|
|
|
|
|
# {{{ log{,ger,_info,_debug,_warn,_error}()
|
|
# {{{ log{,ger,_info,_debug,_warn,_error}()
|
|
@@ -287,19 +273,50 @@ gpg_setup() {
|
|
GPG_HOMEDIR="$(mktemp --quiet --directory -t "${NAME}.XXXXXX")"
|
|
GPG_HOMEDIR="$(mktemp --quiet --directory -t "${NAME}.XXXXXX")"
|
|
chmod 700 "${GPG_HOMEDIR}"
|
|
chmod 700 "${GPG_HOMEDIR}"
|
|
log_debug "With encryption enabled creating a temporary GnuPG home in ${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
|
|
local cmd_prog cmd_args raw_dblist dblist dbexcl databases
|
|
|
|
|
|
cmd_prog="psql"
|
|
cmd_prog="psql"
|
|
cmd_args=(-t -l -A -F:)
|
|
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
|
|
fi
|
|
|
|
|
|
log_debug "Running command: ${cmd_prog} ${cmd_args[*]}"
|
|
log_debug "Running command: ${cmd_prog} ${cmd_args[*]}"
|
|
@@ -340,12 +357,25 @@ dblist () {
|
|
}
|
|
}
|
|
# }}}
|
|
# }}}
|
|
|
|
|
|
-# {{{ dbdump()
|
|
|
|
-dbdump () {
|
|
|
|
|
|
+# {{{ pgdb_dump()
|
|
|
|
+pgdb_dump () {
|
|
local db_name cmd_prog cmd_args pg_args
|
|
local db_name cmd_prog cmd_args pg_args
|
|
|
|
|
|
db_name="${1}"
|
|
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
|
|
if [ "${db_name}" = "${GLOBALS_OBJECTS}" ]; then
|
|
cmd_prog="pg_dumpall"
|
|
cmd_prog="pg_dumpall"
|
|
cmd_args=(--globals-only)
|
|
cmd_args=(--globals-only)
|
|
@@ -359,8 +389,8 @@ dbdump () {
|
|
fi
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
|
|
- if [ "${#PG_CONN[@]}" -gt 0 ]; then
|
|
|
|
- cmd_args+=("${PG_CONN[@]}")
|
|
|
|
|
|
+ if [ "${#CONN_ARGS[@]}" -gt 0 ]; then
|
|
|
|
+ cmd_args+=("${CONN_ARGS[@]}")
|
|
fi
|
|
fi
|
|
if [ "${#pg_args[@]}" -gt 0 ]; then
|
|
if [ "${#pg_args[@]}" -gt 0 ]; then
|
|
cmd_args+=("${pg_args[@]}")
|
|
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()
|
|
dump() {
|
|
dump() {
|
|
local db_name dump_file comp_ext
|
|
local db_name dump_file comp_ext
|
|
@@ -429,16 +439,16 @@ dump() {
|
|
|
|
|
|
if [ -n "${COMP}" ] && [ "${ENCRYPTION}" = "yes" ]; then
|
|
if [ -n "${COMP}" ] && [ "${ENCRYPTION}" = "yes" ]; then
|
|
log_debug "Dumping (${db_name}) +compress +encrypt to '${dump_file}'"
|
|
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
|
|
elif [ -n "${COMP}" ]; then
|
|
log_debug "Dumping (${db_name}) +compress to '${dump_file}'"
|
|
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
|
|
elif [ "${ENCRYPTION}" = "yes" ]; then
|
|
log_debug "Dumping (${db_name}) +encrypt to '${dump_file}'"
|
|
log_debug "Dumping (${db_name}) +encrypt to '${dump_file}'"
|
|
- dbdump "${db_name}" | encryption > "${dump_file}"
|
|
|
|
|
|
+ pgdb_dump "${db_name}" | encryption > "${dump_file}"
|
|
else
|
|
else
|
|
log_debug "Dumping (${db_name}) to '${dump_file}'"
|
|
log_debug "Dumping (${db_name}) to '${dump_file}'"
|
|
- dbdump "${db_name}" > "${dump_file}"
|
|
|
|
|
|
+ pgdb_dump "${db_name}" > "${dump_file}"
|
|
fi
|
|
fi
|
|
|
|
|
|
if [ -f "${dump_file}" ]; then
|
|
if [ -f "${dump_file}" ]; then
|
|
@@ -587,9 +597,11 @@ fi
|
|
|
|
|
|
# If backing up all DBs on the server
|
|
# If backing up all DBs on the server
|
|
if [ "${DBNAMES}" = "all" ]; then
|
|
if [ "${DBNAMES}" = "all" ]; then
|
|
- DBNAMES="$(dblist)"
|
|
|
|
|
|
+ DBNAMES="$(pgdb_list)"
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
+pgdb_init
|
|
|
|
+
|
|
for db in ${DBNAMES} ; do
|
|
for db in ${DBNAMES} ; do
|
|
db="${db//%/ / }"
|
|
db="${db//%/ / }"
|
|
log_info "Backup of Database (${period}) '${db}'"
|
|
log_info "Backup of Database (${period}) '${db}'"
|
|
@@ -644,7 +656,7 @@ if [ "${DEBUG}" = "no" ] && grep -q '^err|' "${LOG_FILE}" ; then
|
|
fi
|
|
fi
|
|
done
|
|
done
|
|
printf "\nFor more information, try to run %s in debug mode, see \`%s -h\`\n" "${NAME}" "$(basename "$0")"
|
|
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
|
|
fi
|
|
# }}}
|
|
# }}}
|
|
|
|
|