# Backup & Disaster Recovery

Backup strategies, disaster recovery procedures, and data retention policies for Monetarie PIX.

## Prerequisites

- Database administrator access
- Understanding of RPO/RTO requirements
- Storage for backup files (local, S3, GCS, or Azure Blob)

## Backup Strategy

### PostgreSQL

| Method | Frequency | RPO | Storage |
|--------|-----------|-----|---------|
| `pg_dump` (logical) | Daily | 24h | Object storage |
| WAL archiving (continuous) | Continuous | Minutes | Object storage |
| Cloud SQL automated | Configurable | Configurable | Cloud provider |
| Point-in-time recovery | On demand | Minutes | WAL archives |

### Logical Backup

```bash
# Full database backup
pg_dump -Fc -h $DB_HOST -U $DB_USER -d monetarie > monetarie_$(date +%Y%m%d).dump

# Schema-specific backup
pg_dump -Fc -h $DB_HOST -U $DB_USER -d monetarie -n monetarie_spi > spi_$(date +%Y%m%d).dump

# Restore
pg_restore -h $DB_HOST -U $DB_USER -d monetarie monetarie_20260209.dump
```

### WAL Archiving

```sql
-- postgresql.conf
ALTER SYSTEM SET archive_mode = on;
ALTER SYSTEM SET archive_command = 'gcloud storage cp %p gs://fluxiq-wal-archive/%f';
ALTER SYSTEM SET wal_level = replica;
```

### Automated Backup Script

```bash
#!/bin/bash
# /opt/monetarie/scripts/backup.sh
BACKUP_DIR="/backups/pix"
DATE=$(date +%Y%m%d_%H%M%S)
RETENTION_DAYS=30

# Backup
pg_dump -Fc -h $DB_HOST -U $DB_USER -d monetarie > $BACKUP_DIR/monetarie_$DATE.dump

# Upload to cloud storage
gcloud storage cp $BACKUP_DIR/monetarie_$DATE.dump gs://fluxiq-backups/pix/

# Cleanup old backups
find $BACKUP_DIR -name "*.dump" -mtime +$RETENTION_DAYS -delete
```

## Redis Backup

Redis is used for ephemeral data (rate limits, token blacklist, idempotency). Backup is optional but recommended:

```bash
# Trigger RDB snapshot
redis-cli BGSAVE

# Copy RDB file
cp /var/lib/redis/dump.rdb /backups/redis/dump_$(date +%Y%m%d).rdb
```

## NATS JetStream

JetStream data is stored on disk. Backup the store directory:

```bash
# Stop NATS gracefully, backup store
tar czf /backups/nats/nats_$(date +%Y%m%d).tar.gz /data/nats/
```

## Disaster Recovery

### RTO/RPO Targets

| Scenario | RPO | RTO |
|----------|-----|-----|
| Database corruption | < 1 hour | 2 hours |
| Full cluster failure | < 1 hour | 4 hours |
| Region outage | < 24 hours | 8 hours |

### Recovery Procedures

#### Database Recovery

```bash
# 1. Stop backend pods
kubectl scale deployment pix-backend -n pix --replicas=0

# 2. Restore from backup
pg_restore -h $NEW_DB_HOST -U $DB_USER -d monetarie monetarie_backup.dump

# 3. Apply any WAL after backup time (PITR)
# Configure recovery.conf for point-in-time recovery

# 4. Restart backend
kubectl scale deployment pix-backend -n pix --replicas=2

# 5. Run pending migrations
kubectl exec -n pix deployment/pix-backend -- bin/monetarie_pix eval "Shared.Release.migrate()"
```

#### NATS Recovery

```bash
# 1. Restore JetStream store
tar xzf nats_backup.tar.gz -C /data/nats/

# 2. Start NATS nodes
systemctl start nats

# 3. Verify streams
nats stream ls
```

## Data Retention

| Data | Retention | Managed By |
|------|-----------|------------|
| ICOM messages | 10 years | XmlArchiver |
| DICT read logs | 2 years | XmlArchiver |
| Audit activity_log | Partitioned monthly | PartitionManager |
| Login history | Partitioned monthly | PartitionManager |
| NATS JetStream | 7-90 days per stream | Stream configuration |
| Redis data | Ephemeral (TTL-based) | Redis eviction |

The `PartitionManager` GenServer automatically purges expired audit partitions daily.

## Expected Outcome

After configuring backup and DR:

- Daily logical backups stored in cloud storage
- WAL archiving for point-in-time recovery
- Documented recovery procedures tested quarterly
- Data retention policies enforced automatically
