Files
system-docs/04_INFRAESTRUCTURA/backup-recovery.md
ARCHITECT 6ea70bd34f Update to Skynet v7 - Complete documentation restructure
- Nueva estructura de carpetas según Skynet v7
- Añadidos schemas SQL completos
- Documentación de entidades, componentes e integraciones
- Modelo de seguridad actualizado
- Infraestructura y operaciones reorganizadas
2025-12-29 18:23:41 +00:00

132 lines
2.8 KiB
Markdown

# Backup y Recovery
**Versión:** 1.0
**Estado:** Definición
---
## Estrategia
| Tipo | Frecuencia | Retención | Destino |
|------|------------|-----------|---------|
| **PostgreSQL** | Diario | 30 días | R2 |
| **Gitea repos** | Por commit | Indefinido | R2 |
| **Archivos** | Diario | 30 días | R2 |
| **Configuración** | Por cambio | Indefinido | Gitea |
---
## Backup PostgreSQL
### Script
```bash
#!/bin/bash
# backup-postgres.sh
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="postgres_backup_${DATE}.sql.gz"
# Dump
pg_dump -U $DB_USER -h $DB_HOST $DB_NAME | gzip > /tmp/$BACKUP_FILE
# Upload a R2
aws s3 cp /tmp/$BACKUP_FILE s3://$R2_BUCKET/backups/postgres/$BACKUP_FILE \
--endpoint-url $R2_ENDPOINT
# Limpiar local
rm /tmp/$BACKUP_FILE
# Limpiar antiguos (>30 días)
aws s3 ls s3://$R2_BUCKET/backups/postgres/ --endpoint-url $R2_ENDPOINT | \
while read -r line; do
createDate=$(echo $line | awk '{print $1}')
if [[ $(date -d "$createDate" +%s) -lt $(date -d "30 days ago" +%s) ]]; then
fileName=$(echo $line | awk '{print $4}')
aws s3 rm s3://$R2_BUCKET/backups/postgres/$fileName --endpoint-url $R2_ENDPOINT
fi
done
```
### Cron
```bash
0 3 * * * /opt/scripts/backup-postgres.sh
```
---
## Recovery PostgreSQL
```bash
# Descargar backup
aws s3 cp s3://$R2_BUCKET/backups/postgres/$BACKUP_FILE /tmp/ \
--endpoint-url $R2_ENDPOINT
# Restaurar
gunzip -c /tmp/$BACKUP_FILE | psql -U $DB_USER -h $DB_HOST $DB_NAME
```
---
## Backup Gitea
Los repositorios bare se sincronizan automáticamente a R2:
```bash
#!/bin/bash
# backup-gitea.sh
rsync -av /var/lib/gitea/git/repositories/ /tmp/gitea_repos/
tar -czf /tmp/gitea_backup_$(date +%Y%m%d).tar.gz -C /tmp gitea_repos/
aws s3 cp /tmp/gitea_backup_*.tar.gz s3://$R2_BUCKET/backups/gitea/ \
--endpoint-url $R2_ENDPOINT
```
---
## Disaster Recovery
### Escenario: Pérdida total del servidor
1. Aprovisionar nuevo servidor
2. Instalar Docker
3. Clonar repos de configuración desde R2/Gitea backup
4. Restaurar PostgreSQL desde R2
5. `docker-compose up -d`
6. Verificar servicios
7. Actualizar DNS
### RTO/RPO
| Métrica | Objetivo |
|---------|----------|
| **RPO** (Recovery Point Objective) | 24 horas |
| **RTO** (Recovery Time Objective) | 4 horas |
---
## Verificación de Backups
```bash
# Verificar integridad mensualmente
#!/bin/bash
# Descargar último backup
LATEST=$(aws s3 ls s3://$R2_BUCKET/backups/postgres/ --endpoint-url $R2_ENDPOINT | tail -1 | awk '{print $4}')
aws s3 cp s3://$R2_BUCKET/backups/postgres/$LATEST /tmp/ --endpoint-url $R2_ENDPOINT
# Restaurar en BD temporal
createdb -U $DB_USER test_restore
gunzip -c /tmp/$LATEST | psql -U $DB_USER test_restore
# Verificar tablas
psql -U $DB_USER test_restore -c "\dt"
# Limpiar
dropdb -U $DB_USER test_restore
```