Files
pfandsystem-demo/scripts/restore.sh

53 lines
1.8 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Dockly-Pfandsystem Demo - Restore-Skript
# Stellt einen Backup-Stand wieder her.
# Aufruf:
# ./restore.sh # listet verfuegbare Backups
# ./restore.sh 2026-05-26_03-00 # stellt Stand wieder her (DB + Uploads)
# ./restore.sh 2026-05-26_03-00 db-only # nur DB
#
# ACHTUNG: Ueberschreibt bestehende DB-Daten und Uploads.
set -euo pipefail
PROJECT_DIR="${PROJECT_DIR:-/root/pfandsystem-demo}"
BACKUP_DIR="${BACKUP_DIR:-/root/backups/pfandsystem-demo}"
TS="${1:-}"
MODE="${2:-full}"
MYSQL_PASSWORD="$(grep -E '^MYSQL_PASSWORD=' "$PROJECT_DIR/.env" | head -n1 | cut -d= -f2-)"
if [[ -z "$TS" ]]; then
echo "Verfuegbare Backups:"
ls -1t "$BACKUP_DIR" 2>/dev/null | grep -E '^db_' | sed 's/^db_//; s/\.sql\.gz$//' | head -30
echo
echo "Aufruf: $0 <timestamp> [db-only|uploads-only|full]"
exit 0
fi
DB_FILE="$BACKUP_DIR/db_${TS}.sql.gz"
UP_FILE="$BACKUP_DIR/uploads_${TS}.tar.gz"
[[ -f "$DB_FILE" ]] || { echo "Nicht gefunden: $DB_FILE"; exit 1; }
read -p "Backup '$TS' wirklich wiederherstellen (Modus: $MODE)? [yes/N] " ANS
[[ "$ANS" == "yes" ]] || { echo "Abbruch."; exit 0; }
if [[ "$MODE" != "uploads-only" ]]; then
echo "[$(date +%T)] DB-Restore aus $DB_FILE"
gunzip -c "$DB_FILE" | docker exec -i pfandsystem-demo-mysql \
mysql --default-character-set=utf8mb4 -upfandsystem -p"${MYSQL_PASSWORD}" pfandsystem_demo
fi
if [[ "$MODE" != "db-only" ]]; then
[[ -f "$UP_FILE" ]] || { echo "Uploads-Backup fehlt: $UP_FILE"; exit 1; }
echo "[$(date +%T)] Uploads-Restore aus $UP_FILE"
docker run --rm \
-v pfandsystem-demo_backend_uploads:/data \
-v "$BACKUP_DIR":/backup:ro \
alpine:3 \
sh -c "rm -rf /data/* && tar xzf /backup/uploads_${TS}.tar.gz -C /data"
fi
echo "[$(date +%T)] Restore fertig."