- Shell 100%
| check4reboot.sh | ||
| deploy-cert-dbl01.sh | ||
| file_backup.sh | ||
| influxdb_backup.sh | ||
| nextcloud_backup.sh | ||
| pgsql_backup.sh | ||
| README.md | ||
| update-forgejo.sh | ||
| update-nextcloud.sh | ||
Backup Scripts
Sammlung von Bash-Scripts für verschiedene Backup-Aufgaben mit einheitlicher Konfiguration, Statusfiles und Logging.
Übersicht
| Script | Version | Beschreibung |
|---|---|---|
file_backup.sh |
2.3 | System-Verzeichnisse als tar.gz |
pgsql_backup.sh |
2.0 | PostgreSQL-Datenbanken mit pg_dump |
influxdb_backup.sh |
2.0 | InfluxDB-Datenbanken mit influxd backup |
nextcloud_backup.sh |
2.0 | Nextcloud-Daten plus PostgreSQL-Datenbank |
Gemeinsame Features
- Config-Files: Alle Konfigurationen in separaten Dateien unter
/usr/local/etc/ - Statusfiles: Monitoring-kompatible Status-Ausgabe für externe Überwachung
- Logging: Einheitliche Log-Struktur mit DEBUG/INFO/ERROR-Level
- Sicherheit: Sichere Passwort-Behandlung, Config-File-Berechtigungen
- Funktionsbasiert: Modularer Aufbau für bessere Wartbarkeit
file_backup.sh
Bash-Script zur Sicherung konfigurierter Verzeichnisse als komprimierte tar.gz-Archive mit automatischer Rotation.
Verwendung
file_backup.sh [--config CONFIG_FILE] [--debug]
file_backup.sh --help
file_backup.sh --version
Konfigurationsdatei
Pfad: /usr/local/etc/file_backup.conf
# Zu sichernde Verzeichnisse (Bash-Array)
BACKUP_DIRS=(
/etc
/usr/local
/usr/src
/home
/root
/var/spool/cron
)
# Verzeichnisse/Muster die vom Backup ausgeschlossen werden sollen
EXCLUDE_PATTERNS=(
".vscode-server"
".cache"
"*.tmp"
"node_modules"
"*.rpm"
)
# Anzahl aufzubewahrender Generationen pro Verzeichnis
RETENTION=7
# Basisverzeichnis für Backups (muss auf /backup liegen)
BASEDIR=/backup
# Logverzeichnis
LOGDIR=/usr/local/logs
Statusdatei
Format: starttime;RC;desc;endtime;duration;lastbackupok;backupsize
pgsql_backup.sh
Bash-Script zur Sicherung PostgreSQL-Datenbanken mit pg_dump (Version 2.0).
Verwendung
pgsql_backup.sh <DATABASE> [--debug]
pgsql_backup.sh --help
pgsql_backup.sh --version
Features Version 2.0
- Config-File: Alle Variablen in
/usr/local/etc/pgsql_backup.conf - Originalgröße: Ermittlung der Datenbankgröße vor dem Backup
- Funktionsbasiert: Modularer Aufbau mit separaten Funktionen
- Erweiterte Statusdatei: Inklusive Originalgröße und verbessertes Monitoring
- Sichere Authentifizierung: Passwort über Umgebungsvariablen
Konfigurationsdatei
Pfad: /usr/local/etc/pgsql_backup.conf
# PostgreSQL User und Passwort (base64 kodiert)
USER="postgres"
# Passwort base64 kodiert - für bessere Sicherheit
PW="TWVpblBhc3N3b3J0"
# Backup-Verzeichnisse
BASEDIR="/backup/pgsql"
# Logverzeichnis
LOGDIR="/usr/local/logs"
# pg_dump-Parameter
DUMP_JOBS=2
DUMP_FORMAT="d" # directory format
DUMP_OPTIONS="-C" # include CREATE DATABASE
Funktionen
get_instances(): Liest verfügbare PostgreSQL-Datenbanken ausget_database_size(): Ermittelt Originalgröße der Datenbankperform_backup(): Führt pg_dump auswrite_state(): Schreibt Statusdatei für Monitoring
Statusdatei
Format: starttime;RC;desc;endtime;duration;lastbackupok;backupsize;lastflashbackok;originalsize
influxdb_backup.sh
Bash-Script zur Sicherung InfluxDB-Datenbanken mit influxd backup (Version 2.0).
Verwendung
influxdb_backup.sh <DATABASE> [--debug]
influxdb_backup.sh --help
influxdb_backup.sh --version
Features Version 2.0
- Config-File: Alle Variablen in
/usr/local/etc/influxdb_backup.conf - Originalgröße: Ermittlung der Datenbankgröße über Filesystem-Analyse
- Funktionsbasiert: Modularer Aufbau mit separaten Funktionen
- Rolling Backup: Automatische .old-Versionen
- Erweiterte Statusdatei: Inklusive Originalgröße
Konfigurationsdatei
Pfad: /usr/local/etc/influxdb_backup.conf
# InfluxDB User und Passwort (base64 kodiert)
INFLUX_USER="backup"
PW="TWVpblBhc3N3b3J0"
# Verzeichnisse
BASEDIR="/backup"
LOGDIR="/usr/local/logs"
Funktionen
get_instances(): Liest verfügbare InfluxDB-Datenbanken ausget_database_size(): Ermittelt Originalgröße über TSM-Dateienmove_dir(): Rolling Backup-Mechanismuswrite_state(): Monitoring-Status
Originalgröße-Ermittlung
- Primär: Dateisystem-basierte Messung (
/var/lib/influxdb/data/<database>/) - Fallback: Schätzung basierend auf Series-Anzahl
Statusdatei
Format: starttime;RC;desc;endtime;duration;lastbackupok;backupsize;lastflashbackok;originalsize
nextcloud_backup.sh
Bash-Script zur Sicherung von Nextcloud-Datenverzeichnis plus PostgreSQL-Datenbank (Version 2.0).
Verwendung
nextcloud_backup.sh [--debug]
nextcloud_backup.sh --help
nextcloud_backup.sh --version
Features Version 2.0
- Config-File: Alle Variablen in
/usr/local/etc/nextcloud_backup.conf - Funktionsbasiert: Modulare Funktionen für jeden Backup-Schritt
- Maintenance-Modus: Automatische Aktivierung/Deaktivierung
- PostgreSQL-Integration: Aufruf des pgsql_backup.sh Scripts
- Cleanup-Operationen: Papierkorb leeren, Dateien scannen
- Backup-Rotation: Automatisches Löschen alter Backups
Konfigurationsdatei
Pfad: /usr/local/etc/nextcloud_backup.conf
# Nextcloud Container und User
USER=www-data
CONTAINER=nextcloud
# Pfade
SOURCE_DIR="/opt/nextcloud/data"
BACKUP_DIR="/backup/nextcloud"
OCC="/opt/nextcloud/occ"
# Backup Retention
KEEP_DAYS=7
# Logverzeichnis
LOGDIR="/usr/local/logs"
# PostgreSQL Backup Integration
PGSQL_BACKUP_SCRIPT="/usr/local/scripts/pgsql_backup.sh"
PGSQL_DATABASE="nextcloud"
Funktionen
enable_maintenance()/disable_maintenance(): Maintenance-Modus verwaltencleanup_nextcloud(): Papierkorb leeren, Dateien scannenbackup_database(): PostgreSQL-Backup via pgsql_backup.shbackup_data(): Nextcloud-Datenverzeichnis als tar.gzcleanup_old_backups(): Backup-Rotation
Backup-Ablauf
- Cleanup: Papierkorb leeren, Dateien scannen
- Maintenance: Nextcloud in Wartungsmodus versetzen
- PostgreSQL: Datenbank-Backup über pgsql_backup.sh
- Daten: Nextcloud-Datenverzeichnis sichern
- Maintenance: Wartungsmodus deaktivieren
- Rotation: Alte Backups löschen
Statusdatei
Format: starttime;RC;desc;endtime;duration;lastbackupok;backupsize
Return Codes
- 0: Erfolgreich abgeschlossen
- 1: Backup fehlgeschlagen (DB oder Daten)
- 2: Backup erfolgreich, aber Maintenance-Modus konnte nicht deaktiviert werden
Restore-Verfahren
file_backup.sh - Verzeichnisse wiederherstellen
# 1. Verfügbare Backups anzeigen
ls -la /backup/system/
# 2. Archiv extrahieren
cd /
tar -xzf /backup/system/etc_20260401_120000.tar.gz
# 3. Spezifisches Verzeichnis an andere Stelle wiederherstellen
mkdir /tmp/restore
tar -xzf /backup/system/etc_20260401_120000.tar.gz -C /tmp/restore
# 4. Einzelne Dateien aus Archiv extrahieren
tar -xzf /backup/system/etc_20260401_120000.tar.gz etc/passwd etc/group
pgsql_backup.sh - PostgreSQL Datenbank wiederherstellen
# 1. Verfügbare Backups anzeigen
ls -la /backup/pgsql/
# 2. Datenbank vollständig wiederherstellen (directory format)
pg_restore -U postgres -d postgres -C /backup/pgsql/nextcloud/20260401_120000/
# 3. Nur Schema wiederherstellen
pg_restore -U postgres -d nextcloud -s /backup/pgsql/nextcloud/20260401_120000/
# 4. Nur Daten wiederherstellen
pg_restore -U postgres -d nextcloud -a /backup/pgsql/nextcloud/20260401_120000/
# 5. Spezifische Tabelle wiederherstellen
pg_restore -U postgres -d nextcloud -t specific_table /backup/pgsql/nextcloud/20260401_120000/
# 6. In neue Datenbank wiederherstellen
createdb -U postgres nextcloud_restore
pg_restore -U postgres -d nextcloud_restore /backup/pgsql/nextcloud/20260401_120000/
influxdb_backup.sh - InfluxDB Datenbank wiederherstellen
# 1. InfluxDB stoppen
systemctl stop influxdb
# 2. Verfügbare Backups anzeigen
ls -la /backup/
# 3. Vollständige Wiederherstellung
influxd restore -portable /backup/telegraf_20260401_120000
# 4. Spezifische Datenbank wiederherstellen
influxd restore -portable -db telegraf /backup/telegraf_20260401_120000
# 5. In neue Datenbank wiederherstellen
influxd restore -portable -db telegraf -newdb telegraf_restore /backup/telegraf_20260401_120000
# 6. Nur bestimmte Retention Policy
influxd restore -portable -db telegraf -rp autogen /backup/telegraf_20260401_120000
# 7. InfluxDB wieder starten
systemctl start influxdb
nextcloud_backup.sh - Nextcloud komplett wiederherstellen
# 1. Nextcloud in Maintenance-Modus versetzen
sudo -u www-data php /opt/nextcloud/occ maintenance:mode --on
# 2. Verfügbare Backups anzeigen
ls -la /backup/nextcloud/
# 3. PostgreSQL Datenbank wiederherstellen
pg_restore -U postgres -d postgres -C /backup/nextcloud/20260401_120000_db/
# 4. Nextcloud-Daten wiederherstellen
rm -rf /opt/nextcloud/data/*
tar -xzf /backup/nextcloud/nextcloud_data_20260401_120000.tar.gz -C /
# 5. Berechtigungen setzen
chown -R www-data:www-data /opt/nextcloud/data/
# 6. Dateien neu scannen
sudo -u www-data php /opt/nextcloud/occ files:scan --all
# 7. Maintenance-Modus deaktivieren
sudo -u www-data php /opt/nextcloud/occ maintenance:mode --off
Point-in-Time Recovery
PostgreSQL PITR (falls WAL-Archivierung aktiviert)
# 1. Basis-Backup wiederherstellen
pg_restore -U postgres -d postgres -C /backup/pgsql/nextcloud/20260401_120000/
# 2. WAL-Dateien kopieren
cp /backup/pgsql/wal/* /var/lib/postgresql/12/main/pg_wal/
# 3. recovery.conf erstellen
echo "restore_command = 'cp /backup/pgsql/wal/%f %p'" > /var/lib/postgresql/12/main/recovery.conf
echo "recovery_target_time = '2026-04-01 14:30:00'" >> /var/lib/postgresql/12/main/recovery.conf
# 4. PostgreSQL starten (führt Recovery aus)
systemctl start postgresql
Allgemeine Restore-Tipps
# 1. Backup-Integrität vor Restore prüfen
tar -tzf /backup/system/etc_20260401_120000.tar.gz > /dev/null && echo "Archive OK"
pg_restore --list /backup/pgsql/nextcloud/20260401_120000/ > /dev/null && echo "PostgreSQL Backup OK"
# 2. Test-Restore in separaten Verzeichnissen
mkdir /tmp/test-restore
tar -xzf /backup/system/etc_20260401_120000.tar.gz -C /tmp/test-restore
# 3. Backup-Größe und Alter prüfen
stat /backup/system/etc_20260401_120000.tar.gz
du -sh /backup/system/etc_20260401_120000.tar.gz
# 4. Restore-Log erstellen
tar -xzf /backup/system/etc_20260401_120000.tar.gz -v 2>&1 | tee /var/log/restore.log
Installation
1. Config-Files erstellen
# Sichere Berechtigungen setzen
chmod 600 /usr/local/etc/*.conf
chown root:root /usr/local/etc/*.conf
2. /backup mounten
# Beispiel für systemd mount
echo "/dev/sdb1 /backup ext4 defaults 0 2" >> /etc/fstab
mount /backup
3. Cron-Jobs einrichten
# Beispiel crontab
0 2 * * * /usr/local/scripts/file_backup.sh
0 3 * * * /usr/local/scripts/pgsql_backup.sh nextcloud
0 4 * * * /usr/local/scripts/influxdb_backup.sh telegraf
0 5 * * * /usr/local/scripts/nextcloud_backup.sh
Monitoring
Alle Scripts schreiben Statusdateien im Format:
starttime;RC;desc;endtime;duration;lastbackupok;backupsize;lastflashbackok;originalsize
Monitoring-Script Beispiel
#!/bin/bash
# Prüfe alle Backup-Status
for statfile in /backup/*/*.stat; do
if [ -f "$statfile" ]; then
RC=$(awk -F';' '{print $2}' "$statfile")
DESC=$(awk -F';' '{print $3}' "$statfile")
if [ "$RC" != "0" ]; then
echo "BACKUP ERROR in $statfile: $DESC (RC: $RC)"
fi
fi
done
Changelog
| Version | Datum | Script | Beschreibung |
|---|---|---|---|
| 1.0 | 2017-07-19 | pgsql_backup.sh | Erste Version |
| 1.0 | 2017-07-19 | influxdb_backup.sh | Erste Version |
| 1.0 | - | nextcloud_backup.sh | Erste Version |
| 2.3 | 2026-04-01 | file_backup.sh | --config Parameter, separate Statusdateien |
| 2.0 | 2026-04-01 | pgsql_backup.sh | Config-File, Funktionen, Originalgröße |
| 2.0 | 2026-04-01 | influxdb_backup.sh | Config-File, Funktionen, Originalgröße |
| 2.0 | 2026-04-01 | nextcloud_backup.sh | Config-File, Funktionen, Statusdatei |
Die Statusdatei (<DATABASE>.stat) enthält semicolon-getrennte Werte für externes Monitoring:
starttime;RC;description;endtime;duration;lastbackupok;backupsize;lastflashbackok,originalsize
| Feld | Beschreibung |
|---|---|
starttime |
Unix-Timestamp: Startzeit des Backups |
RC |
Return Code (0 = OK, 1 = Fehler, 5 = Warnungen, 9 = läuft) |
description |
Statusbeschreibung im Klartext |
endtime |
Unix-Timestamp: Endzeit des Backups |
duration |
Laufzeit in Sekunden |
lastbackupok |
Unix-Timestamp: letztes erfolgreiches Backup |
backupsize |
Größe des Backup-Verzeichnisses in Bytes |
lastflashbackok |
Unix-Timestamp: letzter erfolgreicher Flashback |
originalsize |
Original Größe des zu Sichernden Daten |
Changelog
| Version | Datum | Beschreibung |
|---|---|---|
| 1.0 | 19.07.2017 | Erste Version |
| 2.0 | 22.03.2026 | Refactoring: Config-File, Security Hardening, Best Practices, Error Handling |