miyagi-backup.sh aktualisiert

fix
This commit is contained in:
2025-05-29 23:58:55 +02:00
parent 6508b81ac2
commit 573707f0b9
+52 -33
View File
@@ -3,13 +3,6 @@ set -euo pipefail
IFS=$'\n\t' IFS=$'\n\t'
PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin" 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") SCRIPT_NAME=$(basename "$0")
LOGFILE="/var/log/${SCRIPT_NAME%.sh}.log" LOGFILE="/var/log/${SCRIPT_NAME%.sh}.log"
@@ -18,11 +11,14 @@ log() {
} }
usage() { 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 exit 1
} }
# Argument parsing # Konfigurationsdatei initialisieren (nur wenn -c übergeben)
CONFIG_FILE="" CONFIG_FILE=""
while getopts "c:" opt; do while getopts "c:" opt; do
case "$opt" in case "$opt" in
@@ -31,13 +27,15 @@ while getopts "c:" opt; do
esac esac
done done
if [[ -z "$CONFIG_FILE" || ! -f "$CONFIG_FILE" ]]; then # Bei Einzelaufruf muss Konfiguration geladen sein
log "ERROR: Configuration file is missing or invalid." if [[ -n "${CONFIG_FILE:-}" ]]; then
usage if [[ ! -f "$CONFIG_FILE" ]]; then
log "ERROR: Configuration file not found: $CONFIG_FILE"
exit 1
fi
source "$CONFIG_FILE"
fi fi
source "$CONFIG_FILE"
# Funktionen # Funktionen
write_zsync_config() { write_zsync_config() {
@@ -72,12 +70,16 @@ run_zsync() {
} }
run_updates() { run_updates() {
if [[ "$UPDATES" == "yes" ]]; then log "Running updates..."
log "Running system updates..."
apt update && apt dist-upgrade -y apt update && apt dist-upgrade -y
apt autopurge -y apt autopurge -y
}
run_remote_updates() {
if [[ "$UPDATES" == "yes" ]]; then
ssh "$PBSHOST" apt update && apt dist-upgrade -y
else else
log "No system updates configured" log "Remote updates disabled"
fi fi
} }
@@ -97,29 +99,45 @@ run_pbs_backup() {
--exclude "$BACKUPEXCLUDE" --mode snapshot --all 1 \ --exclude "$BACKUPEXCLUDE" --mode snapshot --all 1 \
--notes-template '{{guestname}}' --notes-template '{{guestname}}'
local status=$? if [[ $? -eq 0 ]]; then
if [[ $status -eq 0 ]]; then
echo "0 DailyPBS - Daily Backup" > /tmp/cmk_tmp.out echo "0 DailyPBS - Daily Backup" > /tmp/cmk_tmp.out
else else
echo "2 DailyPBS - Daily Backup FAILED" > /tmp/cmk_tmp.out echo "2 DailyPBS - Daily Backup FAILED" > /tmp/cmk_tmp.out
fi fi
( echo "<<<local>>>" ; cat /tmp/cmk_tmp.out ) > /tmp/90000_checkpbs ( echo "<<<local>>>" ; cat /tmp/cmk_tmp.out ) > /tmp/90000_checkpbs
scp /tmp/90000_checkpbs root@"$SOURCEHOST":/var/lib/check_mk_agent/spool scp /tmp/90000_checkpbs root@"$SOURCEHOST":/var/lib/check_mk_agent/spool
} }
run_maintenance() { run_maintenance() {
if [[ "$(date +%u)" == "$MAINTDAY" ]]; then 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) 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 prune-job run "$PRUNEJOB"
ssh root@"$PBSHOST" proxmox-backup-manager garbage-collection start "$BACKUPSTOREPBS" ssh root@"$PBSHOST" proxmox-backup-manager garbage-collection start "$BACKUPSTOREPBS"
ssh root@"$PBSHOST" proxmox-backup-manager verify backup
else 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 fi
} }
main() { main() {
log "Starting Miyagi backup process..." log "Starting full backup routine..."
SOURCEHOSTNAME=$(ssh "$SOURCEHOST" hostname) SOURCEHOSTNAME=$(ssh "$SOURCEHOST" hostname)
@@ -127,19 +145,20 @@ main() {
run_zsync run_zsync
run_updates run_updates
run_maintenance run_maintenance
run_scrub
run_pbs_backup run_pbs_backup
run_remote_updates
if [[ "$SHUTDOWN" == "yes" ]]; then shutdown_if_requested
send_piggyback_data
shutdown now
elif [[ "$BACKUPSERVER" == "no" ]]; then
log "No backup configured and no shutdown requested. Exiting."
exit 0
fi
} }
if [[ "${1:-}" == "" ]]; then # Funktionsbasierter Aufruf
main if [[ "${1:-}" == "help" ]]; then
else 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
"$@" "$@"
else
main
fi fi