Skip to content

Commit

Permalink
fix(someone1#457): add more flexible snapshot match by regular expres…
Browse files Browse the repository at this point in the history
…sion.

Signed-off-by: Johnathan Falk <johnathan.falk@gmail.com>
  • Loading branch information
jdfalk committed Nov 13, 2022
1 parent ba726bd commit e4907ad
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
25 changes: 24 additions & 1 deletion backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
Expand All @@ -54,18 +55,35 @@ var (
manifestmutex sync.Mutex
)

type snapshotFilter struct {
prefix string
regexpMatch *regexp.Regexp
}

func newSnapshotFilter(prefix string, match string) *snapshotFilter {
filter := &snapshotFilter{
prefix: prefix,
}
if match != "" {
filter.regexpMatch = regexp.MustCompile(match)
}
log.AppLogger.Debugf("Filtering snapshots with prefix = %s, regex matcher = %v", filter.prefix, filter.regexpMatch)
return filter
}

// 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(), zfs.GetLocalVolumeName(jobInfo))
if err != nil {
return err
}
filter := newSnapshotFilter(jobInfo.SnapshotPrefix, jobInfo.SnapshotRegexp)
// Base Snapshots cannot be a bookmark
for i := range snapshots {
log.AppLogger.Debugf("Considering snapshot %s", snapshots[i].Name)
if !snapshots[i].Bookmark {
if jobInfo.SnapshotPrefix == "" || strings.HasPrefix(snapshots[i].Name, jobInfo.SnapshotPrefix) {
if includeSnapshot(&snapshots[i], filter) {
log.AppLogger.Debugf("Matched snapshot: %s", snapshots[i].Name)
jobInfo.BaseSnapshot = snapshots[i]
break
Expand Down Expand Up @@ -164,6 +182,11 @@ func ProcessSmartOptions(ctx context.Context, jobInfo *files.JobInfo) error {
return nil
}

func includeSnapshot(snapshot *files.SnapshotInfo, filter *snapshotFilter) bool {
return (filter.prefix == "" || strings.HasPrefix(snapshot.Name, filter.prefix)) &&
(filter.regexpMatch == nil || filter.regexpMatch.MatchString(snapshot.Name))
}

// Will list all backups found in the target destination
func getBackupsForTarget(ctx context.Context, volume, target string, jobInfo *files.JobInfo) ([]*files.JobInfo, error) {
// Prepare the backend client
Expand Down
13 changes: 13 additions & 0 deletions backup/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ type errTestFunc func(error) bool

func nilErrTest(e error) bool { return e == nil }

func TestIncludeSnapshot(t *testing.T) {
filter := newSnapshotFilter("", "^weekly.*")

snapInfo := &files.SnapshotInfo{Name: "hourly123"}
if includeSnapshot(snapInfo, filter) {
t.Errorf("%s incorrectly included", snapInfo.Name)
}
snapInfo.Name = "weekly456"
if !includeSnapshot(snapInfo, filter) {
t.Errorf("%s incorrectly excluded", snapInfo.Name)
}
}

func TestRetryUploadChainer(t *testing.T) {
_, goodVol, badVol, err := prepareTestVols()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ func init() {
"",
"Only consider snapshots starting with the given snapshot prefix",
)
sendCmd.Flags().StringVar(
&jobInfo.SnapshotRegexp,
"snapshotRegexp",
"",
"Only consider snapshots matching given regex",
)
sendCmd.Flags().DurationVar(
&jobInfo.FullIfOlderThan,
"fullIfOlderThan",
Expand Down
1 change: 1 addition & 0 deletions files/jobinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type JobInfo struct {
BaseSnapshot SnapshotInfo
IncrementalSnapshot SnapshotInfo
SnapshotPrefix string
SnapshotRegexp string
Compressor string
CompressionLevel int
Separator string
Expand Down

0 comments on commit e4907ad

Please sign in to comment.