-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
255 lines (235 loc) · 4.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"syscall"
"time"
"github.com/mistifyio/go-zfs"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "flux"
app.Version = "1"
app.Usage = "going back in time with zfs"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "enable debug output in the logs",
},
}
app.Commands = []cli.Command{
snapshotCommand,
purgeCommand,
}
app.Before = func(clix *cli.Context) error {
if clix.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
const (
Day = 24 * time.Hour
Week = 7 * Day
TypeSnapshot = "snapshot"
)
var purgeCommand = cli.Command{
Name: "purge",
Usage: "purge old snapshots",
Flags: []cli.Flag{
cli.DurationFlag{
Name: "older-than,o",
Usage: "purge snapshots older than",
Value: 2 * Week,
},
cli.BoolFlag{
Name: "dry",
Usage: "display don't delete",
},
},
Action: func(clix *cli.Context) error {
mark := time.Now().Add(-clix.Duration("older-than"))
data, err := zfs.GetDataset("tank")
if err != nil {
return err
}
sets, err := data.Children(0)
if err != nil {
return err
}
for _, d := range sets {
if d.Type != TypeSnapshot {
continue
}
created, err := getCreationTime(d)
if err != nil {
logrus.WithError(err).Error("get creation time")
continue
}
if created.Before(mark) {
logrus.Debugf("destory %s", d.Name)
if !clix.Bool("dry") {
if err := d.Destroy(zfs.DestroyDefault); err != nil {
logrus.WithError(err).Error("unable destroy")
}
}
}
}
return nil
},
}
const creationProp = "creation"
func getCreationTime(d *zfs.Dataset) (time.Time, error) {
p, err := d.GetProperty(creationProp)
if err != nil {
return time.Time{}, err
}
v, err := strconv.Atoi(p)
if err != nil {
return time.Time{}, err
}
return time.Unix(int64(v), 0), nil
}
var snapshotCommand = cli.Command{
Name: "snapshot",
Usage: "snapshot",
Flags: []cli.Flag{
cli.StringFlag{
Name: "send,s",
Usage: "send to an ssh target",
},
cli.StringFlag{
Name: "dest,d",
Usage: "destination",
},
cli.UintFlag{
Name: "uid",
Usage: "ssh user",
},
cli.UintFlag{
Name: "gid",
Usage: "ssh group",
},
cli.BoolFlag{
Name: "init",
Usage: "send the inital snapshot",
},
},
Action: func(clix *cli.Context) error {
var (
now = time.Now()
names = clix.Args()
target = clix.String("send")
dest = clix.String("dest")
initS = clix.Bool("init")
)
for _, name := range names {
set, err := zfs.GetDataset(name)
if err != nil {
return err
}
snapshots, err := getSnapshots(set)
if err != nil {
return err
}
prev := snapshots[len(snapshots)-1]
if initS {
prev = nil
}
snapshot, err := set.Snapshot(now.Format(time.RFC3339), false)
if err != nil {
return err
}
if target != "" {
if dest == "" {
return errors.New("no dest specified")
}
if err := send(target, dest, uint32(clix.Uint("uid")), uint32(clix.Uint("gid")), snapshot, prev); err != nil {
return err
}
}
}
return nil
},
}
func send(target, dest string, uid, gid uint32, set *zfs.Dataset, prev *ExtDataset) error {
ssh := sshSend(target, dest)
ssh.SysProcAttr = &syscall.SysProcAttr{
Credential: &syscall.Credential{
Uid: uid,
Gid: gid,
},
}
in, err := ssh.StdinPipe()
if err != nil {
return err
}
defer in.Close()
ssh.Stderr = os.Stderr
ssh.Stdout = os.Stdout
if err := ssh.Start(); err != nil {
return err
}
if prev == nil {
if err := set.SendSnapshot(in); err != nil {
return err
}
return ssh.Wait()
}
if err := set.IncrementalSend(prev.Dataset, in); err != nil {
return err
}
return ssh.Wait()
}
func sshSend(target, dest string) *exec.Cmd {
return exec.Command("ssh", target, "zfs", "recv", dest)
}
type ExtDataset struct {
*zfs.Dataset
BaseName string
Created time.Time
}
func getSnapshots(set *zfs.Dataset) ([]*ExtDataset, error) {
sets, err := set.Children(0)
if err != nil {
return nil, err
}
var out []*ExtDataset
for _, s := range sets {
if s.Type == TypeSnapshot {
created, err := getCreationTime(s)
if err != nil {
continue
}
out = append(out, &ExtDataset{
Dataset: s,
BaseName: strings.Split(s.Name, "@")[0],
Created: created,
})
}
}
sort.Sort(byCreated(out))
return out, nil
}
var errNoTime = errors.New("no time specified")
type byCreated []*ExtDataset
func (s byCreated) Len() int {
return len(s)
}
func (s byCreated) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byCreated) Less(i, j int) bool {
return s[i].Created.Before(s[j].Created)
}