Zum Inhalt

Installation & Konfiguration

Borg & Borgmatic installieren

Borg

sudo apt update
sudo apt install borgbackup

# Version prüfen
borg --version

Borgmatic

Die apt-Version ist veraltet, daher via pipx:

# pipx installieren
sudo apt install pipx

# Borgmatic global installieren
sudo pipx install borgmatic --global

# Version prüfen
borgmatic --version

Erwartete Versionen:

  • Borg: 1.4.0+
  • Borgmatic: 1.8.0+

SSH-Key für Hetzner

Neuen SSH-Key erstellen

# Ed25519 Key generieren (ohne Passphrase für Automatisierung)
ssh-keygen -t ed25519 -f /root/.ssh/hetzner-borg -C "borgmatic-backup"

# Bei Passphrase-Abfrage einfach Enter drücken

Key auf Hetzner hochladen

# Methode 1: install-ssh-key (empfohlen)
cat /root/.ssh/hetzner-borg.pub | ssh -p23 u342251@u342251.your-storagebox.de install-ssh-key

# Methode 2: manuell
cat /root/.ssh/hetzner-borg.pub | ssh -p 23 u342251@u342251.your-storagebox.de \
    "mkdir -p .ssh && cat >> .ssh/authorized_keys"

Verbindung testen

ssh -i /root/.ssh/hetzner-borg -p 23 u342251@u342251.your-storagebox.de ls

Borg Repositories initialisieren

Lokales Repository

# Passphrase eingeben (wird nicht angezeigt)
read -s -p "Lokale Passphrase eingeben: " BORG_PASSPHRASE && export BORG_PASSPHRASE

# Repository initialisieren
borg init --encryption=repokey-blake2 /mnt/borg-repo

# Passphrase aus Umgebung entfernen
unset BORG_PASSPHRASE

Hetzner Repository

# Verzeichnis auf Hetzner erstellen
ssh -i /root/.ssh/hetzner-borg -p 23 u342251@u342251.your-storagebox.de mkdir -p backups/nuc

# Passphrase eingeben
read -s -p "Hetzner Passphrase eingeben: " BORG_PASSPHRASE && export BORG_PASSPHRASE

# Repository initialisieren
borg init --encryption=repokey-blake2 \
    ssh://u342251@u342251.your-storagebox.de:23/./backups/nuc

# Passphrase aus Umgebung entfernen
unset BORG_PASSPHRASE

Passphrases sicher aufbewahren

Ohne Passphrase ist keine Wiederherstellung möglich!

Borgmatic Konfiguration

Verzeichnisse erstellen

sudo mkdir -p /etc/borgmatic.d
sudo mkdir -p /root/backup-info

Lokale Konfiguration

Datei: /etc/borgmatic.d/local.yaml

# Borgmatic Config - Lokales Backup auf TR-004

source_directories:
    - /etc
    - /opt
    - /root
    - /home
    - /var
    - /mnt/ssd/container-data
    - /root/backup-info
    # UNAS - später hinzufügen:
    # - /mnt/unas/Dokumente
    # - /mnt/unas/Fotos

exclude_patterns:
    # Temporäre Dateien
    - '*.tmp'
    - '*.temp'
    - '*~'
    - '*.swp'
    - '*.swo'
    # Logs
    - '*.log'
    - '*.log.*'
    - '**/logs/*'
    - '**/log/*'
    # Cache-Verzeichnisse
    - '*/.cache'
    - '*/.cache/*'
    - '*/cache/*'
    - '**/Cache/*'
    - '**/cache/*'
    # Python
    - '*/__pycache__'
    - '*.pyc'
    - '*.pyo'
    - '*/.venv'
    - '*/venv/*'
    # Node.js
    - '**/node_modules'
    # Docker-spezifisch
    - '**/lost+found'
    # Lock und Socket Files
    - '*.lock'
    - '*.sock'
    - '*.socket'
    - '*.pid'
    # Datenbank Temp-Files
    - '**/ib_logfile*'
    - '**/ibdata*'
    - '**/*.db-wal'
    - '**/*.db-shm'
    # Backup-Verzeichnisse
    - '**/backup/*'
    - '**/backups/*'
    # Thumbnails
    - '**/.thumbnails'
    - '**/Thumbs.db'
    - '**/.DS_Store'

exclude_caches: true

exclude_if_present:
    - .nobackup

repositories:
    - path: /mnt/borg-repo
      label: local-backup

encryption_passphrase: "<PASSPHRASE_LOKAL>"

compression: auto,zstd,3

archive_name_format: 'nuc-{now:%Y-%m-%d_%H-%M}'

keep_daily: 7
keep_weekly: 4
keep_monthly: 12
keep_yearly: 2

checks:
    - name: repository
      frequency: 1 week
    - name: archives
      frequency: 2 weeks

retries: 3
retry_wait: 60

before_backup:
    - echo "$(date '+%Y-%m-%d %H:%M:%S') - Starte lokales Backup..."
    - dpkg-query -f '${binary:Package}\n' -W > /root/backup-info/packages_list.txt
    - docker ps -a --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}' > /root/backup-info/docker_containers.txt
    - docker volume ls > /root/backup-info/docker_volumes.txt
    - systemctl list-unit-files --state=enabled > /root/backup-info/systemd_enabled.txt
    - crontab -l > /root/backup-info/root_crontab.txt 2>/dev/null || true

