From f6bd1c7a0256a755d986d86b1eee0c9f3430d220 Mon Sep 17 00:00:00 2001 From: Nick Wright Date: Fri, 9 Aug 2024 19:23:15 +0000 Subject: [PATCH] Add kopia snapshot verify command --- pkg/kopia/command/const.go | 8 +++++ pkg/kopia/command/snapshot.go | 50 ++++++++++++++++++++++++++++++ pkg/kopia/command/snapshot_test.go | 29 +++++++++++++++++ 3 files changed, 87 insertions(+) diff --git a/pkg/kopia/command/const.go b/pkg/kopia/command/const.go index d7c0d963a3..b3532bdc82 100644 --- a/pkg/kopia/command/const.go +++ b/pkg/kopia/command/const.go @@ -31,11 +31,16 @@ const ( showSubCommand = "show" snapshotSubCommand = "snapshot" statsSubCommand = "stats" + verifySubCommand = "verify" allFlag = "--all" configFileFlag = "--config-file" deleteFlag = "--delete" deltaFlag = "--delta" + directoryIDFlag = "--directory-id" + fileIDFlag = "--file-id" + fileParallelismFlag = "--file-parallelism" + fileQueueLengthFlag = "--file-queue-length" filterFlag = "--filter" globalFlag = "--global" jsonFlag = "--json" @@ -44,15 +49,18 @@ const ( fileLogLevelFlag = "--file-log-level" LogLevelError = "error" LogLevelInfo = "info" + maxErrorsFlag = "--max-errors" parallelFlag = "--parallel" passwordFlag = "--password" progressUpdateIntervalFlag = "--progress-update-interval" rawFlag = "--raw" showIdenticalFlag = "--show-identical" + sourcesFlag = "--sources" tagsFlag = "--tags" unsafeIgnoreSourceFlag = "--unsafe-ignore-source" ownerFlag = "--owner" sparseFlag = "--write-sparse-files" + verifyFilesPercentFlag = "--verify-files-percent" ignorePermissionsError = "--ignore-permission-errors" noIgnorePermissionsError = "--no-ignore-permission-errors" diff --git a/pkg/kopia/command/snapshot.go b/pkg/kopia/command/snapshot.go index 67fb9a83d0..2362b9d0b2 100644 --- a/pkg/kopia/command/snapshot.go +++ b/pkg/kopia/command/snapshot.go @@ -15,6 +15,7 @@ package command import ( + "fmt" "strconv" "time" @@ -169,3 +170,52 @@ func SnapListByTags(cmdArgs SnapListByTagsCommandArgs) []string { args = addTags(cmdArgs.Tags, args) return stringSliceCommand(args) } + +type SnapshotVerifyCommandArgs struct { + *CommandArgs + DirectoryID []string + FileID []string + Sources []string + VerifyFilesPercent *float64 + Parallelism *int + FileQueueLength *int + FileParallelism *int + MaxErrors *int +} + +// SnapshotVerify returns kopia command verifying snapshots with given snapshot IDs. +func SnapshotVerify(cmdArgs SnapshotVerifyCommandArgs) []string { + args := commonArgs(cmdArgs.CommandArgs) + args = args.AppendLoggable(snapshotSubCommand, verifySubCommand) + + if cmdArgs.VerifyFilesPercent != nil { + args = args.AppendLoggableKV(verifyFilesPercentFlag, fmt.Sprintf("%v", *cmdArgs.VerifyFilesPercent)) + } + + if cmdArgs.Parallelism != nil { + parallelismStr := strconv.Itoa(*cmdArgs.Parallelism) + args = args.AppendLoggableKV(parallelFlag, parallelismStr) + } + + if cmdArgs.FileQueueLength != nil { + args = args.AppendLoggableKV(fileQueueLengthFlag, strconv.Itoa(*cmdArgs.FileQueueLength)) + } + + if cmdArgs.FileParallelism != nil { + args = args.AppendLoggableKV(fileParallelismFlag, strconv.Itoa(*cmdArgs.FileParallelism)) + } + + for _, dirID := range cmdArgs.DirectoryID { + args = args.AppendLoggableKV(directoryIDFlag, dirID) + } + + for _, fileID := range cmdArgs.FileID { + args = args.AppendLoggableKV(fileIDFlag, fileID) + } + + for _, source := range cmdArgs.Sources { + args = args.AppendLoggableKV(sourcesFlag, source) + } + + return stringSliceCommand(args) +} diff --git a/pkg/kopia/command/snapshot_test.go b/pkg/kopia/command/snapshot_test.go index bc62e4e0ed..f32385c656 100644 --- a/pkg/kopia/command/snapshot_test.go +++ b/pkg/kopia/command/snapshot_test.go @@ -147,6 +147,35 @@ func (kSnapshot *KopiaSnapshotTestSuite) TestSnapshotCommands(c *C) { }, expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key snapshot list --all --delta --show-identical --json --tags tag1:val1 --tags tag2:val2", }, + { + f: func() []string { + args := SnapshotVerifyCommandArgs{ + CommandArgs: commandArgs, + } + return SnapshotVerify(args) + }, + expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key snapshot verify", + }, + { + f: func() []string { + vfp := 12.345 + p := 123 + fql := 456 + fp := 890 + args := SnapshotVerifyCommandArgs{ + CommandArgs: commandArgs, + VerifyFilesPercent: &vfp, + Parallelism: &p, + FileQueueLength: &fql, + FileParallelism: &fp, + DirectoryID: []string{"d1", "d2"}, + FileID: []string{"f1", "f2"}, + Sources: []string{"s1", "s2"}, + } + return SnapshotVerify(args) + }, + expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key snapshot verify --verify-files-percent=12.345 --parallel=123 --file-queue-length=456 --file-parallelism=890 --directory-id=d1 --directory-id=d2 --file-id=f1 --file-id=f2 --sources=s1 --sources=s2", + }, } { cmd := strings.Join(tc.f(), " ") c.Check(cmd, Equals, tc.expectedLog)