Add snapshot create --dry-run to show what excludes keep and drop (closes #436)
check / check (push) Waiting to run
check / check (push) Waiting to run
The dry run scans the source paths, reads the local index to tell unchanged files from new and changed ones, and prints the files examined, to back up and unchanged, their sizes, and the count of excluded entries. With --verbose it prints each excluded entry and the pattern that matched it. It writes nothing to the local index or the destination, takes no lock, and refuses --prune. Model: opus-5-5
This commit is contained in:
1 parent
d747ddef3a
commit
95add7fc38
8 files changed
+492
-58
No files matched your search
+4
-4
@@ -39,10 +39,10 @@ const (
|
||||
// hold the PID lock so that at most one runs at a time.
|
||||
mutating lockMode = iota
|
||||
// readOnly commands (info, snapshot list, snapshot files, snapshot
|
||||
// verify, remote info, snapshot restore) do not write the local index
|
||||
// or the remote store, so they run without the lock and are never
|
||||
// blocked by a running mutator. restore writes only to the target
|
||||
// directory it is given.
|
||||
// verify, remote info, snapshot restore, snapshot create --dry-run)
|
||||
// do not write the local index or the remote store, so they run
|
||||
// without the lock and are never blocked by a running mutator.
|
||||
// restore writes only to the target directory it is given.
|
||||
readOnly
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package cli //nolint:testpackage // shares runEntry and the hermetic config helpers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"sneak.berlin/go/vaultik/internal/database"
|
||||
"sneak.berlin/go/vaultik/internal/ui"
|
||||
)
|
||||
|
||||
// TestEntrySnapshotCreateDryRun runs `snapshot create --dry-run
|
||||
// --verbose` on a source directory that holds one kept file, one file an
|
||||
// unanchored pattern excludes and one directory an anchored pattern
|
||||
// excludes. The summary counts the kept file and two excluded entries,
|
||||
// the excluded ones are listed with their patterns, and nothing is
|
||||
// written to the destination or recorded in the local index. See
|
||||
// https://git.eeqj.de/sneak/vaultik/issues/436.
|
||||
//
|
||||
//nolint:paralleltest // replaces os.Args, os.Stdout, os.Stderr and the xdg globals
|
||||
func TestEntrySnapshotCreateDryRun(t *testing.T) {
|
||||
configPath, storeDir := writeBackupConfig(t)
|
||||
sourceDir := filepath.Join(filepath.Dir(configPath), "source")
|
||||
cacheDir := filepath.Join(sourceDir, "cache")
|
||||
|
||||
require.NoError(t, os.WriteFile(
|
||||
filepath.Join(sourceDir, "debug.log"), []byte("log"), 0o600))
|
||||
require.NoError(t, os.Mkdir(cacheDir, 0o750))
|
||||
|
||||
for _, name := range []string{"a", "b"} {
|
||||
require.NoError(t, os.WriteFile(
|
||||
filepath.Join(cacheDir, name), []byte(name), 0o600))
|
||||
}
|
||||
|
||||
out := ui.NewWithColor(io.Discard, false)
|
||||
require.NoError(t, writeConfigSet(out, configPath, "exclude.0", "*.log"))
|
||||
require.NoError(t, writeConfigSet(out, configPath, "exclude.1", "/cache"))
|
||||
|
||||
code, stdout, stderr := runEntry(t, flagConfig, configPath, "--verbose",
|
||||
cmdSnapshot, cmdCreate, "--dry-run")
|
||||
|
||||
require.Equal(t, 0, code, stderr)
|
||||
assert.Contains(t, stdout, "Excluded "+filepath.Join(sourceDir, "debug.log")+
|
||||
`: matches exclude pattern "*.log".`)
|
||||
assert.Contains(t, stdout, "Excluded "+cacheDir+
|
||||
`: matches exclude pattern "/cache".`)
|
||||
assert.NotContains(t, stdout, filepath.Join(cacheDir, "a"))
|
||||
assert.Contains(t, stdout, "Files: 1 examined, 1 to back up, 0 unchanged.")
|
||||
assert.Contains(t, stdout, "Excluded: 2 entries.")
|
||||
|
||||
entries, err := os.ReadDir(storeDir)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, entries, "a dry run writes nothing to the destination")
|
||||
|
||||
ctx := context.Background()
|
||||
db, err := database.New(ctx,
|
||||
filepath.Join(filepath.Dir(configPath), "index.sqlite"))
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() { require.NoError(t, db.Close()) }()
|
||||
|
||||
repos := database.NewRepositories(db)
|
||||
|
||||
snapshots, err := repos.Snapshots.ListRecent(ctx, 1)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, snapshots, "a dry run records no snapshot")
|
||||
|
||||
binding, err := repos.LocalMeta.Get(ctx, database.LocalMetaKeyStorageURL)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, binding, "a dry run does not bind the local index")
|
||||
}
|
||||
|
||||
// TestEntrySnapshotCreateDryRunRefusesPrune checks that --dry-run with
|
||||
// --prune fails before anything runs.
|
||||
//
|
||||
//nolint:paralleltest // replaces os.Args, os.Stdout, os.Stderr and the xdg globals
|
||||
func TestEntrySnapshotCreateDryRunRefusesPrune(t *testing.T) {
|
||||
configPath, _ := writeBackupConfig(t)
|
||||
|
||||
code, _, stderr := runEntry(t, flagConfig, configPath,
|
||||
cmdSnapshot, cmdCreate, "--dry-run", "--prune")
|
||||
|
||||
assert.NotEqual(t, 0, code)
|
||||
assert.Contains(t, stderr, "--dry-run cannot be used with --prune")
|
||||
}
|
||||
+35
-19
@@ -99,6 +99,13 @@ on Linux), then /etc/vaultik/config.yml.`,
|
||||
|
||||
// Use the backup functionality from cli package
|
||||
rootFlags := GetRootFlags()
|
||||
opts.ListExcluded = rootFlags.Verbose
|
||||
|
||||
// A dry run writes nothing, so it does not take the lock.
|
||||
mode := mutating
|
||||
if opts.DryRun {
|
||||
mode = readOnly
|
||||
}
|
||||
|
||||
// --cron suppression is wired through v.UI by setupGlobals.
|
||||
return RunOperation(cmd.Context(), AppOptions{
|
||||
@@ -109,28 +116,11 @@ on Linux), then /etc/vaultik/config.yml.`,
|
||||
Cron: opts.Cron,
|
||||
Quiet: rootFlags.Quiet,
|
||||
},
|
||||
Mode: mutating,
|
||||
Mode: mode,
|
||||
}, func(v *vaultik.Vaultik) error {
|
||||
return v.CreateSnapshot(opts)
|
||||
}, func(err error) {
|
||||
if errors.Is(err, vaultik.ErrPostBackupPruneFailed) {
|
||||
log.Info("Post-backup prune failed", "error", err)
|
||||
ReportErrorf("Backup completed, but %v", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if errors.Is(err, vaultik.ErrFilesUnreadable) ||
|
||||
errors.Is(err, vaultik.ErrSourcePathEmpty) ||
|
||||
errors.Is(err, vaultik.ErrVolumeNotMounted) {
|
||||
log.Info("Backup completed with an error", "error", err)
|
||||
ReportErrorf("Backup completed, but %v", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
log.Info("Snapshot creation failed", "error", err)
|
||||
ReportErrorf("Snapshot creation failed: %v", err)
|
||||
reportSnapshotCreateFailure(opts.DryRun, err)
|
||||
})
|
||||
},
|
||||
}
|
||||
@@ -149,10 +139,36 @@ on Linux), then /etc/vaultik/config.yml.`,
|
||||
"With --prune: keep snapshots newer than this duration "+
|
||||
"(e.g. 30d, 4w, 6mo, 1y; m is minutes, mo is months) "+
|
||||
"instead of only the latest")
|
||||
cmd.Flags().BoolVar(&opts.DryRun, "dry-run", false,
|
||||
"Scan the snapshot source paths and print what a backup would store, "+
|
||||
"writing nothing to the local index or the backup destination; "+
|
||||
"with --verbose, also print each excluded entry and the exclude "+
|
||||
"pattern that matched it")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
// reportSnapshotCreateFailure shows the user the error `snapshot create`
|
||||
// failed with. dryRun says whether it was given --dry-run.
|
||||
func reportSnapshotCreateFailure(dryRun bool, err error) {
|
||||
switch {
|
||||
case dryRun:
|
||||
log.Info("Dry run failed", "error", err)
|
||||
ReportErrorf("Dry run failed: %v", err)
|
||||
case errors.Is(err, vaultik.ErrPostBackupPruneFailed):
|
||||
log.Info("Post-backup prune failed", "error", err)
|
||||
ReportErrorf("Backup completed, but %v", err)
|
||||
case errors.Is(err, vaultik.ErrFilesUnreadable) ||
|
||||
errors.Is(err, vaultik.ErrSourcePathEmpty) ||
|
||||
errors.Is(err, vaultik.ErrVolumeNotMounted):
|
||||
log.Info("Backup completed with an error", "error", err)
|
||||
ReportErrorf("Backup completed, but %v", err)
|
||||
default:
|
||||
log.Info("Snapshot creation failed", "error", err)
|
||||
ReportErrorf("Snapshot creation failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// newSnapshotListCommand creates the 'snapshot list' subcommand
|
||||
func newSnapshotListCommand() *cobra.Command {
|
||||
var jsonOutput bool
|
||||
|
||||
Reference in new issue
Block a user