Files
backup-configs/root/backup-config/backup-config.sh
Michael Seidel 31cb542d44 Config-Backup 2026-03-28 18:29:56
Geänderte Dateien:
 - etc/systemd/system/system/chronyd.service
 - etc/systemd/system/system/cloud-config.target.wants/cloud-init-hotplugd.socket
 - etc/systemd/system/system/cloud-init.target.wants/cloud-config.service
 - etc/systemd/system/system/cloud-init.target.wants/cloud-final.service
 - etc/systemd/system/system/cloud-init.target.wants/cloud-init-local.service
 - etc/systemd/system/system/cloud-init.target.wants/cloud-init-main.service
 - etc/systemd/system/system/cloud-init.target.wants/cloud-init-network.service
 - etc/systemd/system/system/dbus-org.freedesktop.timesync1.service
 - etc/systemd/system/system/default.target.wants/wtmpdb-update-boot.service
 - etc/systemd/system/system/getty.target.wants/getty@tty1.service
 - etc/systemd/system/system/hibernate.target.wants/grub-common.service
 - etc/systemd/system/system/hybrid-sleep.target.wants/grub-common.service
 - etc/systemd/system/system/multi-user.target.wants/chrony.service
 - etc/systemd/system/system/multi-user.target.wants/console-setup.service
 - etc/systemd/system/system/multi-user.target.wants/cron.service
 - etc/systemd/system/system/multi-user.target.wants/e2scrub_reap.service
 - etc/systemd/system/system/multi-user.target.wants/fail2ban.service
 - etc/systemd/system/system/multi-user.target.wants/grub-common.service
 - etc/systemd/system/system/multi-user.target.wants/networking.service
 - etc/systemd/system/system/multi-user.target.wants/nmbd.service
 - etc/systemd/system/system/multi-user.target.wants/oddjobd.service
 - etc/systemd/system/system/multi-user.target.wants/remote-fs.target
 - etc/systemd/system/system/multi-user.target.wants/rsyslog.service
 - etc/systemd/system/system/multi-user.target.wants/samba-ad-dc.service
 - etc/systemd/system/system/multi-user.target.wants/smbd.service
 - etc/systemd/system/system/multi-user.target.wants/ssh.service
 - etc/systemd/system/system/multi-user.target.wants/sssd.service
 - etc/systemd/system/system/multi-user.target.wants/unattended-upgrades.service
 - etc/systemd/system/system/multi-user.target.wants/winbind.service
 - etc/systemd/system/system/multi-user.target.wants/zfs.target
 - etc/systemd/system/system/network-online.target.wants/networking.service
 - etc/systemd/system/system/nmb.service
 - etc/systemd/system/system/samba.service
 - etc/systemd/system/system/smb.service
 - etc/systemd/system/system/ssh.service.wants/sshd-keygen.service
 - etc/systemd/system/system/ssh.socket.wants/sshd-keygen.service
 - etc/systemd/system/system/sshd.service
 - etc/systemd/system/system/sshd.service.wants/sshd-keygen.service
 - etc/systemd/system/system/sshd@.service.wants/sshd-keygen.service
 - etc/systemd/system/system/sssd.service.wants/sssd-autofs.socket
 - etc/systemd/system/system/sssd.service.wants/sssd-nss.socket
 - etc/systemd/system/system/sssd.service.wants/sssd-pac.socket
 - etc/systemd/system/system/sssd.service.wants/sssd-pam.socket
 - etc/systemd/system/system/sssd.service.wants/sssd-ssh.socket
 - etc/systemd/system/system/sssd.service.wants/sssd-sudo.socket
 - etc/systemd/system/system/suspend-then-hibernate.target.wants/grub-common.service
 - etc/systemd/system/system/suspend.target.wants/grub-common.service
 - etc/systemd/system/system/sysinit.target.wants/apparmor.service
 - etc/systemd/system/system/sysinit.target.wants/keyboard-setup.service
 - etc/systemd/system/system/sysinit.target.wants/systemd-pstore.service
 - etc/systemd/system/system/sysinit.target.wants/systemd-timesyncd.service
 - etc/systemd/system/system/syslog.service
 - etc/systemd/system/system/timers.target.wants/apt-daily-upgrade.timer
 - etc/systemd/system/system/timers.target.wants/apt-daily.timer
 - etc/systemd/system/system/timers.target.wants/dpkg-db-backup.timer
 - etc/systemd/system/system/timers.target.wants/e2scrub_all.timer
 - etc/systemd/system/system/timers.target.wants/fstrim.timer
 - etc/systemd/system/system/timers.target.wants/logrotate.timer
 - etc/systemd/system/system/timers.target.wants/man-db.timer
 - etc/systemd/system/system/zed.service
 - etc/systemd/system/system/zfs-import.target.wants/zfs-import-cache.service
 - etc/systemd/system/system/zfs-mount.service.wants/zfs-load-module.service
 - etc/systemd/system/system/zfs-volumes.target.wants/zfs-volume-wait.service
 - etc/systemd/system/system/zfs.target.wants/zfs-import.target
 - etc/systemd/system/system/zfs.target.wants/zfs-load-module.service
 - etc/systemd/system/system/zfs.target.wants/zfs-mount.service
 - etc/systemd/system/system/zfs.target.wants/zfs-share.service
 - etc/systemd/system/system/zfs.target.wants/zfs-volumes.target
 - etc/systemd/system/system/zfs.target.wants/zfs-zed.service
 - root/backup-config/backup-config.sh
 - root/backup-config/run-backup-and-push.sh
