beads/cmd/bd/backup.go
Julian Knutsen 4e4ebdb443
feat(backup): proxied auto-backup on managed-local, serialized with bd backup sync (#6879)
* feat(backup): run opted-in auto-backup on a managed-local proxied server

#6584 routed the five `bd backup` verbs over the proxied provider on
managed-local, and #6694 made `bd backup status` admit the remaining gap:
the proxied arm of PersistentPostRunE never called runPostRunAutoBackup,
so backup.enabled=true was inert on every proxied workspace. Close it.

The proxied arm now calls the auto-backup hook before it closes the
provider, under the direct arm's maintenance gate (strict --readonly,
`bd serve`, a migration freeze) plus previews. maybeAutoBackup picks a
backend for the command: the direct store as before, or the proxied
provider's RunNonTx seam, which `bd backup sync` already uses.

The proxied backend is honored exactly where `bd backup sync` is: the
"backup sync" capability row's rule for this workspace's topology, so
managed-local only. Auto-backup registers a server-side backup remote,
which on a server bd does not own is global to every client; an implicit
trigger must not get further than the explicit verb. It never opens a
provider of its own: a command that opened none (`bd dolt stop`) or
closed it (restore) backs nothing up rather than relaunching the server.
The default stays OFF, as on every server-mode workspace.

The register/sync-to-a-local-dir sequence that DoltStore and
EmbeddedDoltStore each carried a copy of moves to
versioncontrolops.BackupToDir (and HEAD lookup to CurrentCommit), so the
proxied backend reuses it instead of adding a third copy.

Tests: TestManagedLocalProxiedAutoBackup (default off, opt-in writes a
restorable backup, change detection, `bd dolt stop` stays quiescent,
restore round trip with issue-set equality; fails against origin/main);
TestManagedLocalProxiedBackupRestoreRefusedWhileAttached pins the restore
design (refused while another bd client holds the workspace, serving
child untouched, succeeds once it detaches); unit tests for the backend
locality gate, the post-run wiring, the status note and BackupToDir.
Both managed-local tests, and the existing round trip, are added to the
proxied-local smoke lane's compiled-in list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(backup): serialize auto-backup and bd backup sync with a workspace backup lock

Review follow-up on #6879. Auto-backup and an explicit bd backup sync
both run under the command's SHARED workspace gate, so nothing stopped
them overlapping, and auto-backup's remove -> add -> sync on one fixed
remote name races a concurrent copy of itself.

A per-workspace flock (.beads/backup.lock, already gitignored by
*.lock) now serializes them in every mode. Auto-backup takes it
non-blocking and skips when held (the throttle is left alone, so a
later command retries). bd backup sync waits up to exclusiveGateWait -
the bound restore's exclusive gate acquisition uses - then fails with
"another backup is running for this workspace" before touching the
store. Restore needs no part in it: its exclusive workspace gate
already excludes every command that could hold the lock.

Tests: lock semantics (refusal while held, bounded wait, release),
auto-backup skipping while held and running once released, and sync
failing loudly without reaching the store.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 02:45:58 -07:00

157 lines
4.6 KiB
Go

package main
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/metrics"
)
var backupCmd = &cobra.Command{
Use: "backup",
Short: "Back up your beads database",
Long: `Back up your beads database for off-machine recovery.
This is a Dolt-native database backup. It preserves the database state,
including tables, branches, commit history, and working-set data. This is
different from 'bd export', which writes issue records to JSONL for migration
and interoperability.
Commands:
bd backup init <path> Set up a backup destination (filesystem or DoltHub)
bd backup sync Push to configured backup destination
bd backup restore [path] Restore from a backup directory
bd backup remove Remove backup destination
bd backup status Show backup status
DoltHub is recommended for cloud backup:
bd backup init https://doltremoteapi.dolthub.com/<user>/<repo>
Set DOLT_REMOTE_USER and DOLT_REMOTE_PASSWORD for authentication.
Auto-backup default:
When backup.enabled is unset, auto-backup turns ON in embedded mode if a
git remote exists, and stays OFF in sql-server / shared-server mode. In
server mode many bd clients share one Dolt server, and each would register
a server-side backup remote under the same name pointing at its own local
dir and full-sync the whole database — a self-amplifying storm. To back up
a shared server, run 'bd backup' explicitly (or set backup.enabled=true and
coordinate destinations). 'bd config get backup.enabled' shows the effective
value and its source.
A proxied-server workspace is server mode too: auto-backup is OFF by
default and backup.enabled=true opts in. Both the opt-in and the five
backup verbs above are honored only on a managed-local proxied server —
one bd started itself; on a Dolt server bd does not own they are refused.`,
GroupID: "sync",
}
type backupSizeFunc func(context.Context) (bytes int64, available bool, err error)
var backupStatusCmd = newBackupStatusCommand(doltBackupSize)
func newBackupStatusCommand(sizeDatabase backupSizeFunc) *cobra.Command {
return &cobra.Command{
Use: "status",
Short: "Show last backup status",
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
if usesProxiedServer() {
if err := requireLocalProxiedBackup("backup status"); err != nil {
return err
}
}
evt := metrics.NewCommandEvent("backup-status")
defer func() {
if c := metrics.Global(); c != nil {
c.CloseEventAndAdd(evt)
}
}()
dir, err := backupDir()
if err != nil {
return err
}
state, err := loadBackupState(dir)
if err != nil {
return err
}
databaseSize, sizeAvailable, err := sizeDatabase(cmd.Context())
if err != nil {
return HandleErrorRespectJSON("measure database size: %v", err)
}
if jsonOutput {
result := map[string]interface{}{
"backup": state,
"dolt": showDoltBackupStatusJSON(),
}
if sizeAvailable {
result["database_size"] = showDBSizeJSON(databaseSize)
}
data, err := json.MarshalIndent(result, "", " ")
if err != nil {
return err
}
fmt.Println(string(data))
return nil
}
hasBackup := state.LastDoltCommit != ""
hasDolt := false
if cfg, _ := loadDoltBackupConfig(); cfg != nil {
hasDolt = true
}
if !hasBackup && !hasDolt {
fmt.Println("No backup has been performed yet.")
fmt.Println()
fmt.Println("Setup:")
fmt.Println(" bd backup init <path> Set up a backup destination")
fmt.Println(" bd backup sync Push to backup destination")
if sizeAvailable {
showDBSize(databaseSize)
}
return nil
}
if hasBackup {
fmt.Println("Backup:")
fmt.Printf(" Last backup: %s (%s ago)\n",
state.Timestamp.Format(time.RFC3339),
time.Since(state.Timestamp).Round(time.Second))
fmt.Printf(" Dolt commit: %s\n", state.LastDoltCommit)
}
// Show config (effective values with source)
enabled := isBackupAutoEnabled()
interval := config.GetDuration("backup.interval")
enabledNote := ""
if note := backupAutoStatusNote(enabled); note != "" {
enabledNote = " (" + note + ")"
}
fmt.Printf("\nConfig: enabled=%v%s interval=%s\n",
enabled, enabledNote, interval)
// Show Dolt backup info
showDoltBackupStatus()
// Show database size when the active backend can measure it locally.
if sizeAvailable {
showDBSize(databaseSize)
}
return nil
},
}
}
func init() {
backupCmd.AddCommand(backupStatusCmd)
rootCmd.AddCommand(backupCmd)
}