]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding bin/manual-backup
authorFrank Brehm <frank@brehm-online.com>
Tue, 20 Apr 2021 05:29:52 +0000 (07:29 +0200)
committerFrank Brehm <frank@brehm-online.com>
Tue, 20 Apr 2021 05:29:52 +0000 (07:29 +0200)
bin/manual-backup [new file with mode: 0755]

diff --git a/bin/manual-backup b/bin/manual-backup
new file mode 100755 (executable)
index 0000000..1698526
--- /dev/null
@@ -0,0 +1,263 @@
+#!/bin/bash
+
+set -e
+set -u
+
+BASE_NAME="$( basename ${0} )"
+MY_REAL_NAME=$( readlink -f $0 )
+BIN_DIR=$( dirname "${MY_REAL_NAME}" )
+BASE_DIR=$( dirname "${BIN_DIR}" )
+LIB_DIR="${BASE_DIR}/lib"
+CONF_DIR="${BASE_DIR}/etc"
+
+if [[ -f "${LIB_DIR}/functions.rc" ]] ; then
+    . "${LIB_DIR}/functions.rc"
+else
+    echo "Bash resource file '${LIB_DIR}/functions.rc' not found" >&2
+    exit 5
+fi
+
+BACKUP_PARENTDIR="/var/backup"
+HOSTSYSTEM=$( hostname -s)
+BACKUP_HOSTDIR="${BACKUP_PARENTDIR}/${HOSTSYSTEM}"
+BACKUP_COPIES=3
+BACKUP_DIR=
+STARTTIME=$(date)
+CDATE=$(date +"%Y-%m-%d")
+REMOVE_DATE=$(date -d "${BACKUP_COPIES} days ago" +"%Y-%m-%d")
+
+LOGFILE="${BACKUP_HOSTDIR}/backup.log"
+BACKUP_DIR="${BACKUP_HOSTDIR}/${CDATE}"
+
+declare -a REMOTE_HOSTS=()
+
+DESCRIPTION=$( cat <<-EOF
+       Macht mal eben so ein Backup.
+
+       EOF
+)
+
+detect_color
+
+#------------------------------------------------------------------------------
+usage() {
+    cat <<-EOF
+       Usage: ${BASE_NAME} [-C COPIES] [-R REMOTE_HOST [-R REMOTE_HOST ...]] [Common Options]
+              ${BASE_NAME} [-h|--help]
+              ${BASE_NAME} [-V|--version]
+
+           Common Options:
+       ${STD_USAGE_MSG}
+       EOF
+
+}
+
+#------------------------------------------------------------------------------
+TAR() {
+
+    local cmd="tar --create --gzip --file $* >/dev/null 2>&1 || true"
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd="tar --create --gzip --verbose --file $* 2>&1 | tee -a \"${LOGFILE}\" || true"
+    fi
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        info "Executing: ${cmd}"
+        return
+    fi
+    debug "Executing: ${cmd}"
+    eval ${cmd}
+
+}
+
+#------------------------------------------------------------------------------
+get_options() {
+
+    local tmp=
+    local base_dir=
+
+    local this_short_options='C:R:'
+    local this_long_options='--copies:,--remote:'
+
+    set +e
+    tmp=$( getopt -o "${STD_SHORT_OPTIONS}${this_short_options}" \
+                  --long "${STD_LONG_OPTIONS},${this_long_options}" \
+                  -n "${BASE_NAME}" -- "$@" )
+    if [[ $? != 0 ]] ; then
+        echo "" >&2
+        usage >&2
+        exit 1
+    fi
+    set -e
+
+    # Note the quotes around `$TEMP': they are essential!
+    eval set -- "${tmp}"
+    eval_common_options "$@"
+    if [[ "${DEBUG}" == 'y' ]] ; then
+        declare -p REMAINING_OPTS
+        declare -p REMAINING_ARGS
+    fi
+
+    if [[ "${#REMAINING_OPTS[@]}" -gt 0 ]] ; then
+        error "Unknown options: ${REMAINING_OPTS[*]}"
+        echo >&2
+        usage >&2
+        exit 2
+    fi
+
+    if [[ "${#REMAINING_ARGS[@]}" -gt 0 ]] ; then
+        error "Invalid arguments: ${REMAINING_ARGS[*]}"
+        echo >&2
+        usage >&2
+        exit 2
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+check_preferences() {
+
+    info "Checking preferences ..."
+    local all_ok="y"
+
+    check_for_root
+
+    local -a tools=('rsync' 'md5sum' 'sha1sum' 'sha256sum')
+    local tool=
+    local folder=
+
+    for tool in "${tools[@]}" ; do
+        debug "Checking for '${CYAN}${tool}${NORMAL}' ..."
+        if type -p ${tool} >/dev/null ; then
+            :
+        else
+            all_ok="n"
+            error "Did not found '${RED}${tool}${NORMAL}'. Maybe not installed?"
+        fi
+    done
+
+    for folder in "${BACKUP_PARENTDIR}" "${BACKUP_HOSTDIR}" ; do
+        debug "Checking for directory '${CYAN}${folder}${NORMAL}' ..."
+        if [[ ! -d "${folder}" ]]; then
+            all_ok="n"
+            error "Did not found directory '${RED}${folder}${NORMAL}'."
+        fi
+    done
+
+    if [[ "${all_ok}" != "y" ]] ; then
+        exit 5
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+dir_backup() {
+
+    local bdir="${1}"
+    local tarball_base="${2}"
+    local tarball="${BACKUP_DIR}/${tarball_base}.tar.gz"
+
+    info "Backup of '${CYAN}/${bdir}${NORMAL}' => '${CYAN}${tarball}${NORMAL}'."
+    TAR "${tarball}" "${bdir}"
+
+}
+
+#------------------------------------------------------------------------------
+do_dir_backup() {
+
+    local odir=
+    local odir_base=
+
+    info "Starting Backup ..."
+
+    MKDIR -p "${BACKUP_DIR}"
+
+    cd /
+    dir_backup "etc" "etc"
+    dir_backup "root" "root"
+    dir_backup "var/log" "var-log"
+    dir_backup "var/spool/cron" "var-spool-cron"
+    if [[ -d "var/vmail" ]] ; then
+        dir_backup "var/vmail" "var-vmail"
+    fi
+
+    for odir in opt/* ; do
+        odir_base=$( basename "${odir}" )
+        if [[ "${odir_base}" == 'lost+found' ]] ; then
+            continue
+        fi
+        if [[ ! -d "${odir}" ]] ; then
+            continue
+        fi
+        dir_backup "${odir}" "opt.${odir_base}"
+    done
+
+    for odir in home/* ; do
+        odir_base=$( basename "${odir}" )
+        if [[ "${odir_base}" == 'lost+found' ]] ; then
+            continue
+        fi
+        if [[ "${odir_base}" == 'frank' ]] ; then
+            continue
+        fi
+        if [[ ! -d "${odir}" ]] ; then
+            continue
+        fi
+        dir_backup "${odir}" "home.${odir_base}"
+    done
+
+}
+
+#------------------------------------------------------------------------------
+do_mysql_backup(){
+
+    local dumpfile="${BACKUP_DIR}/mysql.dump.all-dbs.dump.sql.gz"
+    local errorlog="${BACKUP_DIR}/mysql.dump.error.log"
+
+    info "Dumping MySQL Datenbank -> '${CYAN}${dumpfile}${NORMAL}' ..."
+
+    local cmd="mysqldump --all_databases --routines --triggers --add-drop-database"
+    cmd+=" --add-drop-table --complete-insert --extended-insert --lock-tables"
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd+=" --verbose"
+    fi
+    cmd+=" --log-error=\"${errorlog}\""
+    cmd+=" | gzip -9 > \"${dumpfile}\""
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        info "Executing: ${cmd}"
+        return
+    fi
+    debug "Executing: ${cmd}"
+    eval ${cmd}
+    if [[ -e "${errorlog}" ]] ; then
+        if [[ -s "${errorlog}" ]] ; then
+            cmd="gzip -9 \"${errorlog}\""
+            if [[ "${VERBOSE}" == "y" ]] ; then
+                cmd="gzip -9 --verbose \"${errorlog}\""
+            fi
+            info "Executing: ${cmd}"
+            eval ${cmd}
+        else
+            debug "Removing empty '${CYAN}${errorlog}${NORMAL}' ..."
+            RM "${errorlog}"
+        fi
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+main() {
+
+    get_options "$@"
+    umask 0022
+
+    check_preferences
+    do_dir_backup
+    do_mysql_backup
+
+    info "Finished."
+}
+
+main "$@"
+
+exit 0
+
+# vim: ts=4 list