Files
mcp/cmd/mcp-agent/snapshot.go
Kyle Isom 08b3e2a472 Migrate module path from kyle/ to mc/ org
All import paths updated to git.wntrmute.dev/mc/. Bumps mcdsl to v1.2.0,
mc-proxy to v1.1.0.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 02:07:42 -07:00

49 lines
1.2 KiB
Go

package main
import (
"database/sql"
"fmt"
"os"
"path/filepath"
"time"
"git.wntrmute.dev/mc/mcp/internal/config"
"github.com/spf13/cobra"
_ "modernc.org/sqlite"
)
func snapshotCmd() *cobra.Command {
return &cobra.Command{
Use: "snapshot",
Short: "Create a database backup",
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.LoadAgentConfig(cfgPath)
if err != nil {
return fmt.Errorf("load config: %w", err)
}
backupDir := filepath.Join(filepath.Dir(cfg.Database.Path), "backups")
if err := os.MkdirAll(backupDir, 0750); err != nil {
return fmt.Errorf("create backup dir: %w", err)
}
ts := time.Now().Format("20060102-150405")
backupPath := filepath.Join(backupDir, fmt.Sprintf("mcp-%s.db", ts))
db, err := sql.Open("sqlite", cfg.Database.Path)
if err != nil {
return fmt.Errorf("open database: %w", err)
}
defer func() { _ = db.Close() }()
//nolint:gosec // backupPath is derived from config + timestamp, not user input; VACUUM INTO does not support placeholders
if _, err := db.Exec("VACUUM INTO '" + backupPath + "'"); err != nil {
return fmt.Errorf("vacuum into: %w", err)
}
fmt.Printf("snapshot: %s\n", backupPath)
return nil
},
}
}