Skip to content

Commit e4907ad

Browse files
committed
fix(someone1#457): add more flexible snapshot match by regular expression.
Signed-off-by: Johnathan Falk <johnathan.falk@gmail.com>
1 parent ba726bd commit e4907ad

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

backup/backup.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"io/ioutil"
3232
"os"
3333
"path/filepath"
34+
"regexp"
3435
"sort"
3536
"strings"
3637
"sync"
@@ -54,18 +55,35 @@ var (
5455
manifestmutex sync.Mutex
5556
)
5657

58+
type snapshotFilter struct {
59+
prefix string
60+
regexpMatch *regexp.Regexp
61+
}
62+
63+
func newSnapshotFilter(prefix string, match string) *snapshotFilter {
64+
filter := &snapshotFilter{
65+
prefix: prefix,
66+
}
67+
if match != "" {
68+
filter.regexpMatch = regexp.MustCompile(match)
69+
}
70+
log.AppLogger.Debugf("Filtering snapshots with prefix = %s, regex matcher = %v", filter.prefix, filter.regexpMatch)
71+
return filter
72+
}
73+
5774
// ProcessSmartOptions will compute the snapshots to use
5875
// nolint:funlen,gocyclo // Difficult to break this up
5976
func ProcessSmartOptions(ctx context.Context, jobInfo *files.JobInfo) error {
6077
snapshots, err := zfs.GetSnapshotsAndBookmarks(context.Background(), zfs.GetLocalVolumeName(jobInfo))
6178
if err != nil {
6279
return err
6380
}
81+
filter := newSnapshotFilter(jobInfo.SnapshotPrefix, jobInfo.SnapshotRegexp)
6482
// Base Snapshots cannot be a bookmark
6583
for i := range snapshots {
6684
log.AppLogger.Debugf("Considering snapshot %s", snapshots[i].Name)
6785
if !snapshots[i].Bookmark {
68-
if jobInfo.SnapshotPrefix == "" || strings.HasPrefix(snapshots[i].Name, jobInfo.SnapshotPrefix) {
86+
if includeSnapshot(&snapshots[i], filter) {
6987
log.AppLogger.Debugf("Matched snapshot: %s", snapshots[i].Name)
7088
jobInfo.BaseSnapshot = snapshots[i]
7189
break
@@ -164,6 +182,11 @@ func ProcessSmartOptions(ctx context.Context, jobInfo *files.JobInfo) error {
164182
return nil
165183
}
166184

185+
func includeSnapshot(snapshot *files.SnapshotInfo, filter *snapshotFilter) bool {
186+
return (filter.prefix == "" || strings.HasPrefix(snapshot.Name, filter.prefix)) &&
187+
(filter.regexpMatch == nil || filter.regexpMatch.MatchString(snapshot.Name))
188+
}
189+
167190
// Will list all backups found in the target destination
168191
func getBackupsForTarget(ctx context.Context, volume, target string, jobInfo *files.JobInfo) ([]*files.JobInfo, error) {
169192
// Prepare the backend client

backup/backup_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ type errTestFunc func(error) bool
6565

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

68+
func TestIncludeSnapshot(t *testing.T) {
69+
filter := newSnapshotFilter("", "^weekly.*")
70+
71+
snapInfo := &files.SnapshotInfo{Name: "hourly123"}
72+
if includeSnapshot(snapInfo, filter) {
73+
t.Errorf("%s incorrectly included", snapInfo.Name)
74+
}
75+
snapInfo.Name = "weekly456"
76+
if !includeSnapshot(snapInfo, filter) {
77+
t.Errorf("%s incorrectly excluded", snapInfo.Name)
78+
}
79+
}
80+
6881
func TestRetryUploadChainer(t *testing.T) {
6982
_, goodVol, badVol, err := prepareTestVols()
7083
if err != nil {

cmd/send.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ func init() {
119119
"",
120120
"Only consider snapshots starting with the given snapshot prefix",
121121
)
122+
sendCmd.Flags().StringVar(
123+
&jobInfo.SnapshotRegexp,
124+
"snapshotRegexp",
125+
"",
126+
"Only consider snapshots matching given regex",
127+
)
122128
sendCmd.Flags().DurationVar(
123129
&jobInfo.FullIfOlderThan,
124130
"fullIfOlderThan",

files/jobinfo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type JobInfo struct {
4343
BaseSnapshot SnapshotInfo
4444
IncrementalSnapshot SnapshotInfo
4545
SnapshotPrefix string
46+
SnapshotRegexp string
4647
Compressor string
4748
CompressionLevel int
4849
Separator string

0 commit comments

Comments
 (0)