Skip to content

Commit

Permalink
fix(someone1#465): ALlow for a local zfs volume that differs from the…
Browse files Browse the repository at this point in the history
… desired.

Signed-off-by: Johnathan Falk <[email protected]>
  • Loading branch information
jdfalk committed Nov 13, 2022
1 parent c7a8256 commit c9863c4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
6 changes: 3 additions & 3 deletions backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
33 changes: 29 additions & 4 deletions cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 11 additions & 3 deletions zfs/zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}

0 comments on commit c9863c4

Please sign in to comment.