# Database Backup Strategy

## Overview

The Maternal App implements a comprehensive automated backup strategy to ensure data protection and business continuity.

## Features

### 1. Automated Backups
- **Schedule**: Daily at 2 AM (configurable via `BACKUP_SCHEDULE`)
- **Databases**: PostgreSQL (primary) + MongoDB (AI chat history)
- **Compression**: Gzip compression for storage efficiency
- **Retention**: 30 days (configurable via `BACKUP_RETENTION_DAYS`)

### 2. Storage Options
- **Local**: `/var/backups/maternal-app` (development/staging)
- **S3**: AWS S3 for production (off-site storage)
  - Encryption: AES256
  - Storage Class: STANDARD_IA (Infrequent Access)

### 3. Manual Operations
- Manual backup triggering
- Backup listing
- Database restoration
- Admin-only access

## Configuration

### Environment Variables

```bash
# Enable/disable backups
BACKUP_ENABLED=true

# Backup schedule (cron format)
BACKUP_SCHEDULE=0 2 * * *

# Retention period (days)
BACKUP_RETENTION_DAYS=30

# Local backup directory
BACKUP_DIR=/var/backups/maternal-app

# S3 configuration (optional)
BACKUP_S3_BUCKET=maternal-production-backups
BACKUP_S3_REGION=us-east-1
BACKUP_S3_ACCESS_KEY=your-access-key
BACKUP_S3_SECRET_KEY=your-secret-key
```

### Required Packages

```bash
# PostgreSQL client tools
sudo apt-get install postgresql-client

# MongoDB tools
sudo apt-get install mongodb-database-tools

# AWS SDK (for S3 uploads)
npm install @aws-sdk/client-s3
```

## Usage

### Automated Backups

Backups run automatically based on the configured schedule. No manual intervention required.

### Manual Backup

**Endpoint**: `POST /api/v1/backups`

**Authentication**: Admin JWT token required

```bash
curl -X POST https://api.maternal-app.com/api/v1/backups \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN"
```

**Response**:
```json
{
  "success": true,
  "message": "Backup completed successfully",
  "data": {
    "postgres": "/var/backups/maternal-app/postgresql_maternal_app_2025-10-03T02-00-00.sql.gz",
    "mongodb": "/var/backups/maternal-app/mongodb_2025-10-03T02-00-00.tar.gz",
    "timestamp": "2025-10-03T02:00:00.000Z"
  }
}
```

### List Backups

**Endpoint**: `GET /api/v1/backups`

```bash
curl https://api.maternal-app.com/api/v1/backups \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN"
```

**Response**:
```json
{
  "success": true,
  "data": {
    "backups": [
      {
        "filename": "postgresql_maternal_app_2025-10-03T02-00-00.sql.gz",
        "size": 15728640,
        "created": "2025-10-03T02:00:00.000Z"
      }
    ],
    "count": 1
  }
}
```

### Restore from Backup

**Endpoint**: `POST /api/v1/backups/restore?filename=backup.sql.gz`

**⚠️ WARNING**: This will overwrite the current database!

```bash
curl -X POST "https://api.maternal-app.com/api/v1/backups/restore?filename=postgresql_maternal_app_2025-10-03T02-00-00.sql.gz" \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN"
```

## Backup File Formats

### PostgreSQL Backup
- **Format**: Plain SQL with gzip compression
- **Extension**: `.sql.gz`
- **Command**: `pg_dump | gzip`
- **Size**: ~10-50MB (varies by data volume)

### MongoDB Backup
- **Format**: BSON dump with tar.gz compression
- **Extension**: `.tar.gz`
- **Command**: `mongodump + tar`
- **Size**: ~5-20MB (varies by chat history)

## Disaster Recovery

### Recovery Time Objective (RTO)
- **Target**: 1 hour
- **Process**: Restore from most recent backup + replay WAL logs

### Recovery Point Objective (RPO)
- **Target**: 24 hours (daily backups)
- **Improvement**: Enable PostgreSQL WAL archiving for point-in-time recovery

### Recovery Steps

1. **Stop the application**:
   ```bash
   systemctl stop maternal-app
   ```

