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

add more flexible snapshot match by regular expression #457

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
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(), jobInfo.VolumeName)
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 @@ -163,6 +181,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 @@ -117,6 +117,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 @@ -45,6 +45,7 @@ type JobInfo struct {
BaseSnapshot SnapshotInfo
IncrementalSnapshot SnapshotInfo
SnapshotPrefix string
SnapshotRegexp string
Compressor string
CompressionLevel int
Separator string
Expand Down