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
+103
-13
@@ -71,7 +71,10 @@ type Scanner struct {
|
||||
// indexAndLockFiles holds the paths of vaultik's own working files,
|
||||
// which the scan treats as excluded (ScannerConfig.IndexAndLockFiles).
|
||||
indexAndLockFiles map[string]struct{}
|
||||
progress *ProgressReporter
|
||||
// listExcluded prints each excluded entry and why it is excluded
|
||||
// (DryRun).
|
||||
listExcluded bool
|
||||
progress *ProgressReporter
|
||||
// skipErrors skips files that cannot be opened or read (logged loudly);
|
||||
// packer, database, encryption, and upload errors still abort the run.
|
||||
skipErrors bool
|
||||
@@ -190,6 +193,11 @@ type ScanResult struct {
|
||||
// itself when it is a directory. It is zero for an empty directory,
|
||||
// such as the mount point of a volume that is not mounted.
|
||||
EntriesFound int
|
||||
|
||||
// EntriesExcluded counts the entries the walk found that an exclude
|
||||
// pattern matches or that are vaultik's own files. An excluded
|
||||
// directory counts once: the walk does not enter it.
|
||||
EntriesExcluded int
|
||||
}
|
||||
|
||||
// NewScanner creates a new scanner instance
|
||||
@@ -360,6 +368,42 @@ func (s *Scanner) Scan(
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// DryRun walks path as Scan does and counts what a backup would store,
|
||||
// without storing it. It reads the local index to tell unchanged files
|
||||
// from new and changed ones, and writes nothing to the local index or
|
||||
// the backup destination store. With listExcluded it prints each
|
||||
// excluded entry and the exclude pattern that matched it.
|
||||
func (s *Scanner) DryRun(
|
||||
ctx context.Context, path string, listExcluded bool,
|
||||
) (*ScanResult, error) {
|
||||
s.currentSourcePath = path
|
||||
s.listExcluded = listExcluded
|
||||
result := &ScanResult{
|
||||
StartTime: time.Now().UTC(),
|
||||
}
|
||||
|
||||
knownFiles, err := s.loadDatabaseState(ctx, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
existingFiles := make(map[string]struct{})
|
||||
|
||||
_, err = s.scanPhase(ctx, path, result, existingFiles, knownFiles)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scan phase failed: %w", err)
|
||||
}
|
||||
|
||||
err = s.detectDeletedFiles(ctx, path, existingFiles, result)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("detecting deleted files: %w", err)
|
||||
}
|
||||
|
||||
result.EndTime = time.Now().UTC()
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetProgress returns the progress reporter for this scanner, or nil when
|
||||
// progress is off. Scan neither starts nor stops it: the caller does,
|
||||
// once for all the paths it scans, because a second Stop panics.
|
||||
@@ -944,12 +988,8 @@ func (s *Scanner) scanPhase(
|
||||
// or cannot be looked up is neither counted nor reported. For
|
||||
// directories, skip the entire subtree. info is nil when the
|
||||
// walk could not look the entry up.
|
||||
if s.shouldExclude(filePath, path) {
|
||||
if info != nil && info.IsDir() {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
|
||||
return nil
|
||||
if excluded, walkErr := s.skipExcluded(filePath, path, info, result); excluded {
|
||||
return walkErr
|
||||
}
|
||||
|
||||
// The source path is in no directory listing, so its absence is
|
||||
@@ -1016,6 +1056,32 @@ func (s *Scanner) scanPhase(
|
||||
}, nil
|
||||
}
|
||||
|
||||
// skipExcluded reports whether the walk skips filePath, under rootPath,
|
||||
// because it is excluded, and counts it if so. The error is what the walk
|
||||
// function returns for it: filepath.SkipDir for a directory, so the walk
|
||||
// does not enter it, and nil otherwise. info is nil when the walk could
|
||||
// not look the entry up.
|
||||
func (s *Scanner) skipExcluded(
|
||||
filePath, rootPath string, info os.FileInfo, result *ScanResult,
|
||||
) (bool, error) {
|
||||
pattern, excluded := s.excludingPattern(filePath, rootPath)
|
||||
if !excluded {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
result.EntriesExcluded++
|
||||
|
||||
if s.listExcluded {
|
||||
s.printExcluded(filePath, pattern)
|
||||
}
|
||||
|
||||
if info != nil && info.IsDir() {
|
||||
return true, filepath.SkipDir
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// countNonDirectories returns how many of files are not directories.
|
||||
func countNonDirectories(files map[string]*database.File) int64 {
|
||||
var count int64
|
||||
@@ -2351,23 +2417,32 @@ func compileExcludePatterns(patterns []string) ([]compiledPattern, error) {
|
||||
// filePath is the full path to the file
|
||||
// rootPath is the root of the backup source directory
|
||||
func (s *Scanner) shouldExclude(filePath, rootPath string) bool {
|
||||
_, excluded := s.excludingPattern(filePath, rootPath)
|
||||
|
||||
return excluded
|
||||
}
|
||||
|
||||
// excludingPattern reports whether shouldExclude excludes filePath and
|
||||
// returns the exclude pattern, as configured, that matches it. The
|
||||
// pattern is "" for one of vaultik's own working files.
|
||||
func (s *Scanner) excludingPattern(filePath, rootPath string) (string, bool) {
|
||||
if _, ok := s.indexAndLockFiles[filePath]; ok {
|
||||
return true
|
||||
return "", true
|
||||
}
|
||||
|
||||
if len(s.compiledExclude) == 0 {
|
||||
return false
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Get the relative path from root
|
||||
relPath, err := filepath.Rel(rootPath, filePath)
|
||||
if err != nil {
|
||||
return false
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Never exclude the root directory itself
|
||||
if relPath == "." {
|
||||
return false
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Normalize path separators
|
||||
@@ -2376,11 +2451,26 @@ func (s *Scanner) shouldExclude(filePath, rootPath string) bool {
|
||||
// Check each pattern
|
||||
for _, cp := range s.compiledExclude {
|
||||
if cp.matches(relPath) {
|
||||
return true
|
||||
return cp.original, true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
return "", false
|
||||
}
|
||||
|
||||
// printExcluded prints that the walk excluded filePath and why: pattern
|
||||
// is the exclude pattern that matched it, "" for one of vaultik's own
|
||||
// working files.
|
||||
func (s *Scanner) printExcluded(filePath, pattern string) {
|
||||
if pattern == "" {
|
||||
s.ui.Infof("Excluded %s: vaultik's own index or lock file.",
|
||||
s.ui.Path(filePath))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
s.ui.Infof("Excluded %s: matches exclude pattern %q.",
|
||||
s.ui.Path(filePath), pattern)
|
||||
}
|
||||
|
||||
// matches reports whether the compiled pattern matches the given
|
||||
|
||||
Reference in new issue
Block a user