-
Notifications
You must be signed in to change notification settings - Fork 3
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
Prune/Retention Policy #3
Draft
ngharo
wants to merge
5
commits into
digineo:master
Choose a base branch
from
ngharo:feature/prune
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+234
−2
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f31694e
Laying out snapshot pruning functionality
ngharo 040e49b
Loop over each bucket when finding keepers
ngharo 49cbad0
Add -r to zfs list command
ngharo c5d8f7d
Optimize call to zfs list and make nil retention policy be infinite
ngharo ea5b6a4
Rename snapshot.Ds to snapshot.Name for clarity
ngharo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
"github.com/digineo/zackup/config" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
patterns = map[string]string{ | ||
"daily": "2006-01-02", | ||
"weekly": "", // See special case in keepers() | ||
"monthly": "2006-01", | ||
"yearly": "2006", | ||
} | ||
) | ||
|
||
type snapshot struct { | ||
Name string // Snapshot dataset name "backups/foo@RFC3339" | ||
Time time.Time // Parsed timestamp from the dataset name | ||
} | ||
|
||
// FIXME PruneSnapshots does not actually perform any destructive operations | ||
// on your datasets at this time. | ||
func PruneSnapshots(job *config.JobConfig) { | ||
var host = job.Host() | ||
|
||
// Defaults: if config is not set | ||
if job.Retention == nil { | ||
job.Retention = &config.RetentionConfig{ | ||
Daily: nil, | ||
Weekly: nil, | ||
Monthly: nil, | ||
Yearly: nil, | ||
} | ||
} | ||
|
||
var policies = map[string]*int{ | ||
"daily": job.Retention.Daily, | ||
"weekly": job.Retention.Weekly, | ||
"monthly": job.Retention.Monthly, | ||
"yearly": job.Retention.Yearly, | ||
} | ||
|
||
snapshots := listSnapshots(host) | ||
|
||
for bucket, retention := range policies { | ||
for _, snapshot := range listKeepers(snapshots, bucket, retention) { | ||
l := log.WithFields(logrus.Fields{ | ||
"snapshot": snapshot, | ||
"bucket": bucket, | ||
}) | ||
|
||
if retention == nil { | ||
l = l.WithField("retention", "infinite") | ||
} else { | ||
l = l.WithField("retention", *retention) | ||
} | ||
|
||
l.Debug("keeping snapshot") | ||
} | ||
} | ||
|
||
// TODO subtract keepers from the list of snapshots and rm -rf them | ||
} | ||
|
||
// listKeepers returns a list of snapshot that are not subject to deletion | ||
// for a given host, pattern, and retention. | ||
func listKeepers(snapshots []snapshot, bucket string, retention *int) []snapshot { | ||
var keepers []snapshot | ||
var last string | ||
|
||
for _, snapshot := range snapshots { | ||
var period string | ||
|
||
// Weekly is special because golang doesn't have support for "week number in year" | ||
// as Time.Format string pattern. | ||
if bucket == "weekly" { | ||
year, week := snapshot.Time.Local().ISOWeek() | ||
period = fmt.Sprintf("%d-%d", year, week) | ||
} else { | ||
period = snapshot.Time.Local().Format(patterns[bucket]) | ||
} | ||
|
||
if period != last { | ||
last = period | ||
keepers = append(keepers, snapshot) | ||
|
||
// nil will keep infinite snapshots | ||
if retention == nil { | ||
continue | ||
} | ||
|
||
if len(keepers) == *retention { | ||
break | ||
} | ||
} | ||
} | ||
|
||
return keepers | ||
} | ||
|
||
// listSnapshots calls out to ZFS for a list of snapshots for a given host. | ||
// Returned data will be sorted by time, most recent first. | ||
func listSnapshots(host string) []snapshot { | ||
var snapshots []snapshot | ||
|
||
ds := newDataset(host) | ||
|
||
args := []string{ | ||
"list", | ||
"-r", // recursive | ||
"-H", // no field headers in output | ||
"-o", "name", // only name field | ||
"-t", "snapshot", // type snapshot | ||
ds.Name, | ||
} | ||
o, e, err := execProgram("zfs", args...) | ||
if err != nil { | ||
f := appendStdlogs(logrus.Fields{ | ||
logrus.ErrorKey: err, | ||
"prefix": "zfs", | ||
"command": append([]string{"zfs"}, args...), | ||
}, o, e) | ||
log.WithFields(f).Errorf("executing zfs list failed") | ||
} | ||
|
||
for _, ss := range strings.Fields(o.String()) { | ||
ts, err := time.Parse(time.RFC3339, strings.Split(ss, "@")[1]) | ||
|
||
if err != nil { | ||
log.WithField("snapshot", ss).Error("Unable to parse timestamp from snapshot") | ||
continue | ||
} | ||
|
||
snapshots = append(snapshots, snapshot{ | ||
Name: ss, | ||
Time: ts, | ||
}) | ||
} | ||
|
||
// ZFS list _should_ be in chronological order but just in case ... | ||
sort.Slice(snapshots, func(i, j int) bool { | ||
return snapshots[i].Time.After(snapshots[j].Time) | ||
}) | ||
|
||
return snapshots | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/digineo/zackup/app" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// pruneCmd represents the prune command | ||
var pruneCmd = &cobra.Command{ | ||
Use: "prune [host [...]]", | ||
Short: "Prunes backups per-host ZFS dataset", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) == 0 { | ||
args = tree.Hosts() | ||
} | ||
|
||
for _, host := range args { | ||
job := tree.Host(host) | ||
if job == nil { | ||
log.WithField("prune", host).Warn("unknown host, ignoring") | ||
continue | ||
} | ||
|
||
app.PruneSnapshots(job) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(pruneCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,5 @@ require ( | |
gopkg.in/gemnasium/logrus-graylog-hook.v2 v2.0.7 | ||
gopkg.in/yaml.v2 v2.2.2 | ||
) | ||
|
||
go 1.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string.Fields(o.String())
andstrings.Split
produce quite a few allocations. We can avoid them with something like this:This parses the output line-by-line (with a
bufio.Scanner
), and converts only the part after the@
in each line to a string.