feat(db-hardening): Add database persistence, backups, and MinIO assets storage
Database Hardening: - Add docker-compose.db.yml with persistent PostgreSQL volume - Add automatic DB backups every 12h (7 days, 4 weeks, 6 months retention) - Add MinIO S3-compatible storage for assets Assets Migration: - Add MinIO client (lib/assets_client.py) for upload/delete - Update upload endpoint to use MinIO (with local fallback) - Add migration 043_asset_urls_to_text.sql for full HTTPS URLs - Simplify normalizeAssetUrl for S3 URLs Recovery: - Add seed_full_city_reset.py for emergency city recovery - Add DB_RESTORE.md with backup restore instructions - Add SEED_RECOVERY.md with recovery procedures - Add INFRA_ASSETS_MINIO.md with MinIO setup guide Task: TASK_PHASE_DATABASE_HARDENING_AND_ASSETS_MIGRATION_v1
This commit is contained in:
138
docs/DB_RESTORE.md
Normal file
138
docs/DB_RESTORE.md
Normal file
@@ -0,0 +1,138 @@
|
||||
# DB_RESTORE — Як відновити базу daarion
|
||||
|
||||
## 1. Відновлення з останнього бекапу
|
||||
|
||||
### Крок 1: Зупинити сервіси
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.db.yml stop db
|
||||
docker compose -f docker-compose.web.yml stop city-service web
|
||||
```
|
||||
|
||||
### Крок 2: Відновити базу з бекапу
|
||||
|
||||
```bash
|
||||
# Знайти останній бекап
|
||||
ls -lt db_backups/ | head -5
|
||||
|
||||
# Відновити базу
|
||||
docker compose -f docker-compose.db.yml exec db psql -U postgres -d postgres -c "DROP DATABASE IF EXISTS daarion;"
|
||||
docker compose -f docker-compose.db.yml exec db psql -U postgres -d postgres -c "CREATE DATABASE daarion;"
|
||||
docker compose -f docker-compose.db.yml exec -T db psql -U postgres -d daarion < db_backups/daarion_YYYY-MM-DD_HH-MM.sql
|
||||
```
|
||||
|
||||
Або через docker exec:
|
||||
|
||||
```bash
|
||||
# Скопіювати бекап в контейнер
|
||||
docker cp db_backups/daarion_2025-12-02_09-00.sql daarion-postgres:/tmp/backup.sql
|
||||
|
||||
# Відновити
|
||||
docker exec daarion-postgres psql -U postgres -d postgres -c "DROP DATABASE IF EXISTS daarion;"
|
||||
docker exec daarion-postgres psql -U postgres -d postgres -c "CREATE DATABASE daarion;"
|
||||
docker exec -i daarion-postgres psql -U postgres -d daarion < /tmp/backup.sql
|
||||
```
|
||||
|
||||
### Крок 3: Перезапустити сервіси
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.db.yml up -d db
|
||||
docker compose -f docker-compose.web.yml up -d city-service web
|
||||
```
|
||||
|
||||
### Крок 4: Перевірити
|
||||
|
||||
```bash
|
||||
# Перевірити що база працює
|
||||
docker exec daarion-postgres psql -U postgres -d daarion -c "SELECT COUNT(*) FROM microdao;"
|
||||
|
||||
# Перевірити API
|
||||
curl https://daarion.space/api/city/microdao?limit=1
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 2. Якщо бекапу немає (аварійне відновлення)
|
||||
|
||||
### Крок 1: Застосувати міграції
|
||||
|
||||
```bash
|
||||
cd /opt/microdao-daarion
|
||||
for f in migrations/*.sql; do
|
||||
echo "Applying: $f"
|
||||
docker exec -i daarion-postgres psql -U postgres -d daarion < "$f"
|
||||
done
|
||||
```
|
||||
|
||||
### Крок 2: Запустити seed-скрипт
|
||||
|
||||
```bash
|
||||
python3 scripts/seed_full_city_reset.py
|
||||
```
|
||||
|
||||
### Крок 3: Відновити DAGI-агентів NODE2
|
||||
|
||||
```bash
|
||||
python3 scripts/sync-node2-dagi-agents.py
|
||||
```
|
||||
|
||||
### Крок 4: Перевірити
|
||||
|
||||
```bash
|
||||
# Перевірити MicroDAOs
|
||||
docker exec daarion-postgres psql -U postgres -d daarion -c "SELECT slug, name FROM microdao;"
|
||||
|
||||
# Перевірити агентів
|
||||
docker exec daarion-postgres psql -U postgres -d daarion -c "SELECT COUNT(*) FROM agents;"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. Перевірка бекапів
|
||||
|
||||
### Список доступних бекапів
|
||||
|
||||
```bash
|
||||
ls -lh db_backups/
|
||||
```
|
||||
|
||||
### Структура бекапів
|
||||
|
||||
- `daarion_YYYY-MM-DD_HH-MM.sql` - щоденні бекапи (зберігаються 7 днів)
|
||||
- `daarion_YYYY-MM-DD_HH-MM.sql` - тижневі бекапи (зберігаються 4 тижні)
|
||||
- `daarion_YYYY-MM-DD_HH-MM.sql` - місячні бекапи (зберігаються 6 місяців)
|
||||
|
||||
### Створити бекап вручну
|
||||
|
||||
```bash
|
||||
docker exec daarion-postgres pg_dump -U postgres daarion > db_backups/manual_$(date +%F_%H-%M).sql
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. Troubleshooting
|
||||
|
||||
### Проблема: "database does not exist"
|
||||
|
||||
```bash
|
||||
# Створити базу
|
||||
docker exec daarion-postgres psql -U postgres -d postgres -c "CREATE DATABASE daarion;"
|
||||
```
|
||||
|
||||
### Проблема: "connection refused"
|
||||
|
||||
```bash
|
||||
# Перевірити статус контейнера
|
||||
docker ps | grep postgres
|
||||
|
||||
# Перезапустити
|
||||
docker compose -f docker-compose.db.yml restart db
|
||||
```
|
||||
|
||||
### Проблема: "permission denied" при відновленні
|
||||
|
||||
```bash
|
||||
# Перевірити права на файл бекапу
|
||||
chmod 644 db_backups/daarion_*.sql
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user