Backup & Recovery¶
Protect your AegisAI data with regular PostgreSQL backups, Redis persistence, and documented recovery procedures.
What to Back Up¶
| Component | Data | Priority |
|---|---|---|
| PostgreSQL | Users, tenants, agents, policies, audit logs, anomalies, HITL | Critical |
| Redis | Pub/sub state, cached sessions | Medium (rebuildable) |
| OPA policies | Rego templates in policies/templates/ |
Low (in Git) |
| Storage | Async export files in storage/ |
Medium |
| Environment | .env, secrets |
Critical |
Audit logs are compliance-critical
Audit logs may be subject to regulatory retention requirements (GDPR, HIPAA, FZ-152). Plan retention and backup accordingly.
PostgreSQL Backup¶
Manual Backup¶
# Docker
docker compose exec postgres pg_dump -U aegisai -Fc aegisai > aegisai_backup_$(date +%Y%m%d).dump
# Direct connection
pg_dump -h localhost -U aegisai -Fc aegisai > aegisai_backup.dump
Automated Daily Backup (recommended)¶
Use the in-repo scripts from the project root (or set AEGISAI_DIR):
# One-shot
./scripts/backup_postgres.sh
# Optional offsite
BACKUP_S3_URI=s3://my-bucket/aegisai ./scripts/backup_postgres.sh
# or
BACKUP_RSYNC_TARGET=backup@host:/var/backups/aegisai ./scripts/backup_postgres.sh
Cron (daily at 02:15 UTC):
15 2 * * * AEGISAI_DIR=/home/smdg/aegisai BACKUP_DIR=/home/smdg/aegisai-backups /home/smdg/aegisai/scripts/backup_postgres.sh >> /home/smdg/aegisai/backup.log 2>&1
Or install via helper:
AEGISAI_DIR=/home/smdg/aegisai BACKUP_DIR=/home/smdg/aegisai-backups ./scripts/install_backup_cron.sh
Non-destructive restore drill (checksums + pg_restore --list, no downtime):
The script dumps PostgreSQL (pg_dump -Fc), archives models/ + storage/, writes SHA256SUMS, retains 30 days, and optionally uploads to S3 or rsync.
Point-in-Time Recovery (PITR)¶
For production, enable WAL archiving in PostgreSQL:
# postgresql.conf
wal_level = replica
archive_mode = on
archive_command = 'cp %p /var/lib/postgresql/wal_archive/%f'
Restore Procedure¶
Full Restore (script)¶
# Point at a dump file or a backup directory created by backup_postgres.sh
./scripts/restore_postgres.sh /var/backups/aegisai/20260727_021500
# or
./scripts/restore_postgres.sh /var/backups/aegisai/20260727_021500/aegisai_20260727_021500.dump
The script stops app/frontend, recreates the DB, restores the dump, optionally restores models_storage.tgz, runs Alembic, and brings services back up.
Manual restore¶
docker compose -f docker-compose.prod.yml --env-file .env stop app frontend
docker compose -f docker-compose.prod.yml --env-file .env exec -T postgres \
psql -U aegisai -d postgres -c "DROP DATABASE IF EXISTS aegisai;"
docker compose -f docker-compose.prod.yml --env-file .env exec -T postgres \
psql -U aegisai -d postgres -c "CREATE DATABASE aegisai;"
docker compose -f docker-compose.prod.yml --env-file .env exec -T postgres \
pg_restore -U aegisai -d aegisai --no-owner < aegisai_backup.dump
docker compose -f docker-compose.prod.yml --env-file .env up -d
Verify Restore¶
# Must return HTTP 200 (503 means not ready)
curl -sf https://aegisai.com.ru/api/v1/health/ready
curl -sf https://aegisai.com.ru/api/v1/stats -H "X-API-Key: <key>"
curl -sf https://aegisai.com.ru/api/v1/agents -H "X-API-Key: <key>"
Quarterly restore drill¶
- Restore a recent dump to a staging compose project (
-p aegisai-restore). - Confirm row counts for users, tenants, agents, audit_logs.
- Log in as super-admin and open dashboard.
- Record result in ops notes.
Redis Backup¶
Redis data is primarily ephemeral (pub/sub, event bus). Enable persistence for session resilience:
Manual Redis Backup¶
docker compose exec redis redis-cli BGSAVE
docker compose exec redis cp /data/dump.rdb /data/backup_$(date +%Y%m%d).rdb
Audit Log Export Backup¶
Export audit logs regularly for long-term compliance archives:
# Async export for large datasets
curl -X POST "http://localhost:8000/api/v1/audit/export/async?limit=100000" \
-H "X-API-Key: <key>"
# Download and store offsite
curl "http://localhost:8000/api/v1/audit/export/download/<job_id>" \
-H "X-API-Key: <key>" \
-o audit_archive_$(date +%Y%m%d).jsonl
Store exports in S3, GCS, or encrypted local storage with appropriate retention policies.
Storage Directory¶
Async export files are stored in storage/:
Disaster Recovery Plan¶
| Scenario | RTO Target | Procedure |
|---|---|---|
| Database corruption | 1 hour | Restore from latest pg_dump |
| Full server loss | 4 hours | Provision new VPS, restore DB, redeploy |
| Accidental data deletion | 30 minutes | Restore from backup, verify integrity |
| Redis failure | 15 minutes | Restart Redis (data rebuilds from events) |
| OPA failure | 5 minutes | Restart OPA (policies reload from templates) |
Recovery Checklist¶
- Provision infrastructure (see VPS Deployment)
- Restore PostgreSQL from latest backup
- Run
alembic upgrade headif needed - Restore environment variables and secrets
- Start services:
docker compose up -d - Verify health:
/api/v1/health/ready - Verify data integrity: check agent count, recent audit logs
- Re-create super-admin if user table was empty
- Notify users of recovery completion
Backup Testing¶
Untested backups are not backups
Test restore procedures quarterly:
- Restore to a staging environment
- Verify all tables have expected row counts
- Confirm API endpoints return correct data
- Test login with restored user credentials
- Document any issues and update procedures
Retention Policies¶
| Data Type | Recommended Retention | Storage |
|---|---|---|
| PostgreSQL daily backups | 30 days | Local + offsite |
| Audit log exports | Per compliance requirement (1–7 years) | Encrypted object storage |
| Redis snapshots | 7 days | Local |
| Application logs | 90 days | Log aggregation service |
Related Documentation¶
- Deployment — infrastructure setup
- Monitoring — health checks
- Docker Deployment — volume configuration