Files
media-server/host_backup.sh
T
mattspeer 0e0d28b531 fix(backup): exclude snap unit files from systemd rsync
Excludes snap-generated mount files (`*snap*.mount`) and `snapd.mounts.target.wants`
from the `/etc/systemd/system` rsync backup task.

Prevents 'Invalid argument (22)' errors caused by escaped backslashes (`\x2d`)
in dynamic snap mount file names when syncing to non-POSIX destination filesystems.
2026-10-03 15:15:00 -05:00

140 lines
5.9 KiB
Bash

#!/bin/bash
# ==============================================================================
# Script: host_backup.sh
# Description: Securely backs up key host system configuration files (fstab,
# ddclient, crontabs, systemd units, and installed package lists)
# into timestamped directories on a mounted backup drive. Includes
# 30-day retention pruning and ntfy alerts.
# Usage: sudo ./host_backup.sh
# Notes: Requires sudo/root privileges to access system configuration files.
# ==============================================================================
# ==============================================================================
# Define Variables for Source, Destination, and Retention
# ==============================================================================
BACKUP_MOUNT_POINT="/mnt/5TB-Disk1"
HOST_BACKUP_BASE="$BACKUP_MOUNT_POINT/backup/server/host"
RETENTION_DAYS=30
# Timestamp and Directory Setup
TIMESTAMP=$(date +"%Y-%m-%d_%H%M%S")
CURRENT_BACKUP_DIR="$HOST_BACKUP_BASE/backup_$TIMESTAMP"
LATEST_TXT_FILE="$HOST_BACKUP_BASE/latest.txt"
# Host Backup Sources
FSTAB_SOURCE="/etc/fstab"
DDCLIENT_CONF_SOURCE="/etc/ddclient.conf"
DDCLIENT_DEFAULT_SOURCE="/etc/default/ddclient"
SYSTEMD_SOURCE="/etc/systemd/system"
# ntfy Notification Configuration
NTFY_TOPIC="Server"
NTFY_SERVER="https://ntfy.speerfam.net"
NTFY_TOKEN="tk_3xo9mejjtgemyowzckqw2souo3mux"
# ==============================================================================
# Pre-flight Checks
# ==============================================================================
# Check if the mount point directory exists and is a mounted filesystem.
if ! mountpoint -q "$BACKUP_MOUNT_POINT"; then
echo "Error: '$BACKUP_MOUNT_POINT' is not a mount point. Please ensure the drive is mounted." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Host backup failed. Destination '$BACKUP_MOUNT_POINT' is not mounted." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
# Create target subdirectories for current timestamp run
mkdir -p "$CURRENT_BACKUP_DIR/os"
mkdir -p "$CURRENT_BACKUP_DIR/services/ddclient"
mkdir -p "$CURRENT_BACKUP_DIR/cron"
mkdir -p "$CURRENT_BACKUP_DIR/systemd"
mkdir -p "$CURRENT_BACKUP_DIR/packages"
echo "Starting host configuration backup ($TIMESTAMP)..."
# ==============================================================================
# Task 1: Backup System Mounts (/etc/fstab)
# ==============================================================================
if [ -f "$FSTAB_SOURCE" ]; then
rsync -a "$FSTAB_SOURCE" "$CURRENT_BACKUP_DIR/os/"
echo "Backed up: $FSTAB_SOURCE"
else
echo "Warning: Source file '$FSTAB_SOURCE' not found." >&2
fi
# ==============================================================================
# Task 2: Backup DDNS Configurations (ddclient)
# ==============================================================================
if [ -f "$DDCLIENT_CONF_SOURCE" ]; then
rsync -a "$DDCLIENT_CONF_SOURCE" "$CURRENT_BACKUP_DIR/services/ddclient/"
echo "Backed up: $DDCLIENT_CONF_SOURCE"
else
echo "Warning: Source file '$DDCLIENT_CONF_SOURCE' not found." >&2
fi
if [ -f "$DDCLIENT_DEFAULT_SOURCE" ]; then
rsync -a "$DDCLIENT_DEFAULT_SOURCE" "$CURRENT_BACKUP_DIR/services/ddclient/"
echo "Backed up: $DDCLIENT_DEFAULT_SOURCE"
else
echo "Warning: Source file '$DDCLIENT_DEFAULT_SOURCE' not found." >&2
fi
# ==============================================================================
# Task 3: Export Cron Configurations
# ==============================================================================
# Export active user/root crontab
if crontab -l > /dev/null 2>&1; then
crontab -l > "$CURRENT_BACKUP_DIR/cron/root_crontab.txt"
echo "Exported: Root crontab"
fi
# Backup system-wide cron configuration files
if [ -f "/etc/crontab" ]; then
rsync -a /etc/crontab "$CURRENT_BACKUP_DIR/cron/crontab_system"
fi
if [ -d "/etc/cron.d" ]; then
rsync -aL /etc/cron.d "$CURRENT_BACKUP_DIR/cron/"
echo "Backed up: /etc/cron.d directory"
fi
# ==============================================================================
# Task 4: Backup Custom Systemd Units & Services
# ==============================================================================
if [ -d "$SYSTEMD_SOURCE" ]; then
rsync -aL \
--exclude='*snap*.mount' \
--exclude='snapd.mounts.target.wants' \
"$SYSTEMD_SOURCE/" "$CURRENT_BACKUP_DIR/systemd/"
echo "Backed up: $SYSTEMD_SOURCE"
else
echo "Warning: Systemd directory '$SYSTEMD_SOURCE' not found." >&2
fi
# ==============================================================================
# Task 5: Export Installed System Package Recovery Lists
# ==============================================================================
if command -v dpkg > /dev/null 2>&1; then
dpkg --get-selections > "$CURRENT_BACKUP_DIR/packages/dpkg_selections.txt"
echo "Exported: dpkg package selections"
fi
if command -v apt-mark > /dev/null 2>&1; then
apt-mark showmanual > "$CURRENT_BACKUP_DIR/packages/apt_manual_packages.txt"
echo "Exported: Manually installed apt package list"
fi
# ==============================================================================
# Task 6: Record Latest Pointer & Prune Old Backups
# ==============================================================================
echo "$CURRENT_BACKUP_DIR" > "$LATEST_TXT_FILE"
echo "Checking for backups older than $RETENTION_DAYS days..."
find "$HOST_BACKUP_BASE" -maxdepth 1 -type d -name "backup_*" -mtime +$RETENTION_DAYS -exec rm -rf {} +
# ==============================================================================
# Completion and Notification
# ==============================================================================
echo "Success: Host backup completed at '$CURRENT_BACKUP_DIR'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Host backup completed ($TIMESTAMP). Retaining $RETENTION_DAYS days." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1