From c9863c48f8477e38f685352108aa44c45ddb652e Mon Sep 17 00:00:00 2001 From: Johnathan Falk Date: Sun, 13 Nov 2022 11:58:08 -0500 Subject: [PATCH] fix(https://github.com/someone1/zfsbackup-go/pull/465): ALlow for a local zfs volume that differs from the desired. Signed-off-by: Johnathan Falk --- backup/backup.go | 6 +++--- cmd/send.go | 33 +++++++++++++++++++++++++++++---- zfs/zfs.go | 14 +++++++++++--- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/backup/backup.go b/backup/backup.go index d588c9d..2abe8d1 100644 --- a/backup/backup.go +++ b/backup/backup.go @@ -57,7 +57,7 @@ var ( // ProcessSmartOptions will compute the snapshots to use // nolint:funlen,gocyclo // Difficult to break this up func ProcessSmartOptions(ctx context.Context, jobInfo *files.JobInfo) error { - snapshots, err := zfs.GetSnapshotsAndBookmarks(context.Background(), jobInfo.VolumeName) + snapshots, err := zfs.GetSnapshotsAndBookmarks(context.Background(), zfs.GetLocalVolumeName(jobInfo)) if err != nil { return err } @@ -246,7 +246,7 @@ func Backup(pctx context.Context, jobInfo *files.JobInfo) error { } // Validate the snapshots we want to use exist - if ok, verr := validateSnapShotExists(ctx, &jobInfo.BaseSnapshot, jobInfo.VolumeName, false); verr != nil { + if ok, verr := validateSnapShotExists(ctx, &jobInfo.BaseSnapshot, zfs.GetLocalVolumeName(jobInfo), false); verr != nil { log.AppLogger.Errorf("Cannot validate if selected base snapshot exists due to error - %v", verr) return verr } else if !ok { @@ -255,7 +255,7 @@ func Backup(pctx context.Context, jobInfo *files.JobInfo) error { } if jobInfo.IncrementalSnapshot.Name != "" { - if ok, verr := validateSnapShotExists(ctx, &jobInfo.IncrementalSnapshot, jobInfo.VolumeName, true); verr != nil { + if ok, verr := validateSnapShotExists(ctx, &jobInfo.IncrementalSnapshot, zfs.GetLocalVolumeName(jobInfo), true); verr != nil { log.AppLogger.Errorf("Cannot validate if selected incremental snapshot exists due to error - %v", verr) return verr } else if !ok { diff --git a/cmd/send.go b/cmd/send.go index 86893e6..34bfbb0 100644 --- a/cmd/send.go +++ b/cmd/send.go @@ -179,6 +179,12 @@ func init() { 10, "the chunk size, in MiB, to use when uploading. A minimum of 5MiB and maximum of 100MiB is enforced.", ) + sendCmd.Flags().StringVar( + &jobInfo.LocalVolume, + "localVolume", + "", + "the local volume name if different from the S3 volume", + ) } // ResetSendJobInfo exists solely for integration testing @@ -248,7 +254,8 @@ func updateJobInfo(args []string) error { return errInvalidInput } jobInfo.BaseSnapshot = files.SnapshotInfo{Name: parts[1]} - creationTime, err := zfs.GetCreationDate(context.TODO(), args[0]) + localBaseSnapVolumeName := getLocalBaseSnapshotName(args[0]) + creationTime, err := zfs.GetCreationDate(context.TODO(), localBaseSnapVolumeName) if err != nil { log.AppLogger.Errorf("Error trying to get creation date of specified base snapshot - %v", err) return err @@ -257,14 +264,15 @@ func updateJobInfo(args []string) error { if jobInfo.IncrementalSnapshot.Name != "" { var targetName string - jobInfo.IncrementalSnapshot.Name = strings.TrimPrefix(jobInfo.IncrementalSnapshot.Name, jobInfo.VolumeName) + localVolumeName := zfs.GetLocalVolumeName(&jobInfo) + jobInfo.IncrementalSnapshot.Name = strings.TrimPrefix(jobInfo.IncrementalSnapshot.Name, localVolumeName) if strings.HasPrefix(jobInfo.IncrementalSnapshot.Name, "#") { jobInfo.IncrementalSnapshot.Name = strings.TrimPrefix(jobInfo.IncrementalSnapshot.Name, "#") - targetName = fmt.Sprintf("%s#%s", jobInfo.VolumeName, jobInfo.IncrementalSnapshot.Name) + targetName = fmt.Sprintf("%s#%s", localVolumeName, jobInfo.IncrementalSnapshot.Name) jobInfo.IncrementalSnapshot.Bookmark = true } else { jobInfo.IncrementalSnapshot.Name = strings.TrimPrefix(jobInfo.IncrementalSnapshot.Name, "@") - targetName = fmt.Sprintf("%s@%s", jobInfo.VolumeName, jobInfo.IncrementalSnapshot.Name) + targetName = fmt.Sprintf("%s@%s", localVolumeName, jobInfo.IncrementalSnapshot.Name) } creationTime, err = zfs.GetCreationDate(context.TODO(), targetName) @@ -304,6 +312,23 @@ func updateJobInfo(args []string) error { return nil } +// getLocalBaseSnapshotName takes a provided name of the destination snapshot and optionally +// translates it into the local snapshot if --localVolume is given +func getLocalBaseSnapshotName(name string) string { + var localBaseSnapVolumeName string + if jobInfo.LocalVolume != "" { + argParts := strings.Split(name, "@") + if len(argParts) == 2 { + localBaseSnapVolumeName = fmt.Sprintf("%s@%s", jobInfo.LocalVolume, argParts[1]) + } else { + localBaseSnapVolumeName = jobInfo.LocalVolume + } + } else { + localBaseSnapVolumeName = name + } + return localBaseSnapVolumeName +} + func usingSmartOption() bool { return jobInfo.Full || jobInfo.Incremental || jobInfo.FullIfOlderThan != -1*time.Minute } diff --git a/zfs/zfs.go b/zfs/zfs.go index 9453704..0d6424e 100644 --- a/zfs/zfs.go +++ b/zfs/zfs.go @@ -148,7 +148,7 @@ func GetZFSSendCommand(ctx context.Context, j *files.JobInfo) *exec.Cmd { if j.IncrementalSnapshot.Name != "" { incrementalName := j.IncrementalSnapshot.Name if j.IncrementalSnapshot.Bookmark { - incrementalName = fmt.Sprintf("%s#%s", j.VolumeName, incrementalName) + incrementalName = fmt.Sprintf("%s#%s", GetLocalVolumeName(j), incrementalName) } if j.IntermediaryIncremental { @@ -160,7 +160,7 @@ func GetZFSSendCommand(ctx context.Context, j *files.JobInfo) *exec.Cmd { } } - zfsArgs = append(zfsArgs, fmt.Sprintf("%s@%s", j.VolumeName, j.BaseSnapshot.Name)) + zfsArgs = append(zfsArgs, fmt.Sprintf("%s@%s", GetLocalVolumeName(j), j.BaseSnapshot.Name)) cmd := exec.CommandContext(ctx, ZFSPath, zfsArgs...) return cmd @@ -196,8 +196,16 @@ func GetZFSReceiveCommand(ctx context.Context, j *files.JobInfo) *exec.Cmd { zfsArgs = append(zfsArgs, "-o", "origin="+j.Origin) } - zfsArgs = append(zfsArgs, j.LocalVolume) + zfsArgs = append(zfsArgs, GetLocalVolumeName(j)) cmd := exec.CommandContext(ctx, ZFSPath, zfsArgs...) return cmd } + +func GetLocalVolumeName(j *files.JobInfo) string { + if j.LocalVolume != "" { + return j.LocalVolume + } else { + return j.VolumeName + } +}