Files
datensicherung_rsync/netzwerk-sicherung.sh
T
2026-04-28 11:22:40 +02:00

89 lines
2.2 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -Eeuo pipefail
DEST_BASE="/run/media/hans/usbsicherung/system-config-backup"
HOST="$(hostname -s)"
STAMP="$(date +%F_%H-%M-%S)"
TARGET_DIR="${DEST_BASE}/${HOST}/${STAMP}"
LATEST_LINK="${DEST_BASE}/${HOST}/latest"
LOG_DIR="${DEST_BASE}/${HOST}/logs"
LOG_FILE="${LOG_DIR}/netzwerk-backup-${STAMP}.log"
KEEP=20
WG_SRC="/etc/wireguard"
NM_SRC="/etc/NetworkManager/system-connections"
log() {
local msg="[$(date +%F\ %T)] $*"
echo "${msg}"
mkdir -p "${LOG_DIR}"
echo "${msg}" >> "${LOG_FILE}"
}
fail() {
log "FEHLER: $*"
exit 1
}
if [[ ${EUID} -ne 0 ]]; then
fail "Dieses Skript muss mit sudo oder als root ausgeführt werden, da /etc/wireguard und /etc/NetworkManager/system-connections Root-Rechte benötigen."
fi
if [[ ! -d "${DEST_BASE}" ]]; then
fail "Zielbasis ${DEST_BASE} existiert nicht oder ist nicht gemountet."
fi
if [[ ! -w "${DEST_BASE}" ]]; then
fail "Zielbasis ${DEST_BASE} ist nicht beschreibbar."
fi
mkdir -p "${TARGET_DIR}" "${LOG_DIR}"
RSYNC_OPTS=(
-aHAX
--numeric-ids
--delete
--info=stats2,progress2
--human-readable
--partial
)
if [[ -L "${LATEST_LINK}" ]] && [[ -d "$(readlink -f "${LATEST_LINK}")" ]]; then
RSYNC_OPTS+=(--link-dest="$(readlink -f "${LATEST_LINK}")")
log "Verwende link-dest: $(readlink -f "${LATEST_LINK}")"
else
log "Kein vorheriger Snapshot gefunden es wird ein vollständiger Lauf erstellt."
fi
backup_tree() {
local src="$1"
local name="$2"
if [[ -d "${src}" ]]; then
log "Sichere ${src} -> ${TARGET_DIR}/${name}/"
rsync "${RSYNC_OPTS[@]}" "${src}/" "${TARGET_DIR}/${name}/" | tee -a "${LOG_FILE}"
else
log "Quelle ${src} existiert nicht wird übersprungen."
fi
}
backup_tree "${WG_SRC}" "wireguard"
backup_tree "${NM_SRC}" "networkmanager-system-connections"
ln -sfn "${TARGET_DIR}" "${LATEST_LINK}"
log "latest-Link zeigt jetzt auf ${TARGET_DIR}"
cd "${DEST_BASE}/${HOST}"
SNAPS=(20*/)
if (( ${#SNAPS[@]} > KEEP )); then
log "Rotationslauf: ${#SNAPS[@]} Snapshots vorhanden, KEEP=${KEEP}."
ls -1dt 20*/ | tail -n +$((KEEP + 1)) | while read -r old; do
log "Lösche alten Snapshot: ${old}"
rm -rf -- "${old}"
done
else
log "Keine Rotation nötig. Snapshots vorhanden: ${#SNAPS[@]}"
fi
log "Netzwerk-Konfigurationsbackup abgeschlossen."