Skip to content

Commit 4e2ec39

Browse files
committed
Use connection string and do not create file
1 parent e776948 commit 4e2ec39

2 files changed

Lines changed: 60 additions & 65 deletions

File tree

config.example.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
database:
22
name:
3-
username: # Used with --container
4-
password: # Used with --container
3+
username:
4+
password:
5+
host:
6+
port:
57

68
s3:
79
bucket:

main.go

Lines changed: 56 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ import (
2020
)
2121

2222
var (
23-
config *Config
24-
filePath string
25-
date time.Time
26-
baseCommand string
27-
containerId string
23+
config *Config
24+
filePath string
25+
date time.Time
26+
baseCommand string
27+
containerId string
28+
allDatabases bool
2829
)
2930

3031
type Config struct {
@@ -39,6 +40,8 @@ type Config struct {
3940
Name string `yaml:"name"`
4041
Username string `yaml:"username"`
4142
Password string `yaml:"password"`
43+
Host string `yaml:"host"`
44+
Port int `yaml:"port"`
4245
} `yaml:"database"`
4346
}
4447

@@ -47,23 +50,7 @@ type PutRequest struct {
4750
Content string `json:"content"`
4851
}
4952

50-
func init() {
51-
date = time.Now()
52-
53-
var useHelp bool
54-
flag.BoolVar(&useHelp, "help", false, "Show this help menu.")
55-
56-
flag.StringVar(&filePath, "config", "./config.yml", "Select where is located config file.")
57-
58-
flag.StringVar(&containerId, "container", "", "Specific the ID (or name) of the container in which the instance of the database is running, this will avoid the requirement that the command is executed by the postgre user.")
59-
60-
flag.Parse()
61-
62-
if useHelp {
63-
flag.PrintDefaults()
64-
os.Exit(0)
65-
}
66-
53+
func (c *Config) init() {
6754
file, err := filepath.Abs(filePath)
6855
if err != nil {
6956
log.Fatal(err)
@@ -80,12 +67,31 @@ func init() {
8067
}
8168

8269
if config.Database.Name == "" {
70+
if !allDatabases {
71+
log.Fatalf("database name not defined on %s", file)
72+
}
8373
config.Database.Name = "all"
8474
baseCommand = "pg_dumpall"
8575
} else {
8676
baseCommand = "pg_dump"
8777
}
8878

79+
if config.Database.Host == "" {
80+
config.Database.Host = "127.0.0.1"
81+
}
82+
83+
if config.Database.Port == 0 {
84+
config.Database.Port = 5432
85+
}
86+
87+
if config.Database.Username == "" {
88+
log.Fatalf("database username not defined on %s", file)
89+
}
90+
91+
if config.Database.Password == "" {
92+
log.Fatalf("database password not defined on %s", file)
93+
}
94+
8995
if config.S3.Region == "" {
9096
log.Fatalf("s3 region not defined on %s", file)
9197
}
@@ -103,6 +109,27 @@ func init() {
103109
}
104110
}
105111

112+
func init() {
113+
date = time.Now()
114+
115+
var useHelp bool
116+
flag.BoolVar(&useHelp, "help", false, "Show this help menu.")
117+
flag.BoolVar(&allDatabases, "all", false, "If defined will be dumped all the databases (pg_dumpall instead of pg_dump)")
118+
119+
flag.StringVar(&filePath, "config", "./config.yml", "Select where is located config file.")
120+
121+
flag.StringVar(&containerId, "container", "", "Specific the ID (or name) of the container in which the instance of the database is running, this will avoid the requirement that the command is executed by the postgre user.")
122+
123+
flag.Parse()
124+
125+
if useHelp {
126+
flag.PrintDefaults()
127+
os.Exit(0)
128+
}
129+
130+
config.init()
131+
}
132+
106133
func printBanner() {
107134
fmt.Println(`
108135
┌───────────────────────────────────────────────────┐
@@ -127,40 +154,14 @@ func prepareS3Connection() *s3manager.Uploader {
127154
return s3manager.NewUploader(sess)
128155
}
129156

157+
// pg_dump -Z5 -Fc --dbname=postgresql://postgres:postgres@127.0.0.1:5432/hugebot
130158
func buildCommand(destination string) *exec.Cmd {
131159
if containerId == "" {
132-
return exec.Command(baseCommand, "-Z5", "-Fc", config.Database.Name, "-f", destination)
160+
return exec.Command(baseCommand, "-Z5", "-Fc")
161+
} else if allDatabases {
162+
return exec.Command("docker", "exec", containerId, baseCommand, fmt.Sprintf("--dbname=postgresql://%s:%s@%s:%d", config.Database.Username, config.Database.Password, config.Database.Host, config.Database.Port))
133163
} else {
134-
cmdArray := []string{
135-
"docker",
136-
"exec",
137-
"-i",
138-
containerId,
139-
"/bin/bash",
140-
"-c",
141-
}
142-
143-
if config.Database.Password != "" {
144-
cmdArray = append(cmdArray, fmt.Sprintf("'PGPASSWORD=%s", config.Database.Password), baseCommand)
145-
} else {
146-
cmdArray = append(cmdArray, fmt.Sprintf("'%s", baseCommand))
147-
}
148-
149-
if config.Database.Username != "" {
150-
cmdArray = append(cmdArray, "--username", config.Database.Username)
151-
} else {
152-
cmdArray = append(cmdArray, "--username", "postgres")
153-
}
154-
155-
cmdArray = append(cmdArray, "-Z5", "-Fc")
156-
157-
if config.Database.Name != "all" {
158-
cmdArray = append(cmdArray, fmt.Sprintf("%s'", config.Database.Name), ">", destination)
159-
} else {
160-
cmdArray = append(cmdArray, "'", ">", destination)
161-
}
162-
163-
return exec.Command(cmdArray[0], cmdArray[1:]...)
164+
return exec.Command("docker", "exec", containerId, baseCommand, "-Z5", "-Fc", fmt.Sprintf("--dbname=postgresql://%s:%s@%s:%d/%s", config.Database.Username, config.Database.Password, config.Database.Host, config.Database.Port, config.Database.Name))
164165
}
165166
}
166167

@@ -187,26 +188,18 @@ func main() {
187188
destination := fmt.Sprintf("%s/%s", tempDir, fileName)
188189

189190
cmd := buildCommand(destination)
190-
191-
cmd.Stdout = os.Stdout
192191
cmd.Stderr = os.Stderr
193192

194193
log.Printf("Running command %v\n", cmd.Args)
195-
if err := cmd.Run(); err != nil {
196-
log.Fatal(err)
197-
}
198-
199-
log.Printf("backup created successfully on %s.\n", destination)
200-
201-
content, err := os.ReadFile(destination)
194+
out, err := cmd.Output()
202195
if err != nil {
203196
log.Fatal(err)
204197
}
205198

206199
result, err := prepareS3Connection().Upload(&s3manager.UploadInput{
207200
Bucket: aws.String(config.S3.Bucket),
208201
Key: aws.String(fileName),
209-
Body: aws.ReadSeekCloser(bytes.NewReader(content)),
202+
Body: aws.ReadSeekCloser(bytes.NewReader(out)),
210203
})
211204
if err != nil {
212205
log.Fatal(err)

0 commit comments

Comments
 (0)