-
Notifications
You must be signed in to change notification settings - Fork 111
/
restoredb.go
52 lines (40 loc) · 1.09 KB
/
restoredb.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
package main
import (
"fmt"
"io"
"os"
"path"
"github.com/spf13/cobra"
"github.com/chef/automate/components/automate-deployment/pkg/server"
)
func init() {
RootCmd.AddCommand(restoreDBCmd())
}
func restoreDBCmd() *cobra.Command {
return &cobra.Command{
Use: "restoredb CONFIG_FILE",
Short: "Restore deployment-service's database from stdin stream of a dump",
Long: "Restore deployment-service's database from stdin stream of a dump",
Run: runRestoreDB,
}
}
func runRestoreDB(cmd *cobra.Command, args []string) {
e := os.Stderr
dbFile := path.Join(server.DataDir, server.DBName)
f, err := os.OpenFile(dbFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
fmt.Fprintf(e, "failed to open db file %s for writing: %v", dbFile, err)
os.Exit(1)
}
defer f.Close()
if _, err = io.Copy(f, os.Stdin); err != nil {
fmt.Fprintf(e, "error copying backup data from stdin to db file: %v", err)
os.Exit(1)
}
err = f.Sync()
if err != nil {
fmt.Fprintf(e, "WARN: sync failed: %s\n", err.Error())
}
fmt.Fprintf(e, "Backup data restored to file: %s\n", dbFile)
os.Exit(0)
}