From 6de645a2c3fb4800a2ce952459c3673d91f2b942 Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Sun, 29 Oct 2023 15:18:17 +0000 Subject: [PATCH] Remove backup-*. Add do-backup more generic script. --- backup-gv0 | 57 ---------------------------- backup-mirrors | 77 ------------------------------------- do-backup | 100 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 134 deletions(-) delete mode 100755 backup-gv0 delete mode 100755 backup-mirrors create mode 100755 do-backup diff --git a/backup-gv0 b/backup-gv0 deleted file mode 100755 index 45c8578..0000000 --- a/backup-gv0 +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/bash -# Version: 0.1.0 -# Copyright (c) 2023: -# Darren 'Tadgy' Austin -# Licensed under the terms of the GNU General Public License version 3. - -BACKUP_MOUNTPOINT="/localdata" -BACKUP_DESTDIR="$BACKUP_MOUNTPOINT/backups/gv0" -RSYNC_SOURCE="/storage/gv0/" # The / on the end is required. -RSYNC_FILTER=() -RSYNC_OPTIONS=( '-a' '-H' '-A' '--timeout=300' '--partial' '--partial-dir=.rsync-tmp' '--delete-delay' '--delay-updates' ) -RSYNC_OPTIONS_VERBOSE=( '--verbose' '--stats' '--human-readable' ) -RSYNC_LOG="/tmp/${BASH_SOURCE##*/}-$$.log" - -# Only allow one copy of the script to run at any time. -# shellcheck disable=SC2154 -if [[ "$FLOCK" != "$0" ]]; then - exec env FLOCK="$0" flock -e -n "$0" "$0" "$@" || { - printf "%s: %s\\n" "${BASH_SOURCE##*/}" "flock execution error" >&2 - exit 1 - } -fi - -# Source the mail configuration. -source /etc/mail.conf "backups" 2>/dev/null || { - printf "%s: %s\\n" "${BASH_SOURCE##*/}" "Failed to source /etc/mail.conf" >&2 - exit 1 -} - -# Make sure BACKUP_MOUNTPOINT is a mountpoint. -mountpoint "$BACKUP_MOUNTPOINT" >/dev/null 2>&1 || { - CONFIG_FILE="backups" /opt/bin/pushover -T "Backup" -p '-1' -m "Failure: $RSYNC_SOURCE" - mailx "${MAILX_ARGS[@]}" -S "from='$EMAIL_FROM'" -s "Backup failure: $RSYNC_SOURCE" "${EMAIL_TO[@]}" <<-EOF - '$BACKUP_MOUNTPOINT' is not a mountpoint. - EOF - exit 1 -} - -# Do the backup. -rsync "${RSYNC_OPTIONS[@]}" "${RSYNC_OPTIONS_VERBOSE[@]}" "${RSYNC_FILTER[@]}" "$RSYNC_SOURCE" "$BACKUP_DESTDIR" >"$RSYNC_LOG" 2>&1 -ERR="$?" - -# Send a notification and mail a log if there were errors. -(( ERR != 0 )) && (( ERR != 10 )) && (( ERR != 24 )) && { - CONFIG_FILE="backups" /opt/bin/pushover -T "Backup" -p '-1' -m "Failure: $RSYNC_SOURCE" - mailx "${MAILX_ARGS[@]}" -S "from='$EMAIL_FROM'" -s "Backup failure: $RSYNC_SOURCE" "${EMAIL_TO[@]}" <<-EOF - Exit code: $ERR - Output: - $(< "$RSYNC_LOG") - EOF - rm -f "$RSYNC_LOG" - exit 1 -} - -rm -f "$RSYNC_LOG" - -exit 0 diff --git a/backup-mirrors b/backup-mirrors deleted file mode 100755 index 455338c..0000000 --- a/backup-mirrors +++ /dev/null @@ -1,77 +0,0 @@ -#!/bin/bash -# Version: 0.1.0 -# Copyright (c) 2023: -# Darren 'Tadgy' Austin -# Licensed under the terms of the GNU General Public License version 3. - -BACKUP_MOUNTPOINT="/localdata" -BACKUP_DESTDIR="$BACKUP_MOUNTPOINT/backups/mirrors" -RSYNC_SOURCE="slackware.uk::mirrors/" # The / on the end is required. -RSYNC_FILTER=( \ - '--include=/absolute/***' \ - '--include=/csb/***' \ - '--include=/cumulative/***' \ - '--include=/freeslack/***' \ - '--include=/gfs/***' \ - '--include=/microlinux/***' \ - '--include=/msb/***' \ - '--include=/sarpi/***' \ - '--include=/slackonly/***' \ - '--include=/slackvirt/***' \ - '--include=/slackware/' \ - '--include=/slackware/slackware-pre-1.0-beta/***' \ - '--include=/slackware/slackware-1.01/***' \ - '--include=/slackwarearm/***' \ - '--include=/slacky/***' \ - '--include=/slaxbmc/***' \ - '--include=/slint/***' \ - '--include=/sls/***' \ - '--include=/studioware/***' \ - '--exclude=*' ) -RSYNC_OPTIONS=( '-a' '-H' '-A' '--contimeout=30' '--timeout=300' '--no-motd' '--partial' '--partial-dir=.rsync-tmp' '--delete-delay' '--delay-updates' ) -RSYNC_OPTIONS_VERBOSE=( '--verbose' '--stats' '--human-readable' ) -RSYNC_LOG="/tmp/${BASH_SOURCE##*/}-$$.log" - -# Only allow one copy of the script to run at any time. -# shellcheck disable=SC2154 -if [[ "$FLOCK" != "$0" ]]; then - exec env FLOCK="$0" flock -e -n "$0" "$0" "$@" || { - printf "%s: %s\\n" "${BASH_SOURCE##*/}" "flock execution error" >&2 - exit 1 - } -fi - -# Source the mail configuration. -source /etc/mail.conf "backups" 2>/dev/null || { - printf "%s: %s\\n" "${BASH_SOURCE##*/}" "Failed to source /etc/mail.conf" >&2 - exit 1 -} - -# Make sure BACKUP_MOUNTPOINT is a mountpoint. -mountpoint "$BACKUP_MOUNTPOINT" >/dev/null 2>&1 || { - CONFIG_FILE="backups" /opt/bin/pushover -T "Backup" -p '-1' -m "Failure: $RSYNC_SOURCE" - mailx "${MAILX_ARGS[@]}" -S "from='$EMAIL_FROM'" -s "Backup failure: $RSYNC_SOURCE" "${EMAIL_TO[@]}" <<-EOF - '$BACKUP_MOUNTPOINT' is not a mountpoint. - EOF - exit 1 -} - -# Do the backup. -rsync "${RSYNC_OPTIONS[@]}" "${RSYNC_OPTIONS_VERBOSE[@]}" "${RSYNC_FILTER[@]}" "$RSYNC_SOURCE" "$BACKUP_DESTDIR" >"$RSYNC_LOG" 2>&1 -ERR="$?" - -# Send a notification and mail a log if there were errors. -(( ERR != 0 )) && (( ERR != 10 )) && (( ERR != 24 )) && { - CONFIG_FILE="backups" /opt/bin/pushover -T "Backup" -p '-1' -m "Failure: $RSYNC_SOURCE" - mailx "${MAILX_ARGS[@]}" -S "from='$EMAIL_FROM'" -s "Backup failure: $RSYNC_SOURCE" "${EMAIL_TO[@]}" <<-EOF - Exit code: $ERR - Output: - $(< "$RSYNC_LOG") - EOF - rm -f "$RSYNC_LOG" - exit 1 -} - -rm -f "$RSYNC_LOG" - -exit 0 diff --git a/do-backup b/do-backup new file mode 100755 index 0000000..61487b2 --- /dev/null +++ b/do-backup @@ -0,0 +1,100 @@ +#!/bin/bash +# Version: 0.1.0 +# Copyright (c) 2023: +# Darren 'Tadgy' Austin +# Licensed under the terms of the GNU General Public License version 3. + +# Base configuration. +BACKUP_MOUNTPOINT="/localdata" +RSYNC_OPTIONS=( '-a' '-H' '-A' '--timeout=300' '--partial' '--partial-dir=.rsync-tmp' '--delete-delay' '--delay-updates' ) +RSYNC_OPTIONS_VERBOSE=( '--verbose' '--stats' '--human-readable' ) +RSYNC_LOG="/tmp/${BASH_SOURCE[0]##*/}-$$.log" + +# Sanity checks. +(( $# != 1 )) || [[ -z "$1" ]] || [[ "$1" =~ ^(-h|-help|--help)$ ]] && { + printf "%s: %s <%s>\\n" "Usage" "${BASH_SOURCE[0]}" "backup definition" >&2 + exit 1 +} + +# Get backup definition specific configuration. +case "$1" in + 'gv0') + BACKUP_DESTDIR="$BACKUP_MOUNTPOINT/backups/gv0" + RSYNC_SOURCE="/storage/gv0/" # The / on the end is required. + RSYNC_FILTER=() + ;; + 'mirrors') + BACKUP_DESTDIR="$BACKUP_MOUNTPOINT/backups/mirrors" + RSYNC_SOURCE="slackware.uk::mirrors/" # The / on the end is required. + RSYNC_FILTER=( \ + '--include=/absolute/***' \ + '--include=/csb/***' \ + '--include=/cumulative/***' \ + '--include=/freeslack/***' \ + '--include=/gfs/***' \ + '--include=/microlinux/***' \ + '--include=/msb/***' \ + '--include=/sarpi/***' \ + '--include=/slackonly/***' \ + '--include=/slackvirt/***' \ + '--include=/slackware/' \ + '--include=/slackware/slackware-pre-1.0-beta/***' \ + '--include=/slackware/slackware-1.01/***' \ + '--include=/slackwarearm/***' \ + '--include=/slacky/***' \ + '--include=/slaxbmc/***' \ + '--include=/slint/***' \ + '--include=/sls/***' \ + '--include=/studioware/***' \ + '--exclude=*' ) + RSYNC_OPTIONS+=( '--contimeout=30' ) + ;; + *) + printf "%s: %s: %s\\n" "${BASH_SOURCE[0]##*/}" "$1" "unknown backup definition" >&2 + exit 1 + ;; +esac + +# Only allow one copy of the script to run at any time. +# shellcheck disable=SC2154 +if [[ "$FLOCK" != "$0" ]]; then + exec env FLOCK="$0" flock -e -n "$0" "$0" "$@" || { + printf "%s: %s\\n" "${BASH_SOURCE[0]##*/}" "flock execution error" >&2 + exit 1 + } +fi + +# Source the mail configuration. +source /etc/mail.conf "backups" 2>/dev/null || { + printf "%s: %s\\n" "${BASH_SOURCE[0]##*/}" "Failed to source /etc/mail.conf" >&2 + exit 1 +} + +# Make sure BACKUP_MOUNTPOINT is a mountpoint. +mountpoint "$BACKUP_MOUNTPOINT" >/dev/null 2>&1 || { + CONFIG_FILE="backups" /opt/bin/pushover -T "Backup" -p '-1' -m "Failure: $RSYNC_SOURCE" + mailx "${MAILX_ARGS[@]}" -S "from='$EMAIL_FROM'" -s "Backup failure: $RSYNC_SOURCE" "${EMAIL_TO[@]}" <<-EOF + '$BACKUP_MOUNTPOINT' is not a mountpoint. + EOF + exit 1 +} + +# Do the backup. +rsync "${RSYNC_OPTIONS[@]}" "${RSYNC_OPTIONS_VERBOSE[@]}" "${RSYNC_FILTER[@]}" "$RSYNC_SOURCE" "$BACKUP_DESTDIR" >"$RSYNC_LOG" 2>&1 +ERR="$?" + +# Send a notification and mail a log if there were errors. +(( ERR != 0 )) && (( ERR != 10 )) && (( ERR != 24 )) && { + CONFIG_FILE="backups" /opt/bin/pushover -T "Backup" -p '-1' -m "Failure: $RSYNC_SOURCE" + mailx "${MAILX_ARGS[@]}" -S "from='$EMAIL_FROM'" -s "Backup failure: $RSYNC_SOURCE" "${EMAIL_TO[@]}" <<-EOF + Exit code: $ERR + Output: + $(< "$RSYNC_LOG") + EOF + rm -f "$RSYNC_LOG" + exit 1 +} + +rm -f "$RSYNC_LOG" + +exit 0