-
Notifications
You must be signed in to change notification settings - Fork 111
/
dumpdb.go
81 lines (67 loc) · 1.67 KB
/
dumpdb.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
package main
import (
"context"
"fmt"
"io"
"os"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
api "github.com/chef/automate/api/interservice/deployment"
"github.com/chef/automate/components/automate-deployment/pkg/client"
)
func init() {
RootCmd.AddCommand(dumpdbCmd())
}
func dumpdbCmd() *cobra.Command {
return &cobra.Command{
Use: "dumpdb",
Short: "Dump deployment-service's database to stdout",
Long: `Dump deployment-service's database to stdout
Environment:
DEPLOYMENT_SERVICE_ADDRESS set to a string like localhost:10160 to
configure the host and port
`,
Run: runDumpDB,
}
}
func runDumpDB(cmd *cobra.Command, args []string) {
// Data is printed to stdout, so the command will commonly be run as
// deployment-service dumpdb > backup.db
// This means we need to print all other things to stderr
e := os.Stderr
ds, err := client.Connection(1 * time.Second)
if err != nil {
fmt.Fprintf(e, "Error connecting to deployment-service: %s\n", err.Error())
os.Exit(1)
}
ctx := context.Background()
stream, err := ds.DumpDB(ctx, &api.DumpDBRequest{})
if err != nil {
fmt.Fprintf(e, "Error connecting to dumpdb API: %s\n", err.Error())
os.Exit(1)
}
// Write stream to stdout
for {
resp, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
err = errors.Wrap(err, "Error downloading database")
fmt.Fprint(e, err.Error())
os.Exit(1)
}
data := resp.GetData()
if data == nil {
break
}
if _, err := os.Stdout.Write(data); err != nil {
err = errors.Wrap(err, "Error writing database to stdout")
fmt.Fprint(e, err.Error())
os.Exit(1)
}
}
os.Stdout.Sync()
os.Exit(0)
}