feat(backup): backup automatisé PostgreSQL + Alarik avec test de restauration (#180)
This commit is contained in:
1 parent
e49b0d73aa
commit
881ad48066
3 files changed
+270
-2
No files matched your search
@@ -0,0 +1,203 @@
|
||||
# Heimdall — Backup & Restore
|
||||
|
||||
Sauvegarde automatisée de PostgreSQL (quotidien) et Alarik/S3 (hebdomadaire) avec test de restauration.
|
||||
|
||||
## Vue d'ensemble
|
||||
|
||||
| Composant | Fréquence | Script | Rétention |
|
||||
|---|---|---|---|
|
||||
| PostgreSQL `pg_dump` | Quotidien 02:00 UTC | `docker/scripts/backup.sh` | 30 jours (local) + 30 jours (S3) |
|
||||
| Alarik `mc mirror` | Hebdo dimanche 03:00 UTC | `docker/scripts/backup-alarik.sh` | 12 semaines |
|
||||
| Test de restauration | Manuel / CI | `docker/scripts/test-backup-restore.sh` | — |
|
||||
|
||||
## Activation
|
||||
|
||||
### Option 1 — Overlay docker-compose (recommandé)
|
||||
|
||||
```bash
|
||||
cd docker/
|
||||
docker compose -f docker-compose.yml -f docker-compose.backup.yml up -d
|
||||
```
|
||||
|
||||
Le service `backup` tourne dans un conteneur PostgreSQL 17-alpine avec crontab interne.
|
||||
Les dumps sont stockés dans le volume `backup_data` et uploadés vers Alarik/S3 si configuré.
|
||||
|
||||
### Option 2 — Profil dans docker-compose.prod.yml
|
||||
|
||||
```bash
|
||||
cd docker/
|
||||
docker compose --profile backup -f docker-compose.prod.yml up -d
|
||||
```
|
||||
|
||||
Le service backup est activé via le profil Docker Compose `backup`.
|
||||
|
||||
## Variables d'environnement
|
||||
|
||||
| Variable | Défaut | Description |
|
||||
|---|---|---|
|
||||
| `POSTGRES_HOST` | `postgresql` | Host PostgreSQL (nom de service Docker) |
|
||||
| `POSTGRES_PORT` | `5432` | Port PostgreSQL |
|
||||
| `POSTGRES_DB` | `heimdall` | Nom de la base |
|
||||
| `POSTGRES_USER` | `heimdall` | Utilisateur PostgreSQL |
|
||||
| `POSTGRES_PASSWORD` | `heimdall` | Mot de passe PostgreSQL |
|
||||
| `ALARIK_ENDPOINT` | — | Endpoint S3/Alarik (ex: `http://alarik:9000`) |
|
||||
| `ALARIK_ACCESS_KEY` | — | Clé d'accès S3 |
|
||||
| `ALARIK_SECRET_KEY` | — | Clé secrète S3 |
|
||||
| `ALARIK_BUCKET` | `heimdall` | Bucket S3 |
|
||||
| `BACKUP_RETENTION_DAYS` | `30` | Rétention jours (PostgreSQL) |
|
||||
| `BACKUP_RETENTION_WEEKS` | `12` | Rétention semaines (Alarik) |
|
||||
| `BACKUP_LOCAL_DIR` | `/backups` | Répertoire local dans le conteneur |
|
||||
|
||||
## Structure des backups
|
||||
|
||||
### PostgreSQL (S3)
|
||||
|
||||
```
|
||||
s3://heimdall/backup/postgresql/
|
||||
├── latest.sql.gz ← symlink logique (copie du plus récent)
|
||||
├── 2026-08-28/
|
||||
│ └── heimdall-2026-08-28T02-00-00Z.sql.gz
|
||||
├── 2026-08-29/
|
||||
│ └── heimdall-2026-08-29T02-00-00Z.sql.gz
|
||||
└── ...
|
||||
```
|
||||
|
||||
### Alarik (S3 mirror)
|
||||
|
||||
```
|
||||
s3://heimdall/backup/alarik/
|
||||
├── 2026-W35/
|
||||
│ ├── MANIFEST.txt
|
||||
│ ├── snapshots/...
|
||||
│ └── recordings/...
|
||||
├── 2026-W34/
|
||||
│ └── ...
|
||||
└── ...
|
||||
```
|
||||
|
||||
## Commandes manuelles
|
||||
|
||||
### Lancer un backup PostgreSQL
|
||||
|
||||
```bash
|
||||
# Depuis l'hôte (avec accès docker)
|
||||
docker exec heimdall-backup /scripts/backup.sh
|
||||
|
||||
# Dry-run
|
||||
docker exec heimdall-backup /scripts/backup.sh --dry-run
|
||||
```
|
||||
|
||||
### Lancer un mirror Alarik
|
||||
|
||||
```bash
|
||||
docker exec heimdall-backup /scripts/backup-alarik.sh
|
||||
```
|
||||
|
||||
### Restaurer PostgreSQL
|
||||
|
||||
```bash
|
||||
# Depuis un fichier local
|
||||
docker exec heimdall-backup /scripts/restore.sh --file /backups/postgresql/latest.sql.gz --yes
|
||||
|
||||
# Depuis S3
|
||||
docker exec heimdall-backup /scripts/restore.sh --latest --yes
|
||||
|
||||
# Dry-run
|
||||
docker exec heimdall-backup /scripts/restore.sh --latest --dry-run
|
||||
```
|
||||
|
||||
## Test de restauration (runbook)
|
||||
|
||||
### Prérequis
|
||||
|
||||
- Docker
|
||||
- Le stack Heimdall est à jour (`docker compose up -d`)
|
||||
|
||||
### Procédure
|
||||
|
||||
```bash
|
||||
cd docker/scripts/
|
||||
|
||||
# 1. Lancer le test (crée un PG isolé, seed, dump, drop, restore, vérifie)
|
||||
./test-backup-restore.sh
|
||||
|
||||
# Résultat attendu : All tests PASSED ✅
|
||||
```
|
||||
|
||||
Le script teste :
|
||||
|
||||
1. **Création d'un dump** — pg_dump --format=custom --compress=9
|
||||
2. **Drop + recreate** de la base de test
|
||||
3. **Restauration** via pg_restore
|
||||
4. **Vérification** :
|
||||
- Nombre de sites identique
|
||||
- Nombre de caméras identique
|
||||
- Nombre d'événements identique
|
||||
- Noms et URLs préservés (intégrité des données)
|
||||
- Absence de violations de clés étrangères
|
||||
|
||||
### Mode sans cleanup
|
||||
|
||||
```bash
|
||||
./test-backup-restore.sh --no-cleanup
|
||||
# Le conteneur de test reste actif sur le port 15432
|
||||
# Utile pour inspection manuelle
|
||||
```
|
||||
|
||||
### Restauration complète (runbook DR)
|
||||
|
||||
En cas de disaster recovery :
|
||||
|
||||
```bash
|
||||
# 1. Arrêter le stack
|
||||
cd docker/
|
||||
docker compose down
|
||||
|
||||
# 2. Supprimer les données corrompues
|
||||
docker volume rm docker_pgdata
|
||||
|
||||
# 3. Redémarrer PostgreSQL seul
|
||||
docker compose up -d postgresql
|
||||
sleep 10
|
||||
|
||||
# 4. Restaurer depuis S3 (latest)
|
||||
docker compose run --rm backup /scripts/restore.sh --latest --yes
|
||||
|
||||
# 5. Redémarrer tout le stack
|
||||
docker compose up -d
|
||||
|
||||
# 6. Vérifier
|
||||
# - Sites accessibles via l'UI
|
||||
# - Agents connectés (heartbeat)
|
||||
# - Événements présents
|
||||
# - Snapshots accessibles dans Alarik
|
||||
```
|
||||
|
||||
### Vérification post-restauration
|
||||
|
||||
| Check | Commande |
|
||||
|---|---|
|
||||
| Sites | `curl -s http://localhost:5000/api/sites -H "Authorization: Bearer $TOKEN" \| jq '.[].name'` |
|
||||
| Agents | `curl -s http://localhost:5000/api/agents -H "Authorization: Bearer $TOKEN" \| jq '.[].name'` |
|
||||
| Événements | `curl -s "http://localhost:5000/api/cameras/{id}/events" -H "Authorization: Bearer $TOKEN" \| jq 'length'` |
|
||||
| Alarik | Ouvrir `http://localhost:9001` — vérifier les snapshots |
|
||||
|
||||
## Logs
|
||||
|
||||
Les logs backup sont disponibles dans le conteneur :
|
||||
|
||||
```bash
|
||||
# Dernière exécution
|
||||
docker exec heimdall-backup cat /var/log/backup.log
|
||||
|
||||
# Historique (rotation hebdomadaire)
|
||||
docker exec heimdall-backup ls -la /var/log/backup.log*
|
||||
```
|
||||
|
||||
## Pièges
|
||||
|
||||
- **pg_dump --format=custom** nécessite `pg_restore` pour la restauration (pas `psql`)
|
||||
- Le conteneur backup utilise `postgres:17-alpine` pour avoir `pg_dump` et `pg_restore`
|
||||
- mc (MinIO Client) est installé au démarrage du conteneur (curl depuis dl.min.io)
|
||||
- Si S3 n'est pas configuré, le backup est local uniquement (volume Docker)
|
||||
- La restauration DROP la base existante — toujours vérifier le `--dry-run` d'abord
|
||||
@@ -212,6 +212,69 @@ services:
|
||||
networks:
|
||||
- heimdall-net
|
||||
|
||||
# ── Backup service (opt-in via --profile backup) ───────────────
|
||||
|
||||
backup:
|
||||
image: postgres:17-alpine
|
||||
container_name: heimdall-backup
|
||||
profiles:
|
||||
- backup
|
||||
environment:
|
||||
POSTGRES_HOST: postgresql
|
||||
POSTGRES_PORT: "5432"
|
||||
POSTGRES_DB: ${POSTGRES_DB:-heimdall}
|
||||
POSTGRES_USER: ${POSTGRES_USER:-heimdall}
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
|
||||
# S3/Alarik config (optional — backup works local-only without these)
|
||||
ALARIK_ENDPOINT: ${ALARIK_ENDPOINT:-}
|
||||
ALARIK_ACCESS_KEY: ${ALARIK_ACCESS_KEY:-}
|
||||
ALARIK_SECRET_KEY: ${ALARIK_SECRET_KEY:-}
|
||||
ALARIK_BUCKET: ${ALARIK_BUCKET:-heimdall}
|
||||
# Backup config
|
||||
BACKUP_RETENTION_DAYS: ${BACKUP_RETENTION_DAYS:-30}
|
||||
BACKUP_RETENTION_WEEKS: ${BACKUP_RETENTION_WEEKS:-12}
|
||||
BACKUP_LOCAL_DIR: /backups
|
||||
TZ: Europe/Paris
|
||||
secrets:
|
||||
- db_password
|
||||
volumes:
|
||||
- ./scripts:/scripts:ro
|
||||
- backup_data:/backups
|
||||
depends_on:
|
||||
postgresql:
|
||||
condition: service_healthy
|
||||
networks:
|
||||
- heimdall-net
|
||||
restart: unless-stopped
|
||||
logging: *default-logging
|
||||
entrypoint: ["/bin/sh", "-c"]
|
||||
command:
|
||||
- |
|
||||
# Read password from Docker secret file
|
||||
if [ -f /run/secrets/db_password ]; then
|
||||
export POSTGRES_PASSWORD="$(cat /run/secrets/db_password)"
|
||||
fi
|
||||
|
||||
apk add --no-cache curl bash > /dev/null 2>&1 || true
|
||||
curl -sSL https://dl.min.io/client/mc/release/linux-amd64/mc -o /usr/local/bin/mc 2>/dev/null || true
|
||||
chmod +x /usr/local/bin/mc 2>/dev/null || true
|
||||
chmod +x /scripts/*.sh
|
||||
|
||||
# Create crontab
|
||||
echo "# Daily PostgreSQL backup at 02:00 UTC" > /tmp/crontab.txt
|
||||
echo "0 2 * * * /scripts/backup.sh >> /var/log/backup.log 2>&1" >> /tmp/crontab.txt
|
||||
echo "# Weekly Alarik mirror on Sundays at 03:00 UTC" >> /tmp/crontab.txt
|
||||
echo "0 3 * * 0 /scripts/backup-alarik.sh >> /var/log/backup-alarik.log 2>&1" >> /tmp/crontab.txt
|
||||
echo "# Log rotation weekly" >> /tmp/crontab.txt
|
||||
echo "0 4 * * 0 if [ -f /var/log/backup.log ]; then mv /var/log/backup.log /var/log/backup.log.\$$(date +\%Y\%W); fi" >> /tmp/crontab.txt
|
||||
|
||||
crontab /tmp/crontab.txt
|
||||
echo "Crontab installed:"
|
||||
crontab -l
|
||||
|
||||
# Run crond in foreground
|
||||
exec crond -f -l 2
|
||||
|
||||
# ── Optional services ──────────────────────────────────────────
|
||||
|
||||
coturn:
|
||||
@@ -296,6 +359,8 @@ volumes:
|
||||
driver: local
|
||||
rabbitmq_data:
|
||||
driver: local
|
||||
backup_data:
|
||||
driver: local
|
||||
|
||||
secrets:
|
||||
db_password:
|
||||
|
||||
@@ -115,8 +115,8 @@ if [[ "$IS_DRY_RUN" != "1" ]]; then
|
||||
|
||||
if [[ "$CLIENT" == "mc" ]]; then
|
||||
STATS=$(mc ls "heimdall-backup/${S3_BUCKET}/${BACKUP_PREFIX}/${WEEK_TAG}/" --recursive --summarize 2>/dev/null | tail -2)
|
||||
OBJECT_COUNT=$(echo "$STATATS" | grep -oP 'Objects: \K\d+' || echo "0")
|
||||
TOTAL_SIZE=$(echo "$STATATS" | grep -oP 'Size: \K\d+' || echo "0")
|
||||
OBJECT_COUNT=$(echo "$STATS" | grep -oP 'Objects: \K\d+' || echo "0")
|
||||
TOTAL_SIZE=$(echo "$STATS" | grep -oP 'Size: \K\d+' || echo "0")
|
||||
fi
|
||||
|
||||
MANIFEST="Heimdall Alarik Backup Manifest
|
||||
|
||||
Reference in new issue
Block a user