2152baeb4f
- nib export: dump all entities to JSON (stdout or --output file) - nib backup: atomic SQLite backup via VACUUM INTO (WAL-safe) - Store.Backup() method on db layer - Tests for both commands
47 lines
896 B
Go
47 lines
896 B
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/lerko/nib/internal/db"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var backupCmd = &cobra.Command{
|
|
Use: "backup [path]",
|
|
Short: "create a safe backup of the database",
|
|
Long: "Creates an atomic backup using VACUUM INTO. Safe with WAL mode — no need to stop the server.",
|
|
Args: cobra.MaximumNArgs(1),
|
|
RunE: runBackup,
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(backupCmd)
|
|
}
|
|
|
|
func runBackup(cmd *cobra.Command, args []string) error {
|
|
srcPath, err := db.DefaultPath()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
dst := fmt.Sprintf("%s.backup-%s", srcPath, time.Now().Format("20060102-150405"))
|
|
if len(args) > 0 {
|
|
dst = args[0]
|
|
}
|
|
|
|
store, err := db.Open(srcPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer store.Close()
|
|
|
|
if err := store.Backup(dst); err != nil {
|
|
return fmt.Errorf("backup failed: %w", err)
|
|
}
|
|
|
|
fmt.Printf("backed up to %s\n", dst)
|
|
return nil
|
|
}
|