Skip to content

Commit c9863c4

Browse files
committed
fix(someone1#465): ALlow for a local zfs volume that differs from the desired.
Signed-off-by: Johnathan Falk <[email protected]>
1 parent c7a8256 commit c9863c4

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

backup/backup.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ var (
5757
// ProcessSmartOptions will compute the snapshots to use
5858
// nolint:funlen,gocyclo // Difficult to break this up
5959
func ProcessSmartOptions(ctx context.Context, jobInfo *files.JobInfo) error {
60-
snapshots, err := zfs.GetSnapshotsAndBookmarks(context.Background(), jobInfo.VolumeName)
60+
snapshots, err := zfs.GetSnapshotsAndBookmarks(context.Background(), zfs.GetLocalVolumeName(jobInfo))
6161
if err != nil {
6262
return err
6363
}
@@ -246,7 +246,7 @@ func Backup(pctx context.Context, jobInfo *files.JobInfo) error {
246246
}
247247

248248
// Validate the snapshots we want to use exist
249-
if ok, verr := validateSnapShotExists(ctx, &jobInfo.BaseSnapshot, jobInfo.VolumeName, false); verr != nil {
249+
if ok, verr := validateSnapShotExists(ctx, &jobInfo.BaseSnapshot, zfs.GetLocalVolumeName(jobInfo), false); verr != nil {
250250
log.AppLogger.Errorf("Cannot validate if selected base snapshot exists due to error - %v", verr)
251251
return verr
252252
} else if !ok {
@@ -255,7 +255,7 @@ func Backup(pctx context.Context, jobInfo *files.JobInfo) error {
255255
}
256256

257257
if jobInfo.IncrementalSnapshot.Name != "" {
258-
if ok, verr := validateSnapShotExists(ctx, &jobInfo.IncrementalSnapshot, jobInfo.VolumeName, true); verr != nil {
258+
if ok, verr := validateSnapShotExists(ctx, &jobInfo.IncrementalSnapshot, zfs.GetLocalVolumeName(jobInfo), true); verr != nil {
259259
log.AppLogger.Errorf("Cannot validate if selected incremental snapshot exists due to error - %v", verr)
260260
return verr
261261
} else if !ok {

cmd/send.go

+29-4
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ func init() {
179179
10,
180180
"the chunk size, in MiB, to use when uploading. A minimum of 5MiB and maximum of 100MiB is enforced.",
181181
)
182+
sendCmd.Flags().StringVar(
183+
&jobInfo.LocalVolume,
184+
"localVolume",
185+
"",
186+
"the local volume name if different from the S3 volume",
187+
)
182188
}
183189

184190
// ResetSendJobInfo exists solely for integration testing
@@ -248,7 +254,8 @@ func updateJobInfo(args []string) error {
248254
return errInvalidInput
249255
}
250256
jobInfo.BaseSnapshot = files.SnapshotInfo{Name: parts[1]}
251-
creationTime, err := zfs.GetCreationDate(context.TODO(), args[0])
257+
localBaseSnapVolumeName := getLocalBaseSnapshotName(args[0])
258+
creationTime, err := zfs.GetCreationDate(context.TODO(), localBaseSnapVolumeName)
252259
if err != nil {
253260
log.AppLogger.Errorf("Error trying to get creation date of specified base snapshot - %v", err)
254261
return err
@@ -257,14 +264,15 @@ func updateJobInfo(args []string) error {
257264

258265
if jobInfo.IncrementalSnapshot.Name != "" {
259266
var targetName string
260-
jobInfo.IncrementalSnapshot.Name = strings.TrimPrefix(jobInfo.IncrementalSnapshot.Name, jobInfo.VolumeName)
267+
localVolumeName := zfs.GetLocalVolumeName(&jobInfo)
268+
jobInfo.IncrementalSnapshot.Name = strings.TrimPrefix(jobInfo.IncrementalSnapshot.Name, localVolumeName)
261269
if strings.HasPrefix(jobInfo.IncrementalSnapshot.Name, "#") {
262270
jobInfo.IncrementalSnapshot.Name = strings.TrimPrefix(jobInfo.IncrementalSnapshot.Name, "#")
263-
targetName = fmt.Sprintf("%s#%s", jobInfo.VolumeName, jobInfo.IncrementalSnapshot.Name)
271+
targetName = fmt.Sprintf("%s#%s", localVolumeName, jobInfo.IncrementalSnapshot.Name)
264272
jobInfo.IncrementalSnapshot.Bookmark = true
265273
} else {
266274
jobInfo.IncrementalSnapshot.Name = strings.TrimPrefix(jobInfo.IncrementalSnapshot.Name, "@")
267-
targetName = fmt.Sprintf("%s@%s", jobInfo.VolumeName, jobInfo.IncrementalSnapshot.Name)
275+
targetName = fmt.Sprintf("%s@%s", localVolumeName, jobInfo.IncrementalSnapshot.Name)
268276
}
269277

270278
creationTime, err = zfs.GetCreationDate(context.TODO(), targetName)
@@ -304,6 +312,23 @@ func updateJobInfo(args []string) error {
304312
return nil
305313
}
306314

315+
// getLocalBaseSnapshotName takes a provided name of the destination snapshot and optionally
316+
// translates it into the local snapshot if --localVolume is given
317+
func getLocalBaseSnapshotName(name string) string {
318+
var localBaseSnapVolumeName string
319+
if jobInfo.LocalVolume != "" {
320+
argParts := strings.Split(name, "@")
321+
if len(argParts) == 2 {
322+
localBaseSnapVolumeName = fmt.Sprintf("%s@%s", jobInfo.LocalVolume, argParts[1])
323+
} else {
324+
localBaseSnapVolumeName = jobInfo.LocalVolume
325+
}
326+
} else {
327+
localBaseSnapVolumeName = name
328+
}
329+
return localBaseSnapVolumeName
330+
}
331+
307332
func usingSmartOption() bool {
308333
return jobInfo.Full || jobInfo.Incremental || jobInfo.FullIfOlderThan != -1*time.Minute
309334
}

zfs/zfs.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func GetZFSSendCommand(ctx context.Context, j *files.JobInfo) *exec.Cmd {
148148
if j.IncrementalSnapshot.Name != "" {
149149
incrementalName := j.IncrementalSnapshot.Name
150150
if j.IncrementalSnapshot.Bookmark {
151-
incrementalName = fmt.Sprintf("%s#%s", j.VolumeName, incrementalName)
151+
incrementalName = fmt.Sprintf("%s#%s", GetLocalVolumeName(j), incrementalName)
152152
}
153153

154154
if j.IntermediaryIncremental {
@@ -160,7 +160,7 @@ func GetZFSSendCommand(ctx context.Context, j *files.JobInfo) *exec.Cmd {
160160
}
161161
}
162162

163-
zfsArgs = append(zfsArgs, fmt.Sprintf("%s@%s", j.VolumeName, j.BaseSnapshot.Name))
163+
zfsArgs = append(zfsArgs, fmt.Sprintf("%s@%s", GetLocalVolumeName(j), j.BaseSnapshot.Name))
164164
cmd := exec.CommandContext(ctx, ZFSPath, zfsArgs...)
165165

166166
return cmd
@@ -196,8 +196,16 @@ func GetZFSReceiveCommand(ctx context.Context, j *files.JobInfo) *exec.Cmd {
196196
zfsArgs = append(zfsArgs, "-o", "origin="+j.Origin)
197197
}
198198

199-
zfsArgs = append(zfsArgs, j.LocalVolume)
199+
zfsArgs = append(zfsArgs, GetLocalVolumeName(j))
200200
cmd := exec.CommandContext(ctx, ZFSPath, zfsArgs...)
201201

202202
return cmd
203203
}
204+
205+
func GetLocalVolumeName(j *files.JobInfo) string {
206+
if j.LocalVolume != "" {
207+
return j.LocalVolume
208+
} else {
209+
return j.VolumeName
210+
}
211+
}

0 commit comments

Comments
 (0)