|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sort" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/digineo/zackup/config" |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + patterns = map[string]string{ |
| 15 | + "daily": "2006-01-02", |
| 16 | + "weekly": "", // See special case in keepers() |
| 17 | + "monthly": "2006-01", |
| 18 | + "yearly": "2006", |
| 19 | + } |
| 20 | +) |
| 21 | + |
| 22 | +type snapshot struct { |
| 23 | + Ds string // Snapshot dataset name "backups/foo@RFC3339" |
| 24 | + Time time.Time // Parsed timestamp from the dataset name |
| 25 | +} |
| 26 | + |
| 27 | +func PruneSnapshots(job *config.JobConfig) { |
| 28 | + var host = job.Host() |
| 29 | + |
| 30 | + // Set defaults if config is not set |
| 31 | + if job.Retention == nil { |
| 32 | + job.Retention = &config.RetentionConfig{ |
| 33 | + Daily: 100000, |
| 34 | + Weekly: 100000, |
| 35 | + Monthly: 100000, |
| 36 | + Yearly: 100000, |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // This catches any gaps in the config |
| 41 | + if job.Retention.Daily == 0 { |
| 42 | + job.Retention.Daily = 100000 |
| 43 | + } |
| 44 | + if job.Retention.Weekly == 0 { |
| 45 | + job.Retention.Weekly = 100000 |
| 46 | + } |
| 47 | + if job.Retention.Monthly == 0 { |
| 48 | + job.Retention.Monthly = 100000 |
| 49 | + } |
| 50 | + if job.Retention.Yearly == 0 { |
| 51 | + job.Retention.Yearly = 100000 |
| 52 | + } |
| 53 | + |
| 54 | + // FIXME probably should iterate over a list instead here |
| 55 | + for _, snapshot := range listKeepers(host, "daily", job.Retention.Daily) { |
| 56 | + log.WithFields(logrus.Fields{ |
| 57 | + "snapshot": snapshot, |
| 58 | + "period": "daily", |
| 59 | + }).Debug("keeping snapshot") |
| 60 | + } |
| 61 | + for _, snapshot := range listKeepers(host, "weekly", job.Retention.Weekly) { |
| 62 | + log.WithFields(logrus.Fields{ |
| 63 | + "snapshot": snapshot, |
| 64 | + "period": "weekly", |
| 65 | + }).Debug("keeping snapshot") |
| 66 | + } |
| 67 | + for _, snapshot := range listKeepers(host, "monthly", job.Retention.Monthly) { |
| 68 | + log.WithFields(logrus.Fields{ |
| 69 | + "snapshot": snapshot, |
| 70 | + "period": "monthly", |
| 71 | + }).Debug("keeping snapshot") |
| 72 | + } |
| 73 | + for _, snapshot := range listKeepers(host, "yearly", job.Retention.Yearly) { |
| 74 | + log.WithFields(logrus.Fields{ |
| 75 | + "snapshot": snapshot, |
| 76 | + "period": "yearly", |
| 77 | + }).Debug("keeping snapshot") |
| 78 | + } |
| 79 | + |
| 80 | + // TODO subtract keepers from the list of snapshots and rm -rf them |
| 81 | +} |
| 82 | + |
| 83 | +// listKeepers returns a list of snapshot that are not subject to deletion |
| 84 | +// for a given host, pattern, and keep_count. |
| 85 | +func listKeepers(host string, pattern string, keep_count uint) []snapshot { |
| 86 | + var keepers []snapshot |
| 87 | + var last string |
| 88 | + |
| 89 | + for _, snapshot := range listSnapshots(host) { |
| 90 | + var period string |
| 91 | + |
| 92 | + // Weekly is special because golang doesn't have support for "week number in year" |
| 93 | + // in Time.Format strings. |
| 94 | + if pattern == "weekly" { |
| 95 | + year, week := snapshot.Time.Local().ISOWeek() |
| 96 | + period = fmt.Sprintf("%d-%d", year, week) |
| 97 | + } else { |
| 98 | + period = snapshot.Time.Local().Format(patterns[pattern]) |
| 99 | + } |
| 100 | + |
| 101 | + if period != last { |
| 102 | + last = period |
| 103 | + keepers = append(keepers, snapshot) |
| 104 | + |
| 105 | + if uint(len(keepers)) == keep_count { |
| 106 | + break |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return keepers |
| 112 | +} |
| 113 | + |
| 114 | +// listSnapshots calls out to ZFS for a list of snapshots for a given host. |
| 115 | +// Returned data will be sorted by time, most recent first. |
| 116 | +func listSnapshots(host string) []snapshot { |
| 117 | + var snapshots []snapshot |
| 118 | + |
| 119 | + ds := newDataset(host) |
| 120 | + |
| 121 | + args := []string{ |
| 122 | + "list", |
| 123 | + "-H", // no field headers in output |
| 124 | + "-o", "name", // only name field |
| 125 | + "-t", "snapshot", // type snapshot |
| 126 | + ds.Name, |
| 127 | + } |
| 128 | + o, e, err := execProgram("zfs", args...) |
| 129 | + if err != nil { |
| 130 | + f := appendStdlogs(logrus.Fields{ |
| 131 | + logrus.ErrorKey: err, |
| 132 | + "prefix": "zfs", |
| 133 | + "command": append([]string{"zfs"}, args...), |
| 134 | + }, o, e) |
| 135 | + log.WithFields(f).Errorf("executing zfs list failed") |
| 136 | + } |
| 137 | + |
| 138 | + for _, ss := range strings.Fields(o.String()) { |
| 139 | + ts, err := time.Parse(time.RFC3339, strings.Split(ss, "@")[1]) |
| 140 | + |
| 141 | + if err != nil { |
| 142 | + log.WithField("snapshot", ss).Error("Unable to parse timestamp from snapshot") |
| 143 | + continue |
| 144 | + } |
| 145 | + |
| 146 | + snapshots = append(snapshots, snapshot{ |
| 147 | + Ds: ss, |
| 148 | + Time: ts, |
| 149 | + }) |
| 150 | + } |
| 151 | + |
| 152 | + // ZFS list _should_ be in chronological order but just in case ... |
| 153 | + sort.Slice(snapshots, func(i, j int) bool { |
| 154 | + return snapshots[i].Time.After(snapshots[j].Time) |
| 155 | + }) |
| 156 | + |
| 157 | + return snapshots |
| 158 | +} |
0 commit comments