Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow for a local ZFS volume that differs from the desired or existin… #465

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 28 additions & 4 deletions cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ 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 @@ -245,7 +250,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 @@ -254,14 +260,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 @@ -301,6 +308,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 @@ -138,7 +138,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 @@ -150,7 +150,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 @@ -186,8 +186,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
}
}