Skip to content

Local backup: daily scheduled + before destroy#6

Open
titouanmathis wants to merge 9 commits intomainfrom
feature/1-local-backup
Open

Local backup: daily scheduled + before destroy#6
titouanmathis wants to merge 9 commits intomainfrom
feature/1-local-backup

Conversation

@titouanmathis
Copy link
Contributor

Summary

Closes #1 (partial — local backup only, remote storage to come later).

Adds local backup support to trafic with two triggers:

  • Daily scheduled backups via the agent (configurable hour, retention)
  • Automatic backup before destroy in the CLI

Agent changes (packages/trafic-agent/)

New types

  • BackupConfig — configuration for [backup] TOML section
  • BackupResult / BackupEntry — result and listing types

Config (src/utils/config.ts)

  • Parse [backup] section with defaults: disabled, 3am schedule, 7-day retention, /var/backups/trafic

Core logic (src/tasks/backup.ts)

  • backupProjectDb() — exports a project DB via ddev export-db --gzip
  • backupAgentData() — copies agent SQLite DB and config file
  • runBackup() — backup all or a specific project
  • listBackups() — list available backups grouped by date
  • cleanOldBackups() — remove backups older than retention period
  • restoreProjectDb() — restore via ddev import-db
  • findBackup() — find latest or date-specific backup file

Auto-starts stopped projects before export/import. Backups stored as <localDir>/<YYYY-MM-DD>/<project>.sql.gz.

Scheduler (src/tasks/backup-scheduler.ts)

  • Simple daily scheduler: checks every 30min, runs once per day after the configured hour
  • Runs cleanup after each backup

CLI commands (src/cli.ts)

  • trafic-agent backup — backup all projects
  • trafic-agent backup --name my-app — backup a specific project
  • trafic-agent backup --list — list available backups
  • trafic-agent backup --clean — clean old backups
  • trafic-agent restore --name my-app — restore from latest backup
  • trafic-agent restore --name my-app --date 2026-02-07 — restore from specific date

CLI changes (packages/trafic-cli/)

Backup before destroy (src/commands/destroy.ts)

  • Runs trafic-agent backup --name <project> via SSH before ddev delete
  • Non-blocking: warns and continues if backup fails
  • Skip with --no-backup flag

Tests

  • 13 new tests for backup logic (listBackups, cleanOldBackups, findBackup)
  • Updated destroy tests for the new backup step (+2 new tests for --no-backup and backup failure)
  • All 92 tests pass (62 agent + 30 CLI)

Config example

[backup]
enabled = true
schedule_hour = 3
retain_days = 7
local_dir = "/var/backups/trafic"

Future work

  • Remote storage (S3, rsync, rclone) — separate PR
  • Uploads backup (tar project upload dirs) — separate PR

titouanmathis and others added 9 commits February 9, 2026 18:51
Add BackupConfig, BackupResult, and BackupEntry interfaces.
Parse [backup] section from TOML config with defaults:
- enabled: false
- schedule_hour: 3 (3am)
- retain_days: 7
- local_dir: /var/backups/trafic

Co-authored-by: Claude <claude@anthropic.com>
Implement backup.ts with:
- backupProjectDb: export a project DB via ddev export-db
- backupAgentData: copy agent SQLite DB and config
- runBackup: backup all or a specific project
- listBackups: list available backups by date
- cleanOldBackups: retention cleanup by date
- restoreProjectDb: restore via ddev import-db
- findBackup: find latest or date-specific backup file

Auto-starts stopped projects before export/import.
Backups stored as <localDir>/<YYYY-MM-DD>/<project>.sql.gz.

Co-authored-by: Claude <claude@anthropic.com>
Add backup command with --name, --list, --clean options.
Add restore command with --name, --date, --file options.
Add backup-scheduler.ts for daily scheduled backups.
Start backup scheduler alongside idle scheduler when backup.enabled is true.

Co-authored-by: Claude <claude@anthropic.com>
Run trafic-agent backup --name <project> via SSH before deleting
a project. Failure is non-blocking (warns and continues).
Add --no-backup flag to skip the backup step.

Co-authored-by: Claude <claude@anthropic.com>
Add 13 tests for backup logic (listBackups, cleanOldBackups, findBackup).
Update destroy tests for the new backup-before-destroy step:
- Expect 3 exec calls (backup + delete + rm)
- Add test for --no-backup flag
- Add test for backup failure being non-blocking

Co-authored-by: Claude <claude@anthropic.com>
Add [backup] section to examples/config.toml with documentation.
Export backup types and functions from the agent's public API.

Co-authored-by: Claude <claude@anthropic.com>
Use a finally block to ensure projects that were stopped before
backup are stopped again after export, whether it succeeds or fails.
Prevents daily backups from leaving all projects running.

Co-authored-by: Claude <claude@anthropic.com>
Avoid starting all stopped projects during scheduled backups.
Instead:
- Idle scheduler backs up each project before stopping it
- Scheduled daily backup only exports running projects
- Manual CLI backup with --name forces start for a specific project
- trafic-agent backup (all) skips stopped projects

This ensures every project gets a fresh backup before going idle,
without the resource cost of starting everything for a full backup.

Co-authored-by: Claude <claude@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backup solution for DDEV preview servers

1 participant

Comments