-
Notifications
You must be signed in to change notification settings - Fork 55
/
postgres.go
61 lines (51 loc) · 1.35 KB
/
postgres.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
package barkup
import (
"fmt"
"os/exec"
"time"
)
var (
// PGDumpCmd is the path to the `pg_dump` executable
PGDumpCmd = "pg_dump"
)
// Postgres is an `Exporter` interface that backs up a Postgres database via the `pg_dump` command
type Postgres struct {
// DB Host (e.g. 127.0.0.1)
Host string
// DB Port (e.g. 5432)
Port string
// DB Name
DB string
// Connection Username
Username string
// Extra pg_dump options
// e.g []string{"--inserts"}
Options []string
}
// Export produces a `pg_dump` of the specified database, and creates a gzip compressed tarball archive.
func (x Postgres) Export() *ExportResult {
result := &ExportResult{MIME: "application/x-tar"}
result.Path = fmt.Sprintf(`bu_%v_%v.sql.tar.gz`, x.DB, time.Now().Unix())
options := append(x.dumpOptions(), "-Fc", fmt.Sprintf(`-f%v`, result.Path))
out, err := exec.Command(PGDumpCmd, options...).Output()
if err != nil {
result.Error = makeErr(err, string(out))
}
return result
}
func (x Postgres) dumpOptions() []string {
options := x.Options
if x.DB != "" {
options = append(options, fmt.Sprintf(`-d%v`, x.DB))
}
if x.Host != "" {
options = append(options, fmt.Sprintf(`-h%v`, x.Host))
}
if x.Port != "" {
options = append(options, fmt.Sprintf(`-p%v`, x.Port))
}
if x.Username != "" {
options = append(options, fmt.Sprintf(`-U%v`, x.Username))
}
return options
}