2. **Restore PostgreSQL database**:
   ```bash
   gunzip -c /var/backups/maternal-app/postgresql_*.sql.gz | \
   psql -h localhost -U maternal_user -d maternal_app
   ```

3. **Restore MongoDB** (if needed):
   ```bash
   tar -xzf /var/backups/maternal-app/mongodb_*.tar.gz
   mongorestore --uri="mongodb://localhost:27017/maternal_ai_chat" ./mongodb_*
   ```

4. **Restart the application**:
   ```bash
   systemctl start maternal-app
   ```

5. **Verify data integrity**:
   - Check user count
   - Verify recent activities
   - Test AI chat functionality

## Best Practices

### Production Deployment

1. **Enable S3 uploads** for off-site storage
2. **Set up monitoring** for backup failures
3. **Test restoration** quarterly
4. **Document procedures** for on-call engineers
5. **Encrypt backups** at rest and in transit

### Monitoring

Monitor backup health with:
- **Success/failure notifications** (email/Slack)
- **Backup file size tracking** (detect corruption)
- **S3 upload verification**
- **Age of last successful backup**

Example monitoring query:
```bash
# Check age of last backup
find /var/backups/maternal-app -name "postgresql_*.sql.gz" -mtime -1
```

### Security

1. **Restrict access** to backup files (chmod 600)
2. **Encrypt sensitive backups** before S3 upload
3. **Rotate S3 access keys** regularly
4. **Audit backup access** logs
5. **Require MFA** for restoration operations

## Backup Verification

### Automated Verification

The backup service verifies:
- ✅ Backup file exists
- ✅ File size > 0
- ✅ Gzip integrity (`gunzip -t`)

### Manual Verification (Quarterly)

1. Create test environment
2. Restore latest backup
3. Run application smoke tests
4. Compare row counts with production
5. Document verification results

## Troubleshooting

### Backup Failed - Disk Space

**Symptom**: Backup fails with "No space left on device"

**Solution**:
```bash
# Check disk usage
df -h /var/backups

# Clean up old backups manually
find /var/backups/maternal-app -name "*.gz" -mtime +30 -delete

# Increase retention period (reduce BACKUP_RETENTION_DAYS)
```

### Backup Failed - Database Connection

**Symptom**: "could not connect to database"

**Solution**:
- Verify `DATABASE_HOST`, `DATABASE_USER`, `DATABASE_PASSWORD`
- Check PostgreSQL is running: `systemctl status postgresql`
- Test connection: `psql -h $DB_HOST -U $DB_USER -d $DB_NAME`

### S3 Upload Failed

**Symptom**: "Access Denied" or "Invalid credentials"

**Solution**:
- Verify S3 bucket exists and is accessible
- Check IAM permissions for `PutObject`
- Validate `BACKUP_S3_ACCESS_KEY` and `BACKUP_S3_SECRET_KEY`
- Test AWS CLI: `aws s3 ls s3://your-bucket-name/`

## Cost Optimization

### Storage Costs

- **S3 Standard-IA**: ~$0.0125/GB/month
- **30-day retention**: ~$0.375 for 30GB of backups
- **Lifecycle policy**: Move to Glacier after 90 days for long-term archival

### Optimization Tips

1. Use S3 Intelligent-Tiering
2. Enable backup compression
3. Adjust retention period based on compliance requirements
4. Archive old backups to Glacier

## Compliance

### GDPR/COPPA

- **Right to Deletion**: Automated deletion requests backup user data before purge
- **Data Portability**: Backups support full data export
- **Audit Trail**: All backup/restore operations logged

### HIPAA (if applicable)

- **Encryption**: Enable AES-256 encryption for backups
- **Access Control**: Require MFA for backup restoration
- **Audit Logging**: Track all backup access

## Future Enhancements

1. **Point-in-Time Recovery** (PostgreSQL WAL archiving)
2. **Incremental backups** (reduce storage costs)
3. **Cross-region replication** (disaster recovery)
4. **Automated restore testing** (verify backup integrity)
5. **Backup metrics dashboard** (Grafana visualization)
