Files
MABEA/deploy/backup_datenbank.sh
patrickandClaude Sonnet 5 8e4c2608e3
CI / backend-tests (push) Successful in 2m50s
CI / frontend-build (push) Successful in 18s
feat(foundation): FOUND-006 strukturiertes Logging, FOUND-010 DB-Backup-Timer, IDENT-010 GTIN-Materialabgleich
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CVgbozhYmuEhiEJHffRXCV
2026-09-10 14:07:22 +02:00

31 lines
1.3 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# MABEA – FOUND-010: tägliches PostgreSQL-Backup (pg_dump, gzip, Rotation).
# Für einen systemd-Timer auf dem Zielserver gedacht (siehe
# mabea-backup.service/.timer in diesem Verzeichnis), gleiches Muster wie die
# bestehenden Prüfläufe (eskalation_pruefen.py etc.) - reines Ops-Skript, kein
# Teil der Anwendung selbst.
#
# Ausführung auf dem Zielserver (als root oder postgres-berechtigter Nutzer):
# DB_NAME=mabea BACKUP_DIR=/opt/mabea/backups ./backup_datenbank.sh
set -euo pipefail
DB_NAME="${DB_NAME:-mabea}"
BACKUP_DIR="${BACKUP_DIR:-/opt/mabea/backups}"
AUFBEWAHRUNG_TAGE="${AUFBEWAHRUNG_TAGE:-14}"
mkdir -p "${BACKUP_DIR}"
ZEITSTEMPEL="$(date +%Y-%m-%d_%H%M%S)"
ZIEL_DATEI="${BACKUP_DIR}/${DB_NAME}-${ZEITSTEMPEL}.sql.gz"
echo "== Backup ${DB_NAME} -> ${ZIEL_DATEI} =="
# Als postgres-Systemuser (peer auth, Superrecht) statt der Anwendungs-Rolle
# "mabea" - kein Passwort im Skript nötig, gleiches Muster wie
# install_server.sh:run_psql.
su -s /bin/bash postgres -c "pg_dump ${DB_NAME}" | gzip > "${ZIEL_DATEI}"
echo "== Rotation: Backups älter als ${AUFBEWAHRUNG_TAGE} Tage löschen =="
find "${BACKUP_DIR}" -name "${DB_NAME}-*.sql.gz" -mtime "+${AUFBEWAHRUNG_TAGE}" -print -delete
echo "== Fertig: $(du -h "${ZIEL_DATEI}" | cut -f1) =="