diff --git a/miyagi-backup.sh b/miyagi-backup.sh index c564470..870f308 100644 --- a/miyagi-backup.sh +++ b/miyagi-backup.sh @@ -3,13 +3,6 @@ set -euo pipefail IFS=$'\n\t' PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin" - -if [[ "${1:-}" == "help" ]]; then - echo "Verfügbare Funktionen:" - declare -F | awk '{print " - " $3}' - exit 0 -fi - SCRIPT_NAME=$(basename "$0") LOGFILE="/var/log/${SCRIPT_NAME%.sh}.log" @@ -18,11 +11,14 @@ log() { } usage() { - echo "Usage: $0 -c /path/to/config" + echo "Usage:" + echo " $0 -c /path/to/config # Full backup run" + echo " $0 [function] # Run individual function" + echo " $0 help # Show available functions" exit 1 } -# Argument parsing +# Konfigurationsdatei initialisieren (nur wenn -c übergeben) CONFIG_FILE="" while getopts "c:" opt; do case "$opt" in @@ -31,13 +27,15 @@ while getopts "c:" opt; do esac done -if [[ -z "$CONFIG_FILE" || ! -f "$CONFIG_FILE" ]]; then - log "ERROR: Configuration file is missing or invalid." - usage +# Bei Einzelaufruf muss Konfiguration geladen sein +if [[ -n "${CONFIG_FILE:-}" ]]; then + if [[ ! -f "$CONFIG_FILE" ]]; then + log "ERROR: Configuration file not found: $CONFIG_FILE" + exit 1 + fi + source "$CONFIG_FILE" fi -source "$CONFIG_FILE" - # Funktionen write_zsync_config() { @@ -72,12 +70,16 @@ run_zsync() { } run_updates() { + log "Running updates..." + apt update && apt dist-upgrade -y + apt autopurge -y +} + +run_remote_updates() { if [[ "$UPDATES" == "yes" ]]; then - log "Running system updates..." - apt update && apt dist-upgrade -y - apt autopurge -y + ssh "$PBSHOST" apt update && apt dist-upgrade -y else - log "No system updates configured" + log "Remote updates disabled" fi } @@ -97,29 +99,45 @@ run_pbs_backup() { --exclude "$BACKUPEXCLUDE" --mode snapshot --all 1 \ --notes-template '{{guestname}}' - local status=$? - if [[ $status -eq 0 ]]; then + if [[ $? -eq 0 ]]; then echo "0 DailyPBS - Daily Backup" > /tmp/cmk_tmp.out else echo "2 DailyPBS - Daily Backup FAILED" > /tmp/cmk_tmp.out fi + ( echo "<<>>" ; cat /tmp/cmk_tmp.out ) > /tmp/90000_checkpbs scp /tmp/90000_checkpbs root@"$SOURCEHOST":/var/lib/check_mk_agent/spool } run_maintenance() { if [[ "$(date +%u)" == "$MAINTDAY" ]]; then - log "Running maintenance tasks..." + log "Running maintenance..." PRUNEJOB=$(ssh "$PBSHOST" proxmox-backup-manager prune-job list --output-format json-pretty | grep -m 1 "id" | cut -d'"' -f4) ssh root@"$PBSHOST" proxmox-backup-manager prune-job run "$PRUNEJOB" ssh root@"$PBSHOST" proxmox-backup-manager garbage-collection start "$BACKUPSTOREPBS" + ssh root@"$PBSHOST" proxmox-backup-manager verify backup else - log "No maintenance today." + log "No maintenance scheduled for today." + fi +} + +run_scrub() { + ssh root@"$SOURCEHOST" zpool scrub -s "$ZPOOLSRC" + zpool scrub -s "$ZPOOLDST" +} + +shutdown_if_requested() { + if [[ "$SHUTDOWN" == "yes" ]]; then + send_piggyback_data + log "Shutting down now..." + shutdown now + else + log "No shutdown requested." fi } main() { - log "Starting Miyagi backup process..." + log "Starting full backup routine..." SOURCEHOSTNAME=$(ssh "$SOURCEHOST" hostname) @@ -127,19 +145,20 @@ main() { run_zsync run_updates run_maintenance + run_scrub run_pbs_backup - - if [[ "$SHUTDOWN" == "yes" ]]; then - send_piggyback_data - shutdown now - elif [[ "$BACKUPSERVER" == "no" ]]; then - log "No backup configured and no shutdown requested. Exiting." - exit 0 - fi + run_remote_updates + shutdown_if_requested } -if [[ "${1:-}" == "" ]]; then - main -else +# Funktionsbasierter Aufruf +if [[ "${1:-}" == "help" ]]; then + echo "Verfügbare Funktionen:" + declare -F | awk '{print " - " $3}' | grep -v "^ - _" + exit 0 +elif [[ "${1:-}" =~ ^[a-zA-Z0-9_]+$ && "$(type -t "$1")" == "function" ]]; then + shift "$@" -fi \ No newline at end of file +else + main +fi