after_backup:
    - echo "$(date '+%Y-%m-%d %H:%M:%S') - Lokales Backup erfolgreich abgeschlossen."

on_error:
    - echo "$(date '+%Y-%m-%d %H:%M:%S') - FEHLER beim lokalen Backup!"

Hetzner Konfiguration

Datei: /etc/borgmatic.d/hetzner.yaml

# Borgmatic Config - Offsite Backup zu Hetzner

source_directories:
    - /etc
    - /opt
    - /root
    - /home
    - /var
    - /mnt/ssd/container-data
    - /root/backup-info
    # UNAS kritische Daten - später hinzufügen:
    # - /mnt/unas/Dokumente
    # - /mnt/unas/Fotos/Familie

exclude_patterns:
    # Temporäre Dateien
    - '*.tmp'
    - '*.temp'
    - '*~'
    - '*.swp'
    - '*.swo'
    # Logs
    - '*.log'
    - '*.log.*'
    - '**/logs/*'
    - '**/log/*'
    # Cache-Verzeichnisse
    - '*/.cache'
    - '*/.cache/*'
    - '*/cache/*'
    - '**/Cache/*'
    - '**/cache/*'
    # Python
    - '*/__pycache__'
    - '*.pyc'
    - '*.pyo'
    - '*/.venv'
    - '*/venv/*'
    # Node.js
    - '**/node_modules'
    # Docker-spezifisch
    - '**/lost+found'
    # Lock und Socket Files
    - '*.lock'
    - '*.sock'
    - '*.socket'
    - '*.pid'
    # Datenbank Temp-Files
    - '**/ib_logfile*'
    - '**/ibdata*'
    - '**/*.db-wal'
    - '**/*.db-shm'
    # Backup-Verzeichnisse
    - '**/backup/*'
    - '**/backups/*'
    # Thumbnails
    - '**/.thumbnails'
    - '**/Thumbs.db'
    - '**/.DS_Store'

exclude_caches: true

exclude_if_present:
    - .nobackup

repositories:
    - path: ssh://u342251@u342251.your-storagebox.de:23/./backups/nuc
      label: hetzner-offsite

encryption_passphrase: "<PASSPHRASE_HETZNER>"

compression: auto,zstd,9

ssh_command: ssh -i /root/.ssh/hetzner-borg -p23

archive_name_format: 'nuc-{now:%Y-%m-%d_%H-%M}'

upload_rate_limit: 3000

keep_daily: 7
keep_weekly: 4
keep_monthly: 12
keep_yearly: 2

checks:
    - name: repository
      frequency: 2 weeks
    - name: archives
      frequency: 1 month

retries: 3
retry_wait: 60

before_backup:
    - echo "$(date '+%Y-%m-%d %H:%M:%S') - Starte Hetzner Backup..."
    - dpkg-query -f '${binary:Package}\n' -W > /root/backup-info/packages_list.txt
    - docker ps -a --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}' > /root/backup-info/docker_containers.txt
    - docker volume ls > /root/backup-info/docker_volumes.txt
    - systemctl list-unit-files --state=enabled > /root/backup-info/systemd_enabled.txt
    - crontab -l > /root/backup-info/root_crontab.txt 2>/dev/null || true

after_backup:
    - echo "$(date '+%Y-%m-%d %H:%M:%S') - Hetzner Backup erfolgreich abgeschlossen."

on_error:
    - echo "$(date '+%Y-%m-%d %H:%M:%S') - FEHLER beim Hetzner Backup!"

Konfiguration absichern

# Passphrases eintragen
sudo nano /etc/borgmatic.d/local.yaml
sudo nano /etc/borgmatic.d/hetzner.yaml

# Dateien absichern (nur root lesbar)
sudo chmod 600 /etc/borgmatic.d/*.yaml

# Konfiguration validieren
borgmatic config validate

Automatisierung mit Systemd

Service-Datei

Datei: /etc/systemd/system/borgmatic.service

[Unit]
Description=Borgmatic Backup
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot

# Niedrige Priorität um System nicht zu belasten
Nice=19
IOSchedulingClass=best-effort
IOSchedulingPriority=7

# Borgmatic führt alle Configs in /etc/borgmatic.d/ sequentiell aus:
# 1. hetzner.yaml (alphabetisch zuerst)
# 2. local.yaml
ExecStart=/root/.local/bin/borgmatic --verbosity -1 --syslog-verbosity 1

# Timeout für lange Backups (24h)
TimeoutStartSec=86400

# Logging
StandardOutput=journal
StandardError=journal
LogRateLimitIntervalSec=0

Timer-Datei

Datei: /etc/systemd/system/borgmatic.timer

[Unit]
Description=Borgmatic Backup Timer

[Timer]
# Täglich um 02:00 Uhr
OnCalendar=*-*-* 02:00:00

# Backup nachholen falls System um 02:00 aus war
Persistent=true

# Zufällige Verzögerung bis 15min
RandomizedDelaySec=15min

# Explizit den Service angeben
Unit=borgmatic.service

[Install]
WantedBy=timers.target

Timer aktivieren

# Systemd neu laden
sudo systemctl daemon-reload

# Falls maskiert, erst demaskieren
sudo systemctl unmask borgmatic.timer

# Timer aktivieren und starten
sudo systemctl enable borgmatic.timer
sudo systemctl start borgmatic.timer

# Status prüfen
sudo systemctl list-timers borgmatic.timer