# Tasks: Email Migration (013)

**Spec:** [spec.md](./spec.md) · **Plan:** [plan.md](./plan.md)

---

## Fase A — Fundação (P0)

- [ ] **T001** Criar pasta `specs/013-email-server-migration/` ✅ (este sprint)
- [ ] **T002** Migration SQL schema + migrations em `api/app/db.py`
- [ ] **T003** `app/migration/schemas.py` — Pydantic models
- [ ] **T004** `app/migration/store.py` — CRUD jobs/mailboxes/runs
- [ ] **T005** `app/migration/credentials.py` — Fernet encrypt
- [ ] **T006** `app/migration/router.py` — POST/GET jobs, list
- [ ] **T007** `permissions.py` — `can_manage_migration`, `can_approve_migration_gate`
- [ ] **T008** `scripts/install-migration-tools.sh` — imapsync, pst-utils, imap-upload
- [ ] **T009** Frontend: menu + `view-email-migration` lista jobs
- [ ] **T010** Associar job a `ticket_id` (Spec 010)

**Entrega Fase A:** criar job, listar, ver fase, sem sync ainda.

---

## Fase B — IMAP sync + Gate (P0)

- [ ] **T011** `tools/imapsync_runner.py` — gera comando, executa, captura log
- [ ] **T012** `tools/log_parser.py` — parse imapsync LOG para stats_json
- [ ] **T013** `worker/migration_runner.py` — fila Redis/SQLite job runner
- [ ] **T014** POST `/migration/jobs/{id}/preflight` — justlogin origem+destino
- [ ] **T015** POST `/migration/jobs/{id}/sync` — initial | delta | final
- [ ] **T016** `gate.py` — calcular migration_gate (ratio 99%)
- [ ] **T017** GET `/migration/gate?domain=` — público para wizard (auth token)
- [ ] **T018** POST `/migration/jobs/{id}/approve-gate` — ops_lead
- [ ] **T019** UI: detalhe job, botões sync, badge gate, log viewer
- [ ] **T020** `scripts/verify-migration.sh` — teste integração

**Entrega Fase B:** migração IMAP completa com gate DNS.

---

## Fase C — PST / mbox (P0)

- [ ] **T021** Volume Docker `/data/migrations` (uploads, logs, quarantine)
- [ ] **T022** POST `/migration/jobs/{id}/upload-pst` — multipart
- [ ] **T023** `tools/pst_runner.py` — readpst + imap-upload recursivo
- [ ] **T024** Quarentena ERR_MBOX + UI lista erros
- [ ] **T025** `verify.py` — contagens pós-PST
- [ ] **T026** UI: upload PST, progresso por pasta

**Entrega Fase C:** pipeline PST auditável.

---

## Fase D — TGZ + integração wizard (P1)

- [ ] **T027** `tools/tgz_runner.py` — SSH zmmailbox export/import
- [ ] **T028** VM112: consulta gate antes DNS (API client)
- [ ] **T029** Webhook `migration.gate_ready` → Desk
- [ ] **T030** Relatório JSON/PDF export no ticket

---

## Fase F — Infraestrutura (FUTURO — não hoje)

> Decisão Roger 2026-06-10: sizing VM guardado em [infrastructure.md](./infrastructure.md). Revisar antes da primeira migração PST em produção.

- [ ] **T036** Avaliar carga real vs Opção A (VM122) vs Opção B (VM123)
- [ ] **T037** Provisionar VM123 `ligbox-migration` (4 vCPU / 8 GB / 200 GB) — se necessário
- [ ] **T038** Volume +100 GB em VM122 — alternativa piloto
- [ ] **T039** DNAT/FORWARD SSH WAN para VM123 (range 2501–2510)
- [ ] **T040** `install-migration-tools.sh` no host worker escolhido

---

## Fase E — Melhorias (P2)

- [ ] **T031** Integração pst2mbox (https://github.com/beaufour/pst2mbox)
- [ ] **T032** OAuth2 UI para O365 (oauth2_imap)
- [ ] **T033** Cron delta sync automático (6h)
- [ ] **T034** doveadm backup path (Dovecot→Dovecot)
- [ ] **T035** Dashboard métricas migração no Overview Home

---

## Dependências externas

| Pacote | Instalação |
|--------|------------|
| imapsync | apt / imapsync.lamiral.info |
| pst-utils (readpst) | `apt install pst-utils` |
| imap-upload | git clone rgladwell/imap-upload |
| python cryptography | Fernet credentials |

---

## Definition of Done (013)

- [ ] Spec + research + plan + data-model + tasks + quickstart documentados
- [ ] Fase B entregue em staging com 1 migração IMAP real
- [ ] Fase C com 1 PST teste < 2 GB
- [ ] Gate bloqueia e libera DNS com auditoria
- [ ] BACKLOG.md actualizado
