Skip to content

Commit

Permalink
Do not ignore permission errors in kopia restore command (#1835)
Browse files Browse the repository at this point in the history
* Do not ignore permission errors

* Fix snapshot command. Tests added

* Add optional ignore permission arg

* Fix order of args in UT

* Additional UT cases added
  • Loading branch information
TimonOmsk committed Jan 5, 2023
1 parent e9fe7ec commit 4c3858a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 16 deletions.
1 change: 1 addition & 0 deletions pkg/kopia/command/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
unsafeIgnoreSourceFlag = "--unsafe-ignore-source"
ownerFlag = "--owner"
sparseFlag = "--write-sparse-files"
ignorePermissionsError = "--ignore-permission-errors"

// Server specific
addSubCommand = "add"
Expand Down
8 changes: 6 additions & 2 deletions pkg/kopia/command/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@

package command

import "strconv"

type RestoreCommandArgs struct {
*CommandArgs
RootID string
TargetPath string
RootID string
TargetPath string
IgnorePermissionErrors bool
}

// Restore returns the kopia command for restoring root of a snapshot with given root ID
func Restore(cmdArgs RestoreCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(restoreSubCommand, cmdArgs.RootID, cmdArgs.TargetPath)
args = args.AppendLoggableKV(ignorePermissionsError, strconv.FormatBool(cmdArgs.IgnorePermissionErrors))

return stringSliceCommand(args)
}
18 changes: 17 additions & 1 deletion pkg/kopia/command/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,23 @@ func (kRestore *KopiaRestoreTestSuite) TestRestoreCommands(c *C) {
}
return Restore(args)
},
expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key restore snapshot-id target/path",
expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key restore snapshot-id target/path --ignore-permission-errors=false",
},
{
f: func() []string {
args := RestoreCommandArgs{
CommandArgs: &CommandArgs{
RepoPassword: "encr-key",
ConfigFilePath: "path/kopia.config",
LogDirectory: "cache/log",
},
RootID: "snapshot-id",
TargetPath: "target/path",
IgnorePermissionErrors: true,
}
return Restore(args)
},
expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key restore snapshot-id target/path --ignore-permission-errors=true",
},
} {
cmd := strings.Join(tc.f(), " ")
Expand Down
8 changes: 5 additions & 3 deletions pkg/kopia/command/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ func SnapshotCreate(cmdArgs SnapshotCreateCommandArgs) []string {

type SnapshotRestoreCommandArgs struct {
*CommandArgs
SnapID string
TargetPath string
SparseRestore bool
SnapID string
TargetPath string
SparseRestore bool
IgnorePermissionErrors bool
}

// SnapshotRestore returns kopia command restoring snapshots with given snap ID
func SnapshotRestore(cmdArgs SnapshotRestoreCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args = args.AppendLoggable(snapshotSubCommand, restoreSubCommand, cmdArgs.SnapID, cmdArgs.TargetPath)
args = args.AppendLoggableKV(ignorePermissionsError, strconv.FormatBool(cmdArgs.IgnorePermissionErrors))
if cmdArgs.SparseRestore {
args = args.AppendLoggable(sparseFlag)
}
Expand Down
33 changes: 23 additions & 10 deletions pkg/kopia/command/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,39 @@ func (kSnapshot *KopiaSnapshotTestSuite) TestSnapshotCommands(c *C) {
{
f: func() []string {
args := SnapshotRestoreCommandArgs{
CommandArgs: commandArgs,
SnapID: "snapshot-id",
TargetPath: "target/path",
SparseRestore: false,
CommandArgs: commandArgs,
SnapID: "snapshot-id",
TargetPath: "target/path",
SparseRestore: false,
IgnorePermissionErrors: false,
}
return SnapshotRestore(args)
},
expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key snapshot restore snapshot-id target/path --ignore-permission-errors=false",
},
{
f: func() []string {
args := SnapshotRestoreCommandArgs{
CommandArgs: commandArgs,
SnapID: "snapshot-id",
TargetPath: "target/path",
}
return SnapshotRestore(args)
},
expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key snapshot restore snapshot-id target/path",
expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key snapshot restore snapshot-id target/path --ignore-permission-errors=false",
},
{
f: func() []string {
args := SnapshotRestoreCommandArgs{
CommandArgs: commandArgs,
SnapID: "snapshot-id",
TargetPath: "target/path",
SparseRestore: true,
CommandArgs: commandArgs,
SnapID: "snapshot-id",
TargetPath: "target/path",
SparseRestore: true,
IgnorePermissionErrors: true,
}
return SnapshotRestore(args)
},
expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key snapshot restore snapshot-id target/path --write-sparse-files",
expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key snapshot restore snapshot-id target/path --ignore-permission-errors=true --write-sparse-files",
},
{
f: func() []string {
Expand Down

0 comments on commit 4c3858a

Please sign in to comment.