-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
128 lines (99 loc) · 2.71 KB
/
db.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
package main
import (
"bytes"
"fmt"
"github.com/gernest/wow"
"github.com/gernest/wow/spin"
"os"
"os/exec"
)
func backup(database string, filename string, clone bool) error {
configs, err := openConfigs(database)
if err != nil {
return err
}
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
os.Setenv("PGPASSWORD", configs.Password)
cmd := exec.Command("pg_dump", "-h", configs.Host, "-p", configs.Port, "-Fc", "-U", configs.User, "-d", configs.Database)
var stderr bytes.Buffer
cmd.Stderr = &stderr
cmd.Stdout = file
if !clone {
fmt.Println()
w := wow.New(os.Stdout, spin.Get(spin.Dots), fmt.Sprintf(" Backing up '%s' database", configs.Database))
w.Start()
}
err = cmd.Run()
if err != nil {
return fmt.Errorf(stderr.String())
}
if !clone {
fmt.Println(fmt.Sprintf("\n\n Database backed up in %s", filename))
}
return nil
}
func revive(database string, filename string, clone bool) error {
configs, err := openConfigs(database)
if err != nil {
return err
}
cmd := exec.Command("pg_restore", "-d", configs.Database, "-U", configs.User, "-h", configs.Host, "-p", configs.Port, "--no-owner", "--no-privileges", filename)
var stderr bytes.Buffer
cmd.Stderr = &stderr
if !clone {
fmt.Println()
w := wow.New(os.Stdout, spin.Get(spin.Dots), fmt.Sprintf(" Reviving %s", filename))
w.Start()
}
err = cmd.Run()
if err != nil {
return fmt.Errorf(stderr.String())
}
if clone {
fmt.Println(fmt.Sprintf("\n\n Database cloned into '%s'", database))
} else {
fmt.Println(fmt.Sprintf("\n\n Database revived in '%s' database", database))
}
return nil
}
func prepareDb(database string) error {
configs, err := openConfigs(database)
if err != nil {
return err
}
os.Setenv("PGPASSWORD", configs.Password)
cmd := exec.Command("dropdb", "-U", configs.User, "-h", configs.Host, "-p", configs.Port, configs.Database, "--if-exists")
var stderr bytes.Buffer
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
return fmt.Errorf(stderr.String())
}
cmd = exec.Command("createdb", "-U", configs.User, "-h", configs.Host, "-p", configs.Port, configs.Database)
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
return fmt.Errorf(stderr.String())
}
return nil
}
func checkDbCons(database string) error {
configs, err := openConfigs(database)
if err != nil {
return err
}
os.Setenv("PGPASSWORD", configs.Password)
cmd := exec.Command("pg_isready", "-U", configs.User, "-h", configs.Host, "-p", configs.Port, "-d", configs.Database)
var stderr, stdout bytes.Buffer
cmd.Stderr = &stderr
cmd.Stdout = &stdout
err = cmd.Run()
if err != nil {
return fmt.Errorf("Error: Cannot make a connection with %s database", configs.Name)
}
return nil
}