2026-03-28 18:29:56 +01:00

268 lines
5.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
########################################
# Einstellungen
########################################
SOURCE_LIST="/root/backup-config/sources.txt"
BACKUP_ROOT="/root/backup-config/backup"
LOGFILE="/var/log/backup-config.log"
# Git
USE_GIT="yes"
GIT_BRANCH="File1_Configfiles"
# Verzeichnisse, die automatisch nach Config-Dateien durchsucht werden
AUTO_SCAN_DIRS=(
"/root/docker"
)
# Dateitypen, die automatisch erkannt werden
AUTO_FILE_TYPES=(
"*.sh"
"*.yml"
"*.yaml"
"*.env"
"*.conf"
"*.cnf"
"*.ini"
"*.json"
"*.toml"
"*.service"
"Dockerfile"
"docker-compose.yml"
"compose.yml"
)
# Pfade, die beim automatischen Scan ausgeschlossen werden
EXCLUDES=(
"*/data/pages/*"
"*/data/media/*"
"*/data/cache/*"
"*/data/tmp/*"
"*/logs/*"
"*/log/*"
"*/db/*"
"*/database/*"
"*/mysql/*"
"*/mariadb/*"
"*/postgres/*"
"*/redis/*"
"*/.git/*"
)
########################################
# Logging
########################################
log() {
printf '%s %s\n' "$(date '+%F %T')" "$1" | tee -a "$LOGFILE"
}
########################################
# Hilfsfunktionen
########################################
is_excluded() {
local path="$1"
local ex
for ex in "${EXCLUDES[@]}"; do
if [[ "$path" == $ex ]]; then
return 0
fi
done
return 1
}
copy_path() {
local src="$1"
local rel
local dst
if is_excluded "$src"; then
log "SKIP (exclude): $src"
return 0
fi
if [[ -f "$src" ]]; then
rel="${src#/}"
dst="$BACKUP_ROOT/$rel"
mkdir -p "$(dirname "$dst")"
cp -a "$src" "$dst"
log "OK (FILE): $src -> $dst"
return 0
fi
if [[ -d "$src" ]]; then
rel="${src#/}"
dst="$BACKUP_ROOT/$rel"
mkdir -p "$(dirname "$dst")"
cp -a "$src" "$dst"
log "OK (DIR): $src -> $dst"
return 0
fi
log "WARN: Pfad nicht gefunden: $src"
return 1
}
########################################
# Automatischer Scan
########################################
auto_scan() {
local dir
local file
local find_expr=()
log "INFO: Starte automatischen Scan"
# find-Ausdruck für Dateitypen bauen
for pattern in "${AUTO_FILE_TYPES[@]}"; do
find_expr+=( -name "$pattern" -o )
done
unset 'find_expr[${#find_expr[@]}-1]'
for dir in "${AUTO_SCAN_DIRS[@]}"; do
if [[ ! -d "$dir" ]]; then
log "WARN: Auto-Scan-Verzeichnis fehlt: $dir"
continue
fi
while IFS= read -r file; do
if is_excluded "$file"; then
log "SKIP (exclude): $file"
continue
fi
copy_path "$file" || true
done < <(
find "$dir" -type f \( "${find_expr[@]}" \) 2>/dev/null | sort -u
)
done
}
########################################
# Git
########################################
git_prepare_repo() {
if [[ "$USE_GIT" != "yes" ]]; then
return 0
fi
if ! command -v git >/dev/null 2>&1; then
log "ERROR: Git ist nicht installiert"
return 1
fi
mkdir -p "$BACKUP_ROOT"
if [[ ! -d "$BACKUP_ROOT/.git" ]]; then
log "INFO: Git-Repository wird initialisiert"
git -C "$BACKUP_ROOT" init
fi
# Benutzerinfo prüfen
if ! git -C "$BACKUP_ROOT" config user.name >/dev/null; then
log "WARN: Git user.name ist im Repository nicht gesetzt"
fi
if ! git -C "$BACKUP_ROOT" config user.email >/dev/null; then
log "WARN: Git user.email ist im Repository nicht gesetzt"
fi
}
git_checkout_branch() {
local branch="$1"
if [[ "$USE_GIT" != "yes" ]]; then
return 0
fi
if [[ -z "$branch" ]]; then
return 0
fi
if git -C "$BACKUP_ROOT" show-ref --verify --quiet "refs/heads/$branch"; then
git -C "$BACKUP_ROOT" checkout "$branch" >/dev/null 2>&1
log "INFO: Git-Branch aktiviert: $branch"
return 0
fi
# Wenn noch gar kein Commit existiert, zuerst initialen Commit anlegen
if ! git -C "$BACKUP_ROOT" rev-parse --verify HEAD >/dev/null 2>&1; then
git -C "$BACKUP_ROOT" add -A
if ! git -C "$BACKUP_ROOT" diff --cached --quiet; then
git -C "$BACKUP_ROOT" commit -m "Initial backup import"
log "INFO: Initialer Commit erstellt"
fi
fi
git -C "$BACKUP_ROOT" checkout -b "$branch" >/dev/null 2>&1
log "INFO: Git-Branch erstellt und aktiviert: $branch"
}
git_commit_changes() {
local changed_files
local commit_msg
if [[ "$USE_GIT" != "yes" ]]; then
return 0
fi
git -C "$BACKUP_ROOT" add -A
if git -C "$BACKUP_ROOT" diff --cached --quiet; then
log "INFO: Keine Änderungen für Git"
return 0
fi
changed_files="$(git -C "$BACKUP_ROOT" diff --cached --name-only | sed 's#^# - #' || true)"
commit_msg=$(
cat <<EOF
Config-Backup $(date '+%F %T')
Geänderte Dateien:
$changed_files
EOF
)
git -C "$BACKUP_ROOT" commit -m "$commit_msg"
log "OK: Git-Commit erstellt"
}
########################################
# Hauptlogik
########################################
main() {
mkdir -p "$BACKUP_ROOT"
if [[ -f "$SOURCE_LIST" ]]; then
log "INFO: Lese Source-Liste: $SOURCE_LIST"
while IFS= read -r line || [[ -n "$line" ]]; do
[[ -z "${line// }" ]] && continue
[[ "$line" =~ ^[[:space:]]*# ]] && continue
copy_path "$line" || true
done < "$SOURCE_LIST"
else
log "WARN: Source-Liste fehlt: $SOURCE_LIST"
fi
auto_scan
git_prepare_repo
git_checkout_branch "$GIT_BRANCH"
git_commit_changes
log "INFO: Lauf abgeschlossen"
}
main