Backup-Konfiguration hinzugefügt: Service, Timer und Skript
This commit is contained in:
commit
43677ee89a
209
Readme.md
Normal file
209
Readme.md
Normal file
@ -0,0 +1,209 @@
|
||||
# Restic System Backup – Konfiguration & Wiederherstellung
|
||||
|
||||
Dieses Repository enthält die Konfiguration für automatisierte System-Backups mit **Restic**, inklusive **systemd-Service**, **Timer** und **Backup-Skript**.
|
||||
|
||||
---
|
||||
|
||||
## 1. Voraussetzungen installieren
|
||||
|
||||
### 1.1 Pakete installieren
|
||||
Führe folgende Befehle **als Root** aus:
|
||||
|
||||
```bash
|
||||
pacman -S restic rclone fuse jq curl
|
||||
|
||||
|
||||
1.2 rclone als Root konfigurieren
|
||||
Da Restic das Repository über rclone anspricht, muss rclone als Root konfiguriert werden:
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
sudo -i
|
||||
rclone config
|
||||
|
||||
|
||||
Folge den Anweisungen, um dein Cloud-Storage (z. B. Cloud.Michaelis.Digital) einzurichten.
|
||||
Wichtig: Teste die Verbindung mit:
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
rclone lsd Cloud.Michaelis.Digital:
|
||||
|
||||
|
||||
|
||||
2. Repository klonen und Dateien ablegen
|
||||
2.1 Repository klonen
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
git clone <DEINE-GIT-URL> /opt/backup-config
|
||||
cd /opt/backup-config
|
||||
|
||||
2.2 Dateien kopieren
|
||||
Kopiere die Konfigurationsdateien an die richtigen Orte:
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
# Service und Timer
|
||||
cp restic-backup.service /etc/systemd/system/
|
||||
cp restic-backup.timer /etc/systemd/system/
|
||||
|
||||
# Backup-Skript
|
||||
cp restic-backup /usr/local/bin/
|
||||
chmod +x /usr/local/bin/restic-backup
|
||||
|
||||
|
||||
3. Secrets anlegen
|
||||
3.1 Restic-Passwortdatei
|
||||
Erstelle die Passwortdatei für Restic (nur für Root lesbar!):
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
mkdir -p /home/ratatoskr
|
||||
echo "DEIN_RESTIC_REPO_PASSWORT" > /home/ratatoskr/.restic-pass
|
||||
chmod 600 /home/ratatoskr/.restic-pass
|
||||
chown root\:root /home/ratatoskr/.restic-pass
|
||||
|
||||
3.2 Umgebungsvariablen (optional)
|
||||
Falls du die Umgebungsvariablen global setzen willst (z. B. für manuelle Backups), füge folgende Zeilen zu /etc/environment hinzu:
|
||||
ini
|
||||
Kopieren
|
||||
|
||||
RESTIC_REPOSITORY=rclone\:Cloud.Michaelis.Digital\:backup-repo
|
||||
RESTIC_PASSWORD_FILE=/home/ratatoskr/.restic-pass
|
||||
|
||||
Lade die Variablen neu:
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
source /etc/environment
|
||||
|
||||
|
||||
4. systemd aktivieren
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
systemctl daemon-reload
|
||||
systemctl enable --now restic-backup.timer
|
||||
systemctl enable restic-backup.service
|
||||
|
||||
|
||||
Status prüfen:
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
systemctl status restic-backup.timer
|
||||
journalctl -u restic-backup.service -f
|
||||
|
||||
|
||||
|
||||
5. Backup-Repository initialisieren
|
||||
Falls das Repository noch nicht existiert:
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
restic -r rclone\:Cloud.Michaelis.Digital\:backup-repo init
|
||||
|
||||
|
||||
6. Snapshots mounten (bei Bedarf)
|
||||
6.1 Mount-Punkt erstellen
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
mkdir -p /mnt/restic
|
||||
|
||||
6.2 Snapshot mounten (nur bei Bedarf)
|
||||
|
||||
Umgebungsvariablen setzen:
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
export RESTIC_REPOSITORY=rclone\:Cloud.Michaelis.Digital\:backup-repo
|
||||
export RESTIC_PASSWORD_FILE=/home/ratatoskr/.restic-pass
|
||||
|
||||
|
||||
Snapshot suchen und mounten:
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
restic -r \$RESTIC_REPOSITORY --password-file \$RESTIC_PASSWORD_FILE snapshots
|
||||
restic -r \$RESTIC_REPOSITORY --password-file \$RESTIC_PASSWORD_FILE mount /mnt/restic-snapshot/
|
||||
|
||||
Hinweis: Das Mounten erfolgt über FUSE und ist lazy – Daten werden erst bei Zugriff geladen.
|
||||
6.3 Unmounten
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
fusermount -u /mnt/restic-snapshot
|
||||
|
||||
|
||||
7. Wiederherstellung von Dateien
|
||||
7.1 Einzelne Dateien wiederherstellen
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
restic -r rclone\:Cloud.Michaelis.Digital\:backup-repo restore <SNAPSHOT_ID> --target /tmp/restore
|
||||
|
||||
7.2 Vollständiges System wiederherstellen
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
restic -r rclone\:Cloud.Michaelis.Digital\:backup-repo restore <SNAPSHOT_ID> --target /
|
||||
|
||||
|
||||
8. Wichtige Hinweise
|
||||
|
||||
rclone als Root: Restic benötigt Root-Rechte für rclone, da es auf / zugreift.
|
||||
FUSE: Wird automatisch von Restic für das Mounten verwendet.
|
||||
Logs: Backup-Logs findest du in /var/log/restic-backup.log und im Journal (journalctl -u restic-backup.service).
|
||||
Benachrichtigungen: Das Skript sendet Updates an ntfy.michaelis.digital. Passe die URL und Credentials im Skript an.
|
||||
|
||||
9. Zusammenfassung der Befehle (Copy&Paste-freundlich)
|
||||
bash
|
||||
Kopieren
|
||||
|
||||
# 1. Pakete installieren
|
||||
pacman -S restic rclone fuse jq curl
|
||||
|
||||
# 2. rclone als Root konfigurieren
|
||||
sudo -i
|
||||
rclone config
|
||||
|
||||
# 3. Repository klonen und Dateien ablegen
|
||||
git clone <DEINE-GIT-URL> /opt/backup-config
|
||||
cd /opt/backup-config
|
||||
cp restic-backup.service /etc/systemd/system/
|
||||
cp restic-backup.timer /etc/systemd/system/
|
||||
cp restic-backup /usr/local/bin/
|
||||
chmod +x /usr/local/bin/restic-backup
|
||||
|
||||
# 4. Secrets anlegen
|
||||
mkdir -p /home/ratatoskr
|
||||
echo "DEIN_RESTIC_REPO_PASSWORT" > /home/ratatoskr/.restic-pass
|
||||
chmod 600 /home/ratatoskr/.restic-pass
|
||||
chown root\:root /home/ratatoskr/.restic-pass
|
||||
|
||||
# 5. systemd aktivieren
|
||||
systemctl daemon-reload
|
||||
systemctl enable --now restic-backup.timer
|
||||
systemctl enable restic-backup.service
|
||||
|
||||
# 6. Repository initialisieren (falls neu)
|
||||
restic -r rclone\:Cloud.Michaelis.Digital\:backup-repo init
|
||||
|
||||
|
||||
Hinweise zur Nutzung:
|
||||
|
||||
Ersetze <DEINE-GIT-URL> mit der tatsächlichen URL deines GTA-Repositories.
|
||||
Ersetze DEIN_RESTIC_REPO_PASSWORT mit dem tatsächlichen Passwort für dein Restic-Repository.
|
||||
Die gesamte Datei ist durchgehend in Markdown formatiert und kann direkt als README.md gespeichert werden.
|
||||
Kopieren
|
||||
|
||||
### **Anmerkungen:**
|
||||
- Alle Code-Blöcke sind korrekt als `bash` oder `ini` markiert.
|
||||
- Die Struktur ist durchgehend mit `---` getrennt, um die Lesbarkeit zu erhöhen.
|
||||
- Die Befehle zum Mounten der Snapshots wurden korrigiert und um die Umgebungsvariablen ergänzt.
|
||||
- Dopplungen (z. B. die Zusammenfassung der Befehle) wurden entfernt.
|
||||
|
||||
Falls du weitere Anpassungen brauchst, lass es mich wissen!
|
||||
|
||||
71
restic-backup
Executable file
71
restic-backup
Executable file
@ -0,0 +1,71 @@
|
||||
#!/bin/bash
|
||||
|
||||
# ================================
|
||||
# Restic System Backup Script
|
||||
# Autor: Michael
|
||||
# ================================
|
||||
|
||||
# Variablen
|
||||
RESTIC_REPOSITORY=rclone:Cloud.Michaelis.Digital:backup-repo
|
||||
RESTIC_PASSWORD_FILE=/home/ratatoskr/.restic-pass
|
||||
LOGFILE=/var/log/restic-backup.log
|
||||
NTFY_TOPIC=A72
|
||||
NTFY_URL=https://ntfy.michaelis.digital/$NTFY_TOPIC
|
||||
NTFY_USER=Michaelis
|
||||
NTFY_PASS=u5ptufUFuDL6q4yEcSN3iqas5gtXNkN77Lx3cy3oX8UoSgFWdifYy9FVopv2Zwtu
|
||||
|
||||
# TMPDIR auf ein großes Laufwerk setzen
|
||||
export TMPDIR=/var/tmp
|
||||
|
||||
# Backup Startzeit
|
||||
START=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
echo "==== Backup Start: $START ====" | tee -a $LOGFILE
|
||||
|
||||
# Backup ausführen
|
||||
echo "Starte Backup..." | tee -a $LOGFILE
|
||||
BACKUP_OUTPUT=$(restic -r $RESTIC_REPOSITORY \
|
||||
--password-file $RESTIC_PASSWORD_FILE \
|
||||
backup / \
|
||||
--exclude /proc \
|
||||
--exclude /tmp \
|
||||
--exclude /mnt \
|
||||
--exclude /dev \
|
||||
--exclude /sys \
|
||||
--exclude /run \
|
||||
--exclude /var/tmp \
|
||||
--verbose 2>&1)
|
||||
BACKUP_EXIT=$?
|
||||
|
||||
# Backup Endezeit und Dauer
|
||||
END=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
START_SEC=$(date +%s)
|
||||
END_SEC=$(date +%s)
|
||||
DURATION=$(date -u -d @$((END_SEC-START_SEC)) +%H:%M:%S)
|
||||
|
||||
# Letztes Snapshot ermitteln
|
||||
LATEST=$(restic -r $RESTIC_REPOSITORY --password-file $RESTIC_PASSWORD_FILE snapshots --latest 1 --json | jq -r '.[0].short_id' 2>/dev/null)
|
||||
|
||||
# Stats auslesen
|
||||
STATS=$(restic -r $RESTIC_REPOSITORY --password-file $RESTIC_PASSWORD_FILE stats --mode blobs-per-file $LATEST 2>&1)
|
||||
|
||||
# Ausgabe in Log und Terminal
|
||||
{
|
||||
echo "$BACKUP_OUTPUT"
|
||||
echo ""
|
||||
echo "Backup-Dauer: $DURATION"
|
||||
echo "Letzter Snapshot: $LATEST"
|
||||
echo "$STATS"
|
||||
echo "Exit-Code: $BACKUP_EXIT"
|
||||
echo "==== Backup Ende: $END ===="
|
||||
} | tee -a $LOGFILE
|
||||
|
||||
# NTFY Benachrichtigung
|
||||
SUMMARY="Restic Backup abgeschlossen
|
||||
Snapshot: $LATEST
|
||||
Dauer: $DURATION
|
||||
$STATS
|
||||
Exit-Code: $BACKUP_EXIT
|
||||
$(date)"
|
||||
|
||||
curl -u $NTFY_USER:$NTFY_PASS -H "Title: Restic Backup" -H "Tags: backup" -d "$SUMMARY" $NTFY_URL
|
||||
|
||||
42
restic-backup.service
Normal file
42
restic-backup.service
Normal file
@ -0,0 +1,42 @@
|
||||
[Unit]
|
||||
Description=Restic System Backup
|
||||
Documentation=man:restic(1)
|
||||
Wants=network-online.target
|
||||
After=network-online.target
|
||||
|
||||
# Falls ein altes Restic läuft → killen, damit keine Locks bleiben
|
||||
Conflicts=restic-backup.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
# Output direkt an journal
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
|
||||
# Backup soll das gesamte System sehen → root
|
||||
User=root
|
||||
|
||||
# Passwort für das Repo
|
||||
Environment=RESTIC_PASSWORD_FILE=/home/ratatoskr/.restic-pass
|
||||
|
||||
# Dein Script – ohne sudo, ohne Pfadprobleme
|
||||
ExecStart=/usr/local/bin/restic-backup
|
||||
|
||||
# Damit systemd weiß: nicht killen, wenn es lange dauert (z. B. >30 min)
|
||||
TimeoutStartSec=0
|
||||
|
||||
# Prioritäten – Backup darf langsam sein, aber stabil
|
||||
Nice=19
|
||||
IOSchedulingClass=best-effort
|
||||
IOSchedulingPriority=7
|
||||
|
||||
# Wenn das Backup crasht → 3 Versuche
|
||||
Restart=on-failure
|
||||
RestartSec=30
|
||||
|
||||
# Mehr Log-Ausgaben behalten
|
||||
LogRateLimitIntervalSec=0
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
||||
20
restic-backup.timer
Normal file
20
restic-backup.timer
Normal file
@ -0,0 +1,20 @@
|
||||
[Unit]
|
||||
Description=Run Restic Backup 5 Minuten nach Boot
|
||||
Documentation=man:systemd.timer
|
||||
|
||||
[Timer]
|
||||
# Starte 30 Sekunden nach Boot
|
||||
OnBootSec=30sec
|
||||
|
||||
# Optional: regelmäßig stündlich zusätzlich
|
||||
#OnCalendar=hourly
|
||||
|
||||
# Timer merkt sich auch vergangene Starts, wenn PC aus war
|
||||
Persistent=true
|
||||
|
||||
# Minimaler Abstand zwischen Starts, falls Backup noch läuft
|
||||
AccuracySec=1min
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user