# - On Linux, it's postgres.
# - On FreeBSD, it's pgsql.
# - On OpenBSD, it's _postgresql.
-export PGSQL_SYS_USER="postgres"
+PGSQL_SYS_USER="postgres"
# Where to store backup copies.
-export BACKUP_ROOTDIR="/var/backup"
+BACKUP_ROOTDIR="/var/backup"
+
+# Date.
+YEAR="$( date +%Y)"
+MONTH="$( date +%m)"
+DAY="$( date +%d)"
+TIME="$( date +%H:%M:%S)"
+TIMESTAMP="${YEAR}-${MONTH}-${DAY}-${TIME}"
+
+# Pre-defined backup status
+BACKUP_SUCCESS='YES'
+
+# Define, check, create directories.
+BACKUP_DIR="${BACKUP_ROOTDIR}/pgsql/${YEAR}/${MONTH}/${DAY}"
+TMP_DIR=
#-------------------------------------------------------------------
detect_color() {
echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2
}
+#------------------------------------------------------------------------------
+MKDIR() {
+ local cmd="mkdir"
+ if [[ "${VERBOSE}" == "y" ]] ; then
+ cmd+=" --verbose"
+ fi
+ eval ${cmd} "$@"
+}
+
+#------------------------------------------------------------------------------
+RM() {
+ local cmd="rm"
+ if [[ "${VERBOSE}" == "y" ]] ; then
+ cmd+=" --verbose"
+ fi
+ eval ${cmd} "$@"
+}
+
#------------------------------------------------------------------------------
empty_line() {
if [[ "${QUIET}" == "y" ]] ; then
}
+#------------------------------------------------------------------------------
+cleanup_tmp_dir() {
+ if [[ -n "${TMP_DIR}" ]] ; then
+ debug "Removing temporary directory is '${TMP_DIR}' ..."
+ RM --force --recursive "${TMP_DIR}"
+ fi
+}
+
+#------------------------------------------------------------------------------
+prepare_dirs() {
+
+ info "Creating all necessary directories ..."
+ MKDIR -p "${BACKUP_DIR}"
+ TMP_DIR=$( mktemp -d -p "${HOME}" backup.XXXXXXXX.d )
+ debug "Temporary directory is '${TMP_DIR}'."
+
+ debug "Cretaing trap to cleanup temporary directory ..."
+ trap cleanup_tmp_dir INT TERM EXIT ABRT
+
+}
+
################################################################################
##
## Main
get_options "$@"
get_databases
+ prepare_dirs
empty_line
+ debug "Deactivating trap."
+ trap - INT TERM EXIT ABRT
+ cleanup_tmp_dir
info "Finished."
}