]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Start refactoring of bin/backup_pgsql.sh
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 16 Nov 2017 10:10:26 +0000 (11:10 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 16 Nov 2017 10:10:26 +0000 (11:10 +0100)
bin/backup_pgsql.sh

index 19127abfb0a4cc43896767580683ec04023865d0..fc61b932c48cc10aa0cdd87f205c301384a9f5ed 100755 (executable)
@@ -39,6 +39,26 @@ set -u
 export LC_ALL=C
 export LANG=C
 
+VERBOSE="n"
+DEBUG="n"
+QUIET='n'
+
+VERSION="2.1"
+
+# console colors:
+RED=""
+YELLOW=""
+GREEN=""
+BLUE=""
+NORMAL=""
+
+HAS_TTY='y'
+
+BASENAME="$(basename ${0})"
+BASE_DIR="$(dirname ${0})"
+
+declare -a DATABASES=()
+
 #########################################################
 # Modify below variables to fit your need ----
 #########################################################
@@ -54,6 +74,231 @@ export PGSQL_SYS_USER="postgres"
 # Where to store backup copies.
 export BACKUP_ROOTDIR="/var/backup"
 
+#-------------------------------------------------------------------
+detect_color() {
+
+    local safe_term="${TERM//[^[:alnum:]]/?}"
+    local match_lhs=""
+    local use_color="false"
+    [[ -f ~/.dir_colors   ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
+    [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
+    [[ -z ${match_lhs}    ]] \
+        && type -P dircolors >/dev/null \
+        && match_lhs=$(dircolors --print-database)
+    [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color="true"
+
+    # console colors:
+    if [ "${use_color}" = "true" ] ; then
+        RED="\033[38;5;196m"
+        YELLOW="\033[38;5;226m"
+        GREEN="\033[38;5;46m"
+        BLUE="\033[38;5;27m"
+        NORMAL="\033[39m"
+    else
+        RED=""
+        YELLOW=""
+        GREEN=""
+        BLUE=""
+        NORMAL=""
+    fi
+
+    local my_tty=$(tty)
+    if [[ "${my_tty}" =~ 'not a tty' ]] ; then
+        my_tty='-'
+    fi
+
+    if [[ "${my_tty}" = '-' || "${safe_term}" = "dump" ]] ; then
+        HAS_TTY='n'
+    fi
+
+}
+detect_color
+
+#------------------------------------------------------------------------------
+description() {
+    echo -e $( cat <<-EOF
+               Creates a backup of all databases of the PostgreSQL installatio
+               on the current host.
+
+               Only the user '${GREEN}${PGSQL_SYS_USER}${NORMAL}' may execute this script.
+
+               EOF
+    )
+}
+
+#------------------------------------------------------------------------------
+usage() {
+    cat <<-EOF
+       Usage: ${BASENAME} [-K DAYS|--keep=DAYS] [-d|--debug] [[-v|--verbose] | [-q|--quiet]]] [--nocolor]
+              ${BASENAME} [-h|--help]
+              ${BASENAME} [-V|--version]
+
+           Options:
+               -k|--keep DAYS  Keep the backup files of the last DAYS. Default: ${KEEP_DAYS} days.
+               -d|--debug      Debug output (bash -x).
+               -v|--verbose    Set verbosity on. Mutually exclusive to '--quiet'.
+               -q|--quiet      Quiet execution, only errors and warnings are shown.
+               --nocolor       Don't use colors on display.
+               -h|--help       Show this output and exit.
+               -V|--version    prints out version number of the script and exit
+       EOF
+}
+
+
+#------------------------------------------------------------------------------
+get_options() {
+
+    local tmp=
+    local base_dir=
+
+    set +e
+    tmp=$( getopt -o K:dvqhV \
+                    --long keep:,debug,verbose,quiet,nocolor,help,version \
+                    -n "${BASENAME}" -- "$@" )
+    if [[ $? != 0 ]] ; then
+        echo "" >&2
+        usage >&2
+        exit 1
+    fi
+    set -e
+
+    # Note the quotes around `$TEMP': they are essential!
+    eval set -- "${tmp}"
+
+    local p=
+
+    while true ; do
+        case "$1" in
+            -k|--keep)
+                KEEP_DAYS="$1"
+                shift
+                shift
+                ;;
+            -d|--debug)
+                DEBUG="y"
+                shift
+                ;;
+            -v|--verbose)
+                VERBOSE="y"
+                shift
+                ;;
+            -q|--quiet)
+                QUIET="y"
+                shift
+                ;;
+            --nocolor)
+                RED=""
+                YELLOW=""
+                GREEN=""
+                BLUE=""
+                NORMAL=""
+                shift
+                ;;
+            -h|--help)
+                description
+                echo
+                usage
+                exit 0
+                ;;
+            -V|--version)
+                echo "${BASENAME} version: ${VERSION}"
+                exit 0
+                ;;
+            --) shift
+                break
+                ;;
+            *)  echo "Internal error!"
+                exit 1
+                ;;
+        esac
+    done
+
+    if [[ "${DEBUG}" = "y" ]] ; then
+        set -x
+    fi
+    if [[ "${VERBOSE}" == "y" && "${QUIET}" == "y" ]] ; then
+        error "The parameters '${RED}${VERBOSE}${NORMAL}' and '${RED}${VERBOSE}${NORMAL}' are mutually exclusive."
+        usage >&2
+        exit 1
+    fi
+
+    local cur_user=$( id -u -n )
+    if [[ "${cur_user}" != "${PGSQL_SYS_USER}" ]] ; then
+        error "Wrong user '${RED}${cur_user}${NORMAL}'."
+        echo >&2
+        description >&2
+        echo
+        usage >&2
+        exit 1
+    fi
+
+}
+
+#########################################
+# Some often used funktions
+
+#------------------------------------------------------------------------------
+my_date() {
+    date +'%F %T.%N %:::z'
+}
+
+#------------------------------------------------------------------------------
+debug() {
+    if [[ "${VERBOSE}" != "y" ]] ; then
+        return 0
+    fi
+    echo -e " * [$(my_date)] [${BASENAME}:DEBUG]: $@" >&2
+}
+
+#------------------------------------------------------------------------------
+info() {
+    if [[ "${QUIET}" == "y" ]] ; then
+        return 0
+    fi
+    echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" >&2
+}
+
+#------------------------------------------------------------------------------
+warn() {
+    echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2
+}
+
+#------------------------------------------------------------------------------
+error() {
+    echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2
+}
+
+#------------------------------------------------------------------------------
+empty_line() {
+    if [[ "${QUIET}" == "y" ]] ; then
+        return 0
+    fi
+    echo
+}
+
+################################################################################
+##
+## Main
+##
+################################################################################
+
+#------------------------------------------------------------------------------
+main() {
+
+    get_options "$@"
+
+    empty_line
+    info "Finished."
+
+}
+
+main "$@"
+
+exit 0
+
+
+
+
 export SUDO=""
 if [[ "$( id -u -n )" != "${PGSQL_SYS_USER}" ]] ; then
     export SUDO="sudo -i -u ${PGSQL_SYS_USER} "