Writing a backup strategy
A “strategy” knows how to dump and restore one kind of data store (Postgres, MySQL, Redis, raw volumes, …). To add one, implement the Strategy interface in internal/backup/ and register it.
Interface
Section titled “Interface”type Strategy interface { Name() string // unique key, e.g. "postgres" Detect(svc compose.Service) Confidence // 0..1: should we suggest this? Backup(ctx context.Context, run *Run) error // produce an artifact Restore(ctx context.Context, run *Run, src io.Reader) error}Run carries the app slug, target writer, working dir, and labels. The strategy is responsible for streaming the dump into run.Out and reporting bytes written.
Reference: Postgres
Section titled “Reference: Postgres”internal/backup/postgres.go:
- Detect: returns
0.95if the service image starts withpostgres:orbitnami/postgresql;0otherwise. - Backup: shells
pg_dump --format=custom --compress=6inside the container viadocker exec, captures stdout intorun.Out. ReadsPOSTGRES_USER/POSTGRES_DBfrom the service env. - Restore:
pg_restore --clean --if-exists.
Use it as the canonical template.
Steps to add a new strategy
Section titled “Steps to add a new strategy”- Create
internal/backup/<name>.goimplementingStrategy. - Register the strategy in the factory (search for where Postgres is registered; add yours next to it).
- Add a unit test that exercises
Detectagainst a few compose services. - Add an integration test that runs
BackupandRestoreagainst a real container fixture (use the same pattern asinternal/backup/postgres_test.go). - Add a row to the strategies table in Compose labels and a guide page under
docs/guides/backups/<name>.md. - Mention the new strategy in
docs/architecture/backup.md.
Constraints
Section titled “Constraints”- Network: only reach the target via the project’s docker network. Do not expose ports.
- Credentials: read from compose env vars or
*_FILEsecrets; never log them. - Output: stream into
run.Out. Do not buffer entire dumps in memory. - Idempotency: a failed backup must be safe to retry.
- Cancellation: honor
ctx; long-running dumps must exit when cancelled.
Submit
Section titled “Submit”Open a PR with the strategy, tests, and docs. Tag it feat(backup): <name> strategy.