]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding pp_archive_old_homes.sh
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 18 Apr 2017 13:01:14 +0000 (15:01 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 18 Apr 2017 13:01:14 +0000 (15:01 +0200)
pp_archive_old_homes.sh [new file with mode: 0755]

diff --git a/pp_archive_old_homes.sh b/pp_archive_old_homes.sh
new file mode 100755 (executable)
index 0000000..b4d4a25
--- /dev/null
@@ -0,0 +1,462 @@
+#!/bin/bash
+################################################################################
+##
+## Header details
+##
+## Host..............: achilles.pixelpark.com
+## Zone..............: N/A
+##
+## Copyright ........: Pixelpark AG
+## File ........ ....: pp_archive_old_homes.sh
+## Major-Version ....: 2
+## Minor-Version ....: 00
+## Maintained by ....: Frank Brehm
+## Description ......: 
+##
+##
+## History ..........:
+##
+## Vers. Date        By               Reason
+## ----- ----------- ---------------- ---------------------------------------
+## 01    09.01.2009  thomas.kotschok  initial Version
+##
+################################################################################
+
+################################################################################
+##
+## Vars
+##
+################################################################################
+##
+## TIME       -> last Days ago
+##
+## DATE       -> Actual Time
+##
+## NODENAME   -> machine name
+##
+## SAVEPATH   -> where go's the archive
+##
+## ANNOTATION -> Archive save sets can be annotated
+##
+################################################################################
+
+set -e
+set -u
+
+VERBOSE="n"
+DEBUG="n"
+
+# console colors:
+RED=""
+YELLOW=""
+GREEN=""
+BLUE=""
+NORMAL=""
+
+HAS_TTY='y'
+
+DO_ASK="n"
+SIMULATE="n"
+NO_REMOVE="n"
+
+BASENAME="$(basename ${0})"
+BASE_DIR="$(dirname ${0})"
+
+INPUT_TIMEOUT=5
+
+DATE=$( date '+%G%m%d' )
+
+HOMES_DIR="/mnt/nfs/home"
+SAVEPATH="${HOMES_DIR}/_old_homes"
+NODENAME=$( uname -n )
+LOGFILE="${SAVEPATH}/pp_archive_old_homes.log"
+FILELIST=""
+
+declare -a DIRS2ARCH=()
+declare -A BACKUP_FILES=()
+
+#-------------------------------------------------------------------
+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
+               Archiving and removing all given directories beneath '${HOMES_DIR}'.
+
+               EOF
+    )
+}
+
+#------------------------------------------------------------------------------
+usage() {
+    cat <<-EOF
+       Usage: ${BASENAME} [OPTIONS] [<DIR> [<DIR ...]]
+              ${BASENAME} [-h|--help]
+
+           Options:
+               -a|--ask        Asking for some additional directories to archive and remove.
+               -s|--simulate   Simulation mode, nothing is really done.
+               -N|--no-remove  Don't remove the given directories after archiving them.
+               -d|--debug      Debug output (bash -x).
+               -v|--verbose    Set verbosity on.
+               --nocolor       Don't use colors on display.
+               -h|--help       Show this output and exit.
+       EOF
+}
+
+#------------------------------------------------------------------------------
+get_options() {
+
+    local tmp=
+    local base_dir=
+
+    set +e
+    tmp=$( getopt -o asNdvh \
+                    --long ask,simulate,no-remove,debug,verbose,nocolor,help \
+                    -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
+            -a|--ask)
+                DO_ASK="y"
+                shift
+                ;;
+            -s|--simulate)
+                SIMULATE="y"
+                shift
+                ;;
+            -N|--no-remove)
+                NO_REMOVE="y"
+                shift
+                ;;
+            -d|--debug)
+                DEBUG="y"
+                shift
+                ;;
+            -v|--verbose)
+                VERBOSE="y"
+                shift
+                ;;
+            --nocolor)
+                RED=""
+                YELLOW=""
+                GREEN=""
+                BLUE=""
+                NORMAL=""
+                shift
+                ;;
+            -h|--help)
+                description
+                usage
+                exit 0
+                ;;
+            --) shift
+                break
+                ;;
+            *)  echo "Internal error!"
+                exit 1
+                ;;
+        esac
+    done
+
+    if [[ "${DEBUG}" = "y" ]] ; then
+        set -x
+    fi
+
+    if [[ "$#" -lt 1 ]] ; then
+        DO_ASK="y"
+    else
+        local item
+        for item in "$@" ; do
+            base_dir=$( basename "${item}" )
+            add_dir "${base_dir}"
+        done
+        if [[ "${#DIRS2ARCH[*]}" == "0" ]] ; then
+            DO_ASK="y"
+        fi
+    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() {
+    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
+}
+
+if [[ ! -d "${HOMES_DIR}" ]] ; then
+    echo "Directory '${HOMES_DIR}' does not exists." >&2
+    exit 4
+fi
+HOMES_DIR=$( readlink -f "${HOMES_DIR}" )
+
+if [[ ! -d "${SAVEPATH}" ]] ; then
+    echo "Directory '${SAVEPATH}' does not exists." >&2
+    exit 5
+fi
+SAVEPATH=$( readlink -f "${SAVEPATH}" )
+
+#------------------------------------------------------------------------------
+add_dir() {
+
+    local base_dir="$1"
+
+    local i=
+    local real_path="${HOMES_DIR}/${base_dir}"
+    local backup_file=
+
+    if [[ ! -e "${real_path}" ]] ; then
+        error "Directory '${real_path}' does not exists."
+        return 0
+    fi
+
+    real_path=$( readlink -f "${real_path}" )
+    if [[ "${real_path}" == "${SAVEPATH}" ]] ; then
+        error "Directory '${real_path}' is the backup directory."
+        return 0
+    fi
+
+    i=0
+    backup_file="${SAVEPATH}/${base_dir}.${i}.tar.gz"
+    while [[ -e "${backup_file}" ]] ; do
+        i=$(( $i + 1 ))
+        backup_file="${SAVEPATH}/${base_dir}.${i}.tar.gz"
+    done
+
+    info "ADD ${base_dir}      -> ${backup_file}"
+    DIRS2ARCH+=("${base_dir}")
+    BACKUP_FILES[${base_dir}]="${backup_file}"
+
+}
+
+#------------------------------------------------------------------------------
+create_file_list() {
+
+    local cont="1"
+    local input=
+    local base_dir=
+    local ret=
+
+    info "Create a file list ..."
+    echo
+
+    # constructing comand
+    echo
+
+    while [[ "${cont}" -eq 1 ]] ; do
+
+        echo "######################################"
+        echo " q = Done "
+        echo " e = Exit "
+        echo "weiteren hinzufügen oder mit \"q\" beenden?"
+        echo "######################################"
+        echo ""
+
+        set +e
+        read -p "Directory [<dir>|q|e]: " -t "${INPUT_TIMEOUT}" input
+        ret="$?"
+        set -e
+        if [[ -z "${input}" ]] ; then
+            echo
+        fi
+        debug "Got input: \"${input}\", return value: ${ret}."
+        if [[ -z "${input}" ]] ; then
+            if [[ ${ret} -ne 0 ]] ; then
+                error "Input ${RED}timeout after ${INPUT_TIMEOUT} seconds${NORMAL}."
+                exit 1
+            fi
+            continue
+        fi
+        case "${input}" in
+            q|Q)
+                cont=0
+                ;;
+            e|E)
+                info "Exit"
+                exit 0
+                ;;
+            *)
+                base_dir=$( basename "${input}" )
+                add_dir "${base_dir}"
+                ;;
+        esac
+    done
+
+}
+
+#------------------------------------------------------------------------------
+backup_archive() {
+
+    local base_dir=
+    local backup_file=
+    local real_path=
+    local cmd=
+    local annotation="Old home archive from ${NODENAME} -> ${SAVEPATH} - Date ${DATE}"
+
+    if [[ "${SIMULATE}" != "y" ]] ; then
+        echo | tee -a "${LOGFILE}"
+        echo "${annotation}" | tee -a "${LOGFILE}"
+    else
+        echo
+        echo "${annotation}"
+    fi
+    cd "${HOMES_DIR}"
+
+    for base_dir in "${DIRS2ARCH[@]}" ; do
+
+        echo
+        debug "Performing '${base_dir}' ..."
+
+        backup_file="${BACKUP_FILES[${base_dir}]}"
+        real_path="${HOMES_DIR}/${base_dir}"
+
+        info "Creating '${GREEN}${backup_file}${NORMAL}' ..."
+
+        cmd="tar --create --gzip --file=\"${backup_file}\" \"${base_dir}\""
+        if [[ "${VERBOSE}" == "y" ]] ; then
+            echo "${cmd}"
+        fi
+        eval ${cmd}
+
+        debug "Setting timestamps of '${backup_file}' ..."
+        cmd="touch -r \"${real_path}\" \"${backup_file}\""
+        if [[ "${VERBOSE}" == "y" ]] ; then
+            echo "${cmd}"
+        fi
+        eval ${cmd}
+
+        if [[ "${NO_REMOVE}" != "y" ]] ; then
+            info "Removing '${GREEN}${real_path}${NORMAL}' ..."
+            cmd="rm -rf \"${base_dir}\""
+            if [[ "${VERBOSE}" == "y" ]] ; then
+                echo "${cmd}"
+            fi
+            if [[ "${SIMULATE}" != "y" ]] ; then
+                eval ${cmd}
+            fi
+        fi
+
+        if [[ "${SIMULATE}" != "y" ]] ; then
+            echo " - ${base_dir}       ${backup_file}" >> "${LOGFILE}"
+        fi
+        printf "   "
+        ls -l "${backup_file}" || true
+
+        if [[ "${SIMULATE}" == "y" ]] ; then
+            debug "Removing '${backup_file}' ..."
+            rm -f "${backup_file}"
+        fi
+
+    done
+
+}
+
+################################################################################
+##
+## Main
+##
+################################################################################
+
+#------------------------------------------------------------------------------
+main() {
+
+    get_options "$@"
+
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        info "Simulation mode, nothing is really done."
+    fi
+
+    if [[ "${DO_ASK}" == "y" ]] ; then
+        create_file_list
+    fi
+
+    if [[ "${#DIRS2ARCH[*]}" == "0" ]] ; then
+        warn "No directories to archive given."
+        exit 0
+    fi
+
+    backup_archive
+
+    echo
+    info "Finished."
+
+}
+
+main "$@"
+
+exit 0
+
+# vim: ts=4 et list