/
dump.go
105 lines (85 loc) · 2.7 KB
/
dump.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
package etcd
import (
"archive/tar"
"compress/gzip"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/contiv/errored"
"github.com/coreos/etcd/client"
"golang.org/x/net/context"
)
// Dump yields a database dump of the keyspace we manage. It will be contained
// in a tarball based on the timestamp of the dump. If a dir is provided, it
// will be placed under that directory.
func (c *Client) Dump(dir string) (string, error) {
resp, err := c.client.Get(context.Background(), c.prefix, &client.GetOptions{Sort: true, Recursive: true, Quorum: true})
if err != nil {
return "", errored.Errorf(`Failed to recursively GET "%v" namespace from etcd`, c.prefix).Combine(err)
}
// FIXME the code below (yes, all of it) should be moved somewhere else. It
// doesn't belong in the etcd client.
now := time.Now()
// tar hangs during unpacking if the base directory has colons in it
// unless --force-local is specified, so use the simpler "%Y%m%d-%H%M%S".
niceTimeFormat := fmt.Sprintf("%d%02d%02d-%02d%02d%02d",
now.Year(), now.Month(), now.Day(),
now.Hour(), now.Minute(), now.Second())
file, err := ioutil.TempFile(dir, "etcd_dump_"+niceTimeFormat+"_")
if err != nil {
return "", errored.Errorf("Failed to create tempfile").Combine(err)
}
defer file.Close()
// create a gzipped, tarball writer which cleans up after itself
gzipWriter := gzip.NewWriter(file)
defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()
// ensure that the tarball extracts to a folder with the same name as the tarball
baseDirectory := filepath.Base(file.Name())
err = addNodeToTarball(resp.Node, tarWriter, baseDirectory)
if err != nil {
return "", err
}
// give the file a more fitting name
newFilename := file.Name() + ".tar.gz"
err = os.Rename(file.Name(), newFilename)
if err != nil {
return "", err
}
return newFilename, nil
}
func addNodeToTarball(node *client.Node, writer *tar.Writer, baseDirectory string) error {
now := time.Now()
header := &tar.Header{
AccessTime: now,
ChangeTime: now,
ModTime: now,
Name: baseDirectory + node.Key,
}
if node.Dir {
header.Mode = 0700
header.Typeflag = tar.TypeDir
} else {
header.Mode = 0600
header.Size = int64(len(node.Value))
header.Typeflag = tar.TypeReg
}
err := writer.WriteHeader(header)
if err != nil {
return errored.Errorf("Failed to write tar entry header").Combine(err)
}
// we don't have to write anything for directories except the header
if !node.Dir {
_, err = writer.Write([]byte(node.Value))
if err != nil {
return errored.Errorf("Failed to write tar entry").Combine(err)
}
}
for _, n := range node.Nodes {
addNodeToTarball(n, writer, baseDirectory)
}
return nil
}