-
Notifications
You must be signed in to change notification settings - Fork 15
/
etcd_localbackup.go
148 lines (127 loc) · 3.22 KB
/
etcd_localbackup.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
package cmd
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/cybozu-go/well"
"github.com/spf13/cobra"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/etcdutl/v3/snapshot"
)
var config struct {
maxBackups int
dir string
}
const backupTimeFormat = "20060102-150405"
func backupFilename(t time.Time) string {
return fmt.Sprintf("etcd-%s.backup", t.UTC().Format(backupTimeFormat))
}
var etcdLocalBackupCmd = &cobra.Command{
Use: "local-backup",
Short: "take a snapshot of CKE-managed etcd data and save it",
Long: `This command takes a snapshot of CKE-managed etcd that stores Kubernetes data.
The snapshots are saved in a directory specified with --dir flag
with this format: etcd-YYYYMMDD-hhmmss.backup
The date and time is UTC.
Old backups are automatically removed when the number of backup files
exceed the maximum specified with --max-backups flag.`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
cmd.SilenceUsage = true
well.Go(func(ctx context.Context) error {
etcd, err := inf.NewEtcdClient(ctx, nil)
if err != nil {
return err
}
err = backup(ctx, etcd)
if err != nil {
return fmt.Errorf("failed to take a backup: %w", err)
}
return removeOldBackups()
})
well.Stop()
return well.Wait()
},
}
func backup(ctx context.Context, etcd *clientv3.Client) error {
r, err := etcd.Snapshot(ctx)
if err != nil {
return err
}
defer r.Close()
switch fi, err := os.Stat(config.dir); {
case err == nil:
if !fi.IsDir() {
return fmt.Errorf("%s is not a directory", config.dir)
}
case os.IsNotExist(err):
if err := os.MkdirAll(config.dir, 0755); err != nil {
return err
}
default:
return err
}
fname := backupFilename(time.Now())
fullName := filepath.Join(config.dir, fname)
w, err := os.Create(fullName)
if err != nil {
return err
}
defer w.Close()
if _, err := io.Copy(w, r); err != nil {
os.Remove(fullName)
return err
}
if err := w.Sync(); err != nil {
os.Remove(fullName)
return err
}
ss := snapshot.NewV3(nil)
if _, err := ss.Status(fullName); err != nil {
os.Remove(fullName)
return fmt.Errorf("failed to check status of the new backup: %w", err)
}
fmt.Printf("created backup %s\n", fname)
return nil
}
func removeOldBackups() error {
fis, err := os.ReadDir(config.dir)
if err != nil {
return err
}
names := make([]string, 0, len(fis))
for _, fi := range fis {
name := fi.Name()
if strings.HasPrefix(name, "etcd-") && strings.HasSuffix(name, ".backup") {
names = append(names, name)
}
}
sort.Strings(names)
toRemove := len(names) - config.maxBackups
if toRemove > 0 {
for _, name := range names[0:toRemove] {
err := os.Remove(filepath.Join(config.dir, name))
if err != nil {
return fmt.Errorf("failed to remove %s: %w", name, err)
}
fmt.Printf("removed %s\n", name)
}
}
d, err := os.Open(config.dir)
if err != nil {
return err
}
defer d.Close()
return d.Sync()
}
func init() {
etcdCmd.AddCommand(etcdLocalBackupCmd)
f := etcdLocalBackupCmd.Flags()
f.IntVar(&config.maxBackups, "max-backups", 10, "the maximum number of backups to keep")
f.StringVar(&config.dir, "dir", "/var/cke/etcd-backups", "the directory to keep the backup files")
}