|
| 1 | +# Backup and Disaster Recovery |
| 2 | + |
| 3 | +Comprehensive backup and recovery procedures for GhidraInsight. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +GhidraInsight backup strategy includes: |
| 8 | +- Database backups (PostgreSQL) |
| 9 | +- Configuration backups |
| 10 | +- Binary analysis cache backups |
| 11 | +- Disaster recovery procedures |
| 12 | + |
| 13 | +## Database Backup |
| 14 | + |
| 15 | +### Automated Backups |
| 16 | + |
| 17 | +#### PostgreSQL (Docker) |
| 18 | + |
| 19 | +```bash |
| 20 | +# Create backup script |
| 21 | +cat > scripts/backup-db.sh << 'EOF' |
| 22 | +#!/bin/bash |
| 23 | +BACKUP_DIR="/backups/postgres" |
| 24 | +DATE=$(date +%Y%m%d_%H%M%S) |
| 25 | +mkdir -p $BACKUP_DIR |
| 26 | +
|
| 27 | +# Backup database |
| 28 | +docker exec postgres pg_dump -U ghidrauser ghidrainsight | gzip > $BACKUP_DIR/ghidrainsight_$DATE.sql.gz |
| 29 | +
|
| 30 | +# Keep only last 30 days |
| 31 | +find $BACKUP_DIR -name "*.sql.gz" -mtime +30 -delete |
| 32 | +EOF |
| 33 | + |
| 34 | +chmod +x scripts/backup-db.sh |
| 35 | + |
| 36 | +# Add to crontab (daily at 2 AM) |
| 37 | +echo "0 2 * * * /path/to/scripts/backup-db.sh" | crontab - |
| 38 | +``` |
| 39 | + |
| 40 | +#### PostgreSQL (Kubernetes) |
| 41 | + |
| 42 | +```yaml |
| 43 | +# k8s/postgres-backup-job.yaml |
| 44 | +apiVersion: batch/v1 |
| 45 | +kind: CronJob |
| 46 | +metadata: |
| 47 | + name: postgres-backup |
| 48 | + namespace: ghidrainsight |
| 49 | +spec: |
| 50 | + schedule: "0 2 * * *" # Daily at 2 AM |
| 51 | + jobTemplate: |
| 52 | + spec: |
| 53 | + template: |
| 54 | + spec: |
| 55 | + containers: |
| 56 | + - name: postgres-backup |
| 57 | + image: postgres:15-alpine |
| 58 | + command: |
| 59 | + - /bin/sh |
| 60 | + - -c |
| 61 | + - | |
| 62 | + pg_dump -h postgres -U ghidrauser ghidrainsight | gzip > /backup/ghidrainsight_$(date +%Y%m%d_%H%M%S).sql.gz |
| 63 | + env: |
| 64 | + - name: PGPASSWORD |
| 65 | + valueFrom: |
| 66 | + secretKeyRef: |
| 67 | + name: postgres-secret |
| 68 | + key: password |
| 69 | + volumeMounts: |
| 70 | + - name: backup-storage |
| 71 | + mountPath: /backup |
| 72 | + volumes: |
| 73 | + - name: backup-storage |
| 74 | + persistentVolumeClaim: |
| 75 | + claimName: backup-pvc |
| 76 | + restartPolicy: OnFailure |
| 77 | +``` |
| 78 | +
|
| 79 | +### Manual Backup |
| 80 | +
|
| 81 | +```bash |
| 82 | +# Docker |
| 83 | +docker exec postgres pg_dump -U ghidrauser ghidrainsight > backup.sql |
| 84 | + |
| 85 | +# Kubernetes |
| 86 | +kubectl exec -it postgres-xxx -n ghidrainsight -- pg_dump -U ghidrauser ghidrainsight > backup.sql |
| 87 | + |
| 88 | +# Direct connection |
| 89 | +pg_dump -h localhost -U ghidrauser ghidrainsight > backup.sql |
| 90 | +``` |
| 91 | + |
| 92 | +## Database Restore |
| 93 | + |
| 94 | +### From Backup File |
| 95 | + |
| 96 | +```bash |
| 97 | +# Docker |
| 98 | +gunzip -c backup.sql.gz | docker exec -i postgres psql -U ghidrauser ghidrainsight |
| 99 | + |
| 100 | +# Kubernetes |
| 101 | +gunzip -c backup.sql.gz | kubectl exec -i postgres-xxx -n ghidrainsight -- psql -U ghidrauser ghidrainsight |
| 102 | + |
| 103 | +# Direct connection |
| 104 | +gunzip -c backup.sql.gz | psql -h localhost -U ghidrauser ghidrainsight |
| 105 | +``` |
| 106 | + |
| 107 | +## Configuration Backup |
| 108 | + |
| 109 | +### Backup Configuration Files |
| 110 | + |
| 111 | +```bash |
| 112 | +# Create backup script |
| 113 | +cat > scripts/backup-config.sh << 'EOF' |
| 114 | +#!/bin/bash |
| 115 | +BACKUP_DIR="/backups/config" |
| 116 | +DATE=$(date +%Y%m%d_%H%M%S) |
| 117 | +mkdir -p $BACKUP_DIR |
| 118 | +
|
| 119 | +# Backup docker-compose.yml |
| 120 | +cp docker-compose.yml $BACKUP_DIR/docker-compose_$DATE.yml |
| 121 | +
|
| 122 | +# Backup .env files |
| 123 | +cp .env $BACKUP_DIR/env_$DATE 2>/dev/null || true |
| 124 | +
|
| 125 | +# Backup Kubernetes configs |
| 126 | +tar -czf $BACKUP_DIR/k8s_$DATE.tar.gz k8s/ helm/ 2>/dev/null || true |
| 127 | +
|
| 128 | +# Keep only last 30 days |
| 129 | +find $BACKUP_DIR -type f -mtime +30 -delete |
| 130 | +EOF |
| 131 | + |
| 132 | +chmod +x scripts/backup-config.sh |
| 133 | +``` |
| 134 | + |
| 135 | +## Cloud Storage Backups |
| 136 | + |
| 137 | +### AWS S3 |
| 138 | + |
| 139 | +```bash |
| 140 | +# Install AWS CLI |
| 141 | +pip install awscli |
| 142 | + |
| 143 | +# Configure |
| 144 | +aws configure |
| 145 | + |
| 146 | +# Backup to S3 |
| 147 | +aws s3 sync /backups/postgres s3://ghidrainsight-backups/postgres/ |
| 148 | +aws s3 sync /backups/config s3://ghidrainsight-backups/config/ |
| 149 | +``` |
| 150 | + |
| 151 | +### Google Cloud Storage |
| 152 | + |
| 153 | +```bash |
| 154 | +# Install gsutil |
| 155 | +pip install gsutil |
| 156 | + |
| 157 | +# Configure |
| 158 | +gcloud auth login |
| 159 | + |
| 160 | +# Backup to GCS |
| 161 | +gsutil -m cp -r /backups/postgres gs://ghidrainsight-backups/ |
| 162 | +gsutil -m cp -r /backups/config gs://ghidrainsight-backups/ |
| 163 | +``` |
| 164 | + |
| 165 | +### Azure Blob Storage |
| 166 | + |
| 167 | +```bash |
| 168 | +# Install Azure CLI |
| 169 | +az storage blob upload-batch \ |
| 170 | + --account-name ghidrainsight \ |
| 171 | + --destination backups \ |
| 172 | + --source /backups/postgres |
| 173 | +``` |
| 174 | + |
| 175 | +## Disaster Recovery |
| 176 | + |
| 177 | +### Recovery Procedures |
| 178 | + |
| 179 | +#### 1. Full System Recovery |
| 180 | + |
| 181 | +```bash |
| 182 | +# 1. Restore database |
| 183 | +gunzip -c backup.sql.gz | psql -h localhost -U ghidrauser ghidrainsight |
| 184 | + |
| 185 | +# 2. Restore configuration |
| 186 | +cp /backups/config/docker-compose_YYYYMMDD.yml docker-compose.yml |
| 187 | +cp /backups/config/env_YYYYMMDD .env |
| 188 | + |
| 189 | +# 3. Restart services |
| 190 | +docker-compose down |
| 191 | +docker-compose up -d |
| 192 | +``` |
| 193 | + |
| 194 | +#### 2. Point-in-Time Recovery |
| 195 | + |
| 196 | +```bash |
| 197 | +# Enable WAL archiving in postgresql.conf |
| 198 | +wal_level = replica |
| 199 | +archive_mode = on |
| 200 | +archive_command = 'cp %p /backups/wal/%f' |
| 201 | + |
| 202 | +# Restore to specific time |
| 203 | +pg_basebackup -D /restore -Ft -z -P |
| 204 | +# Then apply WAL files up to target time |
| 205 | +``` |
| 206 | + |
| 207 | +#### 3. Multi-Region Recovery |
| 208 | + |
| 209 | +```bash |
| 210 | +# Setup replication |
| 211 | +# Primary: us-east-1 |
| 212 | +# Standby: us-west-2 |
| 213 | + |
| 214 | +# Promote standby to primary |
| 215 | +pg_ctl promote -D /var/lib/postgresql/data |
| 216 | +``` |
| 217 | + |
| 218 | +## Backup Verification |
| 219 | + |
| 220 | +### Test Restore |
| 221 | + |
| 222 | +```bash |
| 223 | +# Create test database |
| 224 | +createdb ghidrainsight_test |
| 225 | + |
| 226 | +# Restore backup |
| 227 | +gunzip -c backup.sql.gz | psql ghidrainsight_test |
| 228 | + |
| 229 | +# Verify |
| 230 | +psql ghidrainsight_test -c "SELECT COUNT(*) FROM binary_analyses;" |
| 231 | +``` |
| 232 | + |
| 233 | +## Automation |
| 234 | + |
| 235 | +### Backup Monitoring |
| 236 | + |
| 237 | +```python |
| 238 | +# scripts/backup-monitor.py |
| 239 | +import os |
| 240 | +import subprocess |
| 241 | +from datetime import datetime, timedelta |
| 242 | + |
| 243 | +def check_backup_age(backup_dir): |
| 244 | + """Check if backups are recent.""" |
| 245 | + files = os.listdir(backup_dir) |
| 246 | + if not files: |
| 247 | + return False |
| 248 | + |
| 249 | + latest = max(files, key=lambda f: os.path.getmtime(os.path.join(backup_dir, f))) |
| 250 | + latest_time = datetime.fromtimestamp(os.path.getmtime(os.path.join(backup_dir, latest))) |
| 251 | + |
| 252 | + return datetime.now() - latest_time < timedelta(days=1) |
| 253 | + |
| 254 | +if __name__ == "__main__": |
| 255 | + if not check_backup_age("/backups/postgres"): |
| 256 | + print("WARNING: No recent backup found!") |
| 257 | + # Send alert |
| 258 | +``` |
| 259 | + |
| 260 | +## Best Practices |
| 261 | + |
| 262 | +1. **Automate Backups**: Use cron jobs or Kubernetes CronJobs |
| 263 | +2. **Test Restores**: Regularly test backup restoration |
| 264 | +3. **Offsite Storage**: Store backups in cloud storage |
| 265 | +4. **Encryption**: Encrypt sensitive backups |
| 266 | +5. **Retention Policy**: Keep backups for appropriate duration |
| 267 | +6. **Monitoring**: Monitor backup success/failure |
| 268 | +7. **Documentation**: Document recovery procedures |
| 269 | + |
| 270 | +## Recovery Time Objectives (RTO) |
| 271 | + |
| 272 | +- **Database**: < 1 hour |
| 273 | +- **Configuration**: < 15 minutes |
| 274 | +- **Full System**: < 4 hours |
| 275 | + |
| 276 | +## Recovery Point Objectives (RPO) |
| 277 | + |
| 278 | +- **Database**: < 24 hours (daily backups) |
| 279 | +- **Configuration**: < 1 hour (version control) |
| 280 | +- **Cache**: Acceptable to lose (can regenerate) |
0 commit comments