/
postgresql.go
127 lines (108 loc) 路 2.71 KB
/
postgresql.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
package database
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/gobackup/gobackup/helper"
"github.com/gobackup/gobackup/logger"
)
// PostgreSQL database
//
// ref:
// https://www.postgresql.org/docs/current/app-pgdump.html
//
// # Keys
//
// - type: postgresql
// - host: localhost
// - port: 5432
// - socket:
// - database:
// - username:
// - password:
// - tables:
// - exclude_tables:
// - args:
type PostgreSQL struct {
Base
host string
port string
socket string
database string
username string
tables []string
excludeTables []string
password string
args string
_dumpFilePath string
}
func (db *PostgreSQL) init() (err error) {
viper := db.viper
viper.SetDefault("host", "localhost")
viper.SetDefault("port", 5432)
db.host = viper.GetString("host")
db.port = viper.GetString("port")
db.socket = viper.GetString("socket")
db.database = viper.GetString("database")
db.username = viper.GetString("username")
db.password = viper.GetString("password")
db.tables = viper.GetStringSlice("tables")
db.excludeTables = viper.GetStringSlice("exclude_tables")
db.args = viper.GetString("args")
if len(db.database) == 0 {
return fmt.Errorf("PostgreSQL database config is required")
}
db._dumpFilePath = path.Join(db.dumpPath, db.database+".sql")
// socket
if len(db.socket) != 0 {
db.host = ""
db.port = ""
}
return nil
}
func (db *PostgreSQL) build() string {
// pg_dump command
var dumpArgs []string
if len(db.host) > 0 {
dumpArgs = append(dumpArgs, "--host="+db.host)
}
if len(db.port) > 0 {
dumpArgs = append(dumpArgs, "--port="+db.port)
}
if len(db.socket) > 0 {
host := filepath.Dir(db.socket)
port := strings.TrimPrefix(filepath.Ext(db.socket), ".")
dumpArgs = append(dumpArgs, "--host="+host, "--port="+port)
}
if len(db.username) > 0 {
dumpArgs = append(dumpArgs, "--username="+db.username)
}
// include / exclude tables
if len(db.tables) > 0 {
dumpArgs = append(dumpArgs, "--table="+strings.Join(db.tables, " --table="))
}
if len(db.excludeTables) > 0 {
dumpArgs = append(dumpArgs, "--exclude-table="+strings.Join(db.excludeTables, " --exclude-table="))
}
if len(db.args) > 0 {
dumpArgs = append(dumpArgs, db.args)
}
dumpArgs = append(dumpArgs, db.database)
dumpArgs = append(dumpArgs, "-f", db._dumpFilePath)
return "pg_dump " + strings.Join(dumpArgs, " ")
}
func (db *PostgreSQL) perform() error {
logger := logger.Tag("PostgreSQL")
logger.Info("-> Dumping PostgreSQL...")
if len(db.password) > 0 {
os.Setenv("PGPASSWORD", db.password)
}
_, err := helper.Exec(db.build())
if err != nil {
return err
}
logger.Info("dump path:", db._dumpFilePath)
return nil
}