From: Frank Brehm Date: Thu, 16 Nov 2017 10:37:42 +0000 (+0100) Subject: Adding preparation of directories X-Git-Tag: 0.1.2~78 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=80031a9184302555210f50bbc1590467c98a0a97;p=pixelpark%2Fadmin-tools.git Adding preparation of directories --- diff --git a/bin/backup_pgsql.sh b/bin/backup_pgsql.sh index 8a469c4..2c8013d 100755 --- a/bin/backup_pgsql.sh +++ b/bin/backup_pgsql.sh @@ -69,10 +69,24 @@ KEEP_DAYS='90' # - 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() { @@ -268,6 +282,24 @@ error() { 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 @@ -303,6 +335,27 @@ get_databases() { } +#------------------------------------------------------------------------------ +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 @@ -315,7 +368,11 @@ main() { get_options "$@" get_databases + prepare_dirs empty_line + debug "Deactivating trap." + trap - INT TERM EXIT ABRT + cleanup_tmp_dir info "Finished." }