* fix(cli): refuse backup init/sync/remove and import under --readonly
Strict --readonly is meant to refuse every command that writes, with
"Error: operation '<op>' is not allowed in read-only mode" and exit 1.
bd backup init, backup sync, backup remove and bd import never called
CheckReadonly, so that gate never refused them:
- On a server-mode store they ran to completion under --readonly:
backup init and backup remove rewrote or emptied the server's
dolt_backups registry, backup sync wrote the backup destination, and
import wrote issues.
- On an embedded store they failed only because the store was opened
read-only, with the storage layer's "store is read-only" text, and
backup sync took the backup lock first and left .beads/backup.lock
behind.
The three backup verbs now call CheckReadonly first in their RunE, as
the other write commands do. Its migration-freeze half is redundant for
them: their leaf names are not on the read-only list and they have no
preview flag, so PersistentPreRunE's early freeze gate refuses them
first.
bd import calls it only when --readonly is set. CheckReadonly also
answers a migration freeze, and does so by exiting the process, while
runImport answers a freeze by returning the error so cleanup still
runs; guarding the call leaves that path exactly as it was.
bd import --dry-run is refused too. The preview opens the store
read-only and writes nothing, so this is a policy choice. It matches
create --dry-run (also refused under --readonly) and import's own
freeze posture: a migration freeze already refuses import --dry-run
(TestImportBlockedDuringMigrationFreeze), and CheckReadonly treats the
two gates as one. The cost is small, since the same preview runs
without --readonly. bd backup status and bd backup itself (a command
group that only prints help) stay allowed.
TestBackupAndImportBlockedInReadonlyMode runs each command, plus
import --dry-run, under --readonly and --readonly --sandbox against an
embedded workspace and one on the Dolt test server. Each row expects
exit 1, exactly the standard refusal on stderr, an empty stdout, and
nothing changed: the .beads tree, the backup destination, the issue
count, and, in server mode, the dolt_backups registry.
TestBackupStatusAndListWorkInReadonlyMode is the control showing that
backup status and list still run.
Ported from the versioned-beads fork: b86df6a9eb (test) and
5a067acc56 (fix). The comment on TestImportBlockedDuringMigrationFreeze
is updated, since runImport now calls CheckReadonly under --readonly.
Co-authored-by: Jim Wordelman <jim@wordelman.name>
Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(backup): refuse --readonly backup restore before the exclusive gate
A restore replaces the store, so strict --readonly must refuse it.
Nothing did:
- In server mode no storage layer stops the connection, so
`bd --readonly backup restore --force` ran to the end: exit 0,
"Restore complete", and the store was replaced.
- In embedded mode the command took the EXCLUSIVE workspace gate first,
leaving the holder sidecars (.beads.gate.lock.info,
.beads/embeddeddolt.gate.lock.info) behind, and was stopped only
later by the storage layer's "store is read-only".
acquireCommandWorkspaceGates now calls CheckReadonly("backup restore")
first for the exclusive (restore) case when readonlyMode is set. That
is before any gate file is created or the store is opened, so nothing
is held and the exit skips no release. The guard checks readonlyMode
alone, so a restore without --readonly, including one refused by a
migration freeze, keeps its existing order. The restore RunE also calls
CheckReadonly first, as every write command does.
This does not depend on the strict-readonly SHARED-gate change
(workspacegate.Options.ReadOnly): the refusal comes before any gate is
built.
The one visible change in proxied-server mode, where every --readonly
command is already refused: `bd --readonly backup restore` now gets
this standard refusal (plain text, exit 1). Before, the proxy
capability refusal answered it (exit 1, typed JSON under --json), and
only after the exclusive gate had been taken.
TestEmbeddedBackupRestoreStrictReadonly and
TestBackupRestoreStrictReadonlyServerMode run the built bd as a
subprocess, because CheckReadonly exits. The workspace's live store
holds three issues and its backup holds two. Each test asserts the
standard refusal with exit 1 and an empty stdout, no storage-layer
message, an unchanged project directory and .beads tree, and an
unchanged issue count. Each runs under --readonly and under
--readonly --sandbox. A control restores without --readonly and
expects two issues, so "unchanged" cannot be vacuous.
Ported from the versioned-beads fork: 1cd5f1c202 (test) and
6ac79d6207 (fix). The fork's test used a dirEntryNames helper that
belongs to its separate SHARED-gate change. It is inlined here so this
change compiles and lands on its own in either order. The server test's
comment is corrected too: the fixture's backup and the restore both run
inside the server at the same path, so it passes against a
containerized test server as well as a local one.
Co-authored-by: Jim Wordelman <jim@wordelman.name>
Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Jim Wordelman <jim@wordelman.name>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
499 lines
14 KiB
Go
499 lines
14 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/steveyegge/beads/internal/beads"
|
|
"github.com/steveyegge/beads/internal/metrics"
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/storage/versioncontrolops"
|
|
)
|
|
|
|
// --- Dolt-native backup commands ---
|
|
//
|
|
// These wrap Dolt's built-in backup feature (CALL DOLT_BACKUP(...)) for standalone
|
|
// users who want their beads database backed up to a filesystem path, NAS, or DoltHub.
|
|
//
|
|
// Unlike issue JSONL exports, Dolt-native backups preserve full commit history
|
|
// and are faster for large databases.
|
|
|
|
const defaultDoltBackupName = "default"
|
|
|
|
var backupInitCmd = &cobra.Command{
|
|
Use: "init <path>",
|
|
Aliases: []string{"add"},
|
|
Short: "Set up a Dolt backup destination",
|
|
Long: `Configure a filesystem path or URL as a backup destination.
|
|
|
|
The path can be a local directory (external drive, NAS, Dropbox folder) or a
|
|
DoltHub remote URL. If the destination was previously configured, it is
|
|
updated to the new path.
|
|
|
|
Filesystem examples:
|
|
bd backup add /mnt/usb/beads-backup
|
|
bd backup add ~/Dropbox/beads-backup
|
|
|
|
DoltHub (recommended for cloud backup):
|
|
bd backup add https://doltremoteapi.dolthub.com/myuser/beads-backup
|
|
|
|
After adding, run 'bd backup sync' to push your data.`,
|
|
Args: cobra.ExactArgs(1),
|
|
// As on `backup status`: the failures here are configuration and I/O
|
|
// errors, not usage errors, and cobra's default prints the message, then
|
|
// "Error: exit code 1", then the whole usage block — three things where one
|
|
// was wanted, and on --json it lands after the JSON object.
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
CheckReadonly("backup init")
|
|
|
|
evt := metrics.NewCommandEvent("backup-init")
|
|
defer func() {
|
|
if c := metrics.Global(); c != nil {
|
|
c.CloseEventAndAdd(evt)
|
|
}
|
|
}()
|
|
|
|
ctx := rootCtx
|
|
rawPath := args[0]
|
|
|
|
if usesProxiedServer() {
|
|
return runBackupInitProxied(ctx, rawPath)
|
|
}
|
|
|
|
if store == nil {
|
|
return fmt.Errorf("no store available")
|
|
}
|
|
|
|
// Resolve filesystem paths to absolute and add file:// prefix.
|
|
// DoltHub URLs are passed through as-is.
|
|
backupURL := resolveDoltBackupURL(rawPath)
|
|
|
|
bs, ok := storage.UnwrapStore(store).(storage.BackupStore)
|
|
if !ok {
|
|
return fmt.Errorf("storage backend does not support backup operations")
|
|
}
|
|
|
|
// Register the backup with Dolt
|
|
if err := bs.BackupAdd(ctx, defaultDoltBackupName, backupURL); err != nil {
|
|
if strings.Contains(err.Error(), "already exists") {
|
|
// Same name, different URL — remove and re-add to update
|
|
_ = bs.BackupRemove(ctx, defaultDoltBackupName)
|
|
if err := bs.BackupAdd(ctx, defaultDoltBackupName, backupURL); err != nil {
|
|
return fmt.Errorf("failed to update backup destination: %w", err)
|
|
}
|
|
} else if conflict := versioncontrolops.ExtractAddressConflictName(err); conflict != "" {
|
|
// Different name (e.g. "backup_export") points at same URL — remove it, re-add as "default"
|
|
_ = bs.BackupRemove(ctx, conflict)
|
|
if err := bs.BackupAdd(ctx, defaultDoltBackupName, backupURL); err != nil {
|
|
return fmt.Errorf("failed to add backup destination: %w", err)
|
|
}
|
|
} else {
|
|
return fmt.Errorf("failed to add backup destination: %w", err)
|
|
}
|
|
}
|
|
|
|
// Store the backup config in beads metadata for status display
|
|
if err := saveDoltBackupConfig(backupURL); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: backup registered but failed to save config: %v\n", err)
|
|
}
|
|
|
|
commandDidWrite.Store(true)
|
|
|
|
if jsonOutput {
|
|
return outputJSON(map[string]interface{}{
|
|
"backup_url": backupURL,
|
|
"backup_name": defaultDoltBackupName,
|
|
"initialized": true,
|
|
})
|
|
}
|
|
|
|
fmt.Printf("Backup destination configured: %s\n", backupURL)
|
|
fmt.Println("Run 'bd backup sync' to push your data.")
|
|
return nil
|
|
},
|
|
}
|
|
|
|
var backupSyncCmd = &cobra.Command{
|
|
Use: "sync",
|
|
Short: "Push database to configured Dolt backup",
|
|
Long: `Sync the current beads database to the configured Dolt backup destination.
|
|
|
|
This pushes the entire database state (all branches, full history) to the
|
|
backup location configured with 'bd backup init'.
|
|
|
|
The backup is atomic — if the sync fails, the previous backup state is preserved.
|
|
|
|
Run 'bd backup init <path>' first to configure a destination.`,
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
CheckReadonly("backup sync")
|
|
|
|
evt := metrics.NewCommandEvent("backup-sync")
|
|
defer func() {
|
|
if c := metrics.Global(); c != nil {
|
|
c.CloseEventAndAdd(evt)
|
|
}
|
|
}()
|
|
|
|
ctx := rootCtx
|
|
if usesProxiedServer() {
|
|
if err := requireLocalProxiedBackup("backup sync"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
release, err := acquireBackupLock(backupLockWait)
|
|
if err != nil {
|
|
if errors.Is(err, errBackupBusy) {
|
|
return HandleErrorRespectJSON("%v; retry when it completes", err)
|
|
}
|
|
return HandleErrorRespectJSON("%v", err)
|
|
}
|
|
defer release()
|
|
|
|
if usesProxiedServer() {
|
|
return runBackupSyncProxied(ctx)
|
|
}
|
|
if store == nil {
|
|
return fmt.Errorf("no store available")
|
|
}
|
|
|
|
bs, ok := storage.UnwrapStore(store).(storage.BackupStore)
|
|
if !ok {
|
|
return fmt.Errorf("storage backend does not support backup operations")
|
|
}
|
|
|
|
// First, commit any pending changes so they're included in the backup
|
|
if err := store.Commit(ctx, "bd: pre-backup commit"); err != nil && !isDoltNothingToCommit(err) {
|
|
fmt.Fprintf(os.Stderr, "Warning: failed to commit pending changes: %v\n", err)
|
|
}
|
|
commandDidExplicitDoltCommit = true
|
|
|
|
start := time.Now()
|
|
|
|
// Sync to the configured backup
|
|
if err := bs.BackupSync(ctx, defaultDoltBackupName); err != nil {
|
|
if strings.Contains(err.Error(), "no backup") ||
|
|
strings.Contains(err.Error(), "not found") {
|
|
return fmt.Errorf("no backup destination configured. Run 'bd backup init <path>' first")
|
|
}
|
|
return fmt.Errorf("backup sync failed: %w", err)
|
|
}
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
// Update backup state
|
|
if err := updateDoltBackupState(elapsed); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: backup synced but failed to update state: %v\n", err)
|
|
}
|
|
|
|
if jsonOutput {
|
|
return outputJSON(map[string]interface{}{
|
|
"synced": true,
|
|
"duration": elapsed.String(),
|
|
})
|
|
}
|
|
|
|
fmt.Printf("Backup synced in %s\n", elapsed.Round(time.Millisecond))
|
|
return nil
|
|
},
|
|
}
|
|
|
|
// resolveDoltBackupURL converts a user-provided path or URL into a Dolt backup URL.
|
|
// Filesystem paths get resolved to absolute and prefixed with file://
|
|
// URLs (https://, http://) are passed through as-is.
|
|
func resolveDoltBackupURL(raw string) string {
|
|
// DoltHub or other remote URLs — pass through
|
|
if strings.HasPrefix(raw, "https://") || strings.HasPrefix(raw, "http://") ||
|
|
strings.HasPrefix(raw, "file://") || strings.HasPrefix(raw, "aws://") ||
|
|
strings.HasPrefix(raw, "gs://") {
|
|
return raw
|
|
}
|
|
|
|
// Expand ~ to home directory
|
|
if strings.HasPrefix(raw, "~/") {
|
|
home, _ := os.UserHomeDir()
|
|
raw = filepath.Join(home, raw[2:])
|
|
}
|
|
|
|
// Resolve to absolute path
|
|
abs, err := filepath.Abs(raw)
|
|
if err != nil {
|
|
abs = raw
|
|
}
|
|
|
|
return "file://" + abs
|
|
}
|
|
|
|
// doltBackupConfig stores the backup destination info in .beads/dolt-backup.json
|
|
type doltBackupConfig struct {
|
|
BackupURL string `json:"backup_url"`
|
|
BackupName string `json:"backup_name"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
// doltBackupState tracks the last successful Dolt backup sync.
|
|
type doltBackupState struct {
|
|
LastSync time.Time `json:"last_sync"`
|
|
Duration string `json:"duration"`
|
|
}
|
|
|
|
func doltBackupConfigPath() (string, error) {
|
|
beadsDir := beads.FindBeadsDir()
|
|
if beadsDir == "" {
|
|
return "", fmt.Errorf("%s", activeWorkspaceNotFoundError())
|
|
}
|
|
return filepath.Join(beadsDir, "dolt-backup.json"), nil
|
|
}
|
|
|
|
func doltBackupStatePath() (string, error) {
|
|
beadsDir := beads.FindBeadsDir()
|
|
if beadsDir == "" {
|
|
return "", fmt.Errorf("%s", activeWorkspaceNotFoundError())
|
|
}
|
|
return filepath.Join(beadsDir, "dolt-backup-state.json"), nil
|
|
}
|
|
|
|
func saveDoltBackupConfig(backupURL string) error {
|
|
path, err := doltBackupConfigPath()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cfg := doltBackupConfig{
|
|
BackupURL: backupURL,
|
|
BackupName: defaultDoltBackupName,
|
|
CreatedAt: time.Now().UTC(),
|
|
}
|
|
data, err := json.MarshalIndent(cfg, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return atomicWriteFile(path, data)
|
|
}
|
|
|
|
func loadDoltBackupConfig() (*doltBackupConfig, error) {
|
|
path, err := doltBackupConfigPath()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
data, err := os.ReadFile(path) //nolint:gosec // path is constructed internally
|
|
if os.IsNotExist(err) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var cfg doltBackupConfig
|
|
if err := json.Unmarshal(data, &cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
return &cfg, nil
|
|
}
|
|
|
|
func updateDoltBackupState(duration time.Duration) error {
|
|
path, err := doltBackupStatePath()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
state := doltBackupState{
|
|
LastSync: time.Now().UTC(),
|
|
Duration: duration.String(),
|
|
}
|
|
data, err := json.MarshalIndent(state, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return atomicWriteFile(path, data)
|
|
}
|
|
|
|
func loadDoltBackupState() (*doltBackupState, error) {
|
|
path, err := doltBackupStatePath()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
data, err := os.ReadFile(path) //nolint:gosec // path is constructed internally
|
|
if os.IsNotExist(err) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var state doltBackupState
|
|
if err := json.Unmarshal(data, &state); err != nil {
|
|
return nil, err
|
|
}
|
|
return &state, nil
|
|
}
|
|
|
|
// showDoltBackupStatus prints Dolt backup info as part of bd backup status.
|
|
func showDoltBackupStatus() {
|
|
cfg, err := loadDoltBackupConfig()
|
|
if err != nil || cfg == nil {
|
|
return
|
|
}
|
|
|
|
fmt.Println("\nDolt Backup:")
|
|
fmt.Printf(" Destination: %s\n", cfg.BackupURL)
|
|
fmt.Printf(" Configured: %s\n", cfg.CreatedAt.Format(time.RFC3339))
|
|
|
|
state, err := loadDoltBackupState()
|
|
if err != nil || state == nil {
|
|
fmt.Println(" Last sync: never")
|
|
return
|
|
}
|
|
|
|
fmt.Printf(" Last sync: %s (%s ago, took %s)\n",
|
|
state.LastSync.Format(time.RFC3339),
|
|
time.Since(state.LastSync).Round(time.Second),
|
|
state.Duration)
|
|
}
|
|
|
|
// showDoltBackupStatusJSON returns Dolt backup info for JSON output.
|
|
func showDoltBackupStatusJSON() map[string]interface{} {
|
|
result := map[string]interface{}{
|
|
"configured": false,
|
|
}
|
|
|
|
cfg, err := loadDoltBackupConfig()
|
|
if err != nil || cfg == nil {
|
|
return result
|
|
}
|
|
|
|
result["configured"] = true
|
|
result["backup_url"] = cfg.BackupURL
|
|
result["backup_name"] = cfg.BackupName
|
|
result["created_at"] = cfg.CreatedAt.Format(time.RFC3339)
|
|
|
|
state, err := loadDoltBackupState()
|
|
if err == nil && state != nil {
|
|
result["last_sync"] = state.LastSync.Format(time.RFC3339)
|
|
result["sync_duration"] = state.Duration
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// doltBackupSize returns the active database size when the current store
|
|
// instance can measure its storage locally. Unsupported backends preserve the
|
|
// optional status-field contract instead of failing the command.
|
|
//
|
|
// Proxied mode has no store to ask — the provider owns the connection — so the
|
|
// fork lives here, where "which backend measures this database" is already the
|
|
// question being answered.
|
|
func doltBackupSize(ctx context.Context) (int64, bool, error) {
|
|
if usesProxiedServer() {
|
|
return doltBackupSizeProxied(ctx)
|
|
}
|
|
if store == nil {
|
|
return 0, false, fmt.Errorf("no storage backend is open")
|
|
}
|
|
return doltBackupSizeForStore(ctx, store)
|
|
}
|
|
|
|
func doltBackupSizeForStore(ctx context.Context, candidate storage.DoltStorage) (int64, bool, error) {
|
|
sizer, ok := storage.UnwrapStore(candidate).(storage.ActiveDatabaseSizer)
|
|
if !ok {
|
|
return 0, false, nil
|
|
}
|
|
return doltBackupSizeFromSizer(ctx, sizer)
|
|
}
|
|
|
|
func doltBackupSizeFromSizer(ctx context.Context, sizer storage.ActiveDatabaseSizer) (int64, bool, error) {
|
|
size, err := sizer.ActiveDatabaseSize(ctx)
|
|
if err != nil {
|
|
var unsupported *storage.ErrUnsupported
|
|
if errors.As(err, &unsupported) {
|
|
return 0, false, nil
|
|
}
|
|
return 0, false, err
|
|
}
|
|
return size, true, nil
|
|
}
|
|
|
|
// showDBSize prints the database size as part of status.
|
|
func showDBSize(size int64) {
|
|
fmt.Printf(" Database size: %s\n", formatBytes(size))
|
|
}
|
|
|
|
// showDBSizeJSON returns database size for JSON output.
|
|
func showDBSizeJSON(size int64) map[string]interface{} {
|
|
return map[string]interface{}{
|
|
"bytes": size,
|
|
"human": formatBytes(size),
|
|
}
|
|
}
|
|
|
|
var backupRemoveCmd = &cobra.Command{
|
|
Use: "remove",
|
|
Short: "Remove the configured backup destination",
|
|
Long: `Remove the configured backup destination.
|
|
|
|
This unregisters the backup remote from Dolt and removes the local
|
|
backup configuration. The backup data at the destination is not deleted.`,
|
|
Aliases: []string{"rm"},
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
CheckReadonly("backup remove")
|
|
|
|
evt := metrics.NewCommandEvent("backup-remove")
|
|
defer func() {
|
|
if c := metrics.Global(); c != nil {
|
|
c.CloseEventAndAdd(evt)
|
|
}
|
|
}()
|
|
|
|
ctx := rootCtx
|
|
if usesProxiedServer() {
|
|
return runBackupRemoveProxied(ctx)
|
|
}
|
|
if store == nil {
|
|
return fmt.Errorf("no store available")
|
|
}
|
|
|
|
bs, ok := storage.UnwrapStore(store).(storage.BackupStore)
|
|
if !ok {
|
|
return fmt.Errorf("storage backend does not support backup operations")
|
|
}
|
|
|
|
if err := bs.BackupRemove(ctx, defaultDoltBackupName); err != nil {
|
|
if strings.Contains(err.Error(), "not found") || strings.Contains(err.Error(), "no backup") {
|
|
return fmt.Errorf("no backup destination configured")
|
|
}
|
|
return fmt.Errorf("failed to remove backup: %w", err)
|
|
}
|
|
|
|
// Also remove backup_export if it exists (auto-export may have created it at same URL)
|
|
_ = bs.BackupRemove(ctx, versioncontrolops.ExportBackupName)
|
|
|
|
// Remove local config
|
|
if path, err := doltBackupConfigPath(); err == nil {
|
|
_ = os.Remove(path)
|
|
}
|
|
if path, err := doltBackupStatePath(); err == nil {
|
|
_ = os.Remove(path)
|
|
}
|
|
|
|
if jsonOutput {
|
|
return outputJSON(map[string]interface{}{"removed": true})
|
|
}
|
|
|
|
fmt.Println("Backup destination removed.")
|
|
return nil
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
backupCmd.AddCommand(backupInitCmd)
|
|
backupCmd.AddCommand(backupSyncCmd)
|
|
backupCmd.AddCommand(backupRemoveCmd)
|
|
}
|