-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
160 lines (134 loc) · 3.44 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"flag"
_ "github.com/lib/pq"
"github.com/spf13/viper"
"github.com/theckman/yacspin"
"io/fs"
"log"
"os"
"path"
"path/filepath"
"sort"
)
var (
local db
remote db
pathToDump string
pathToRestore string
pathToBackups string
shouldBackup bool
shouldRestore bool
backupOptions []string
restoreOptions []string
shouldCleanUp bool
maxBackups int
spinner *yacspin.Spinner
restoreFile string
)
func init() {
configName := flag.String("config", "main", "custom config name")
flag.Parse()
ex, err := os.Executable()
if err != nil {
log.Fatal(err)
}
dir := path.Dir(ex)
log.Print(dir)
viper.SetConfigName(*configName)
viper.AddConfigPath(dir)
viper.AddConfigPath("./")
err = viper.ReadInConfig()
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Fatalf("Could not find config file. Please make sure it is in the same directory as the executable and named %s.yaml.\n %s", *configName, err)
} else {
log.Fatalln("Failed to read config file.", err)
}
}
local = db{
host: viper.GetString("localDb.host"),
port: viper.GetInt("localDb.port"),
user: viper.GetString("localDb.user"),
password: viper.GetString("localDb.password"),
name: viper.GetString("localDb.name"),
}
remote = db{
host: viper.GetString("remoteDb.host"),
port: viper.GetInt("remoteDb.port"),
user: viper.GetString("remoteDb.user"),
password: viper.GetString("remoteDb.password"),
name: viper.GetString("remoteDb.name"),
}
findExecutables()
pathToBackups = viper.GetString("directories.backups")
if pathToBackups == "" {
pathToBackups = dir + "backups/"
}
if pathToBackups[len(pathToBackups)-1:] != "/" {
pathToBackups = pathToBackups + "/"
}
backupOptions = viper.GetStringSlice("backupOptions")
restoreOptions = viper.GetStringSlice("restoreOptions")
viper.SetDefault("shouldRestore", false)
viper.SetDefault("shouldBackup", true)
viper.SetDefault("shouldCleanUp", true)
viper.SetDefault("maxBackups", 10)
shouldRestore = viper.GetBool("shouldRestore")
shouldBackup = viper.GetBool("shouldBackup")
args := flag.Args()
for _, arg := range args {
switch arg {
case "r", "restore":
shouldBackup = false
case "b", "backup":
shouldRestore = false
}
}
shouldCleanUp = viper.GetBool("shouldCleanUp")
maxBackups = viper.GetInt("maxBackups")
spinner, err = yacspin.New(cfg)
if err != nil {
log.Println("Failed to start the spinner")
log.Println(err)
}
}
func main() {
if shouldBackup {
restoreFile = backup()
}
if shouldRestore {
if restoreFile == "" {
log.Println("No backup file explicitly given, searching for most recent backup")
files, err := os.ReadDir(pathToBackups)
if err != nil {
log.Fatal(err)
}
sort.Slice(files, func(i, j int) bool {
return files[i].Name() > files[j].Name()
})
restoreFile = pathToBackups + files[0].Name()
log.Printf("Latest backup found was: %s", files[0].Name())
}
restore(restoreFile)
}
if shouldCleanUp {
cleanUp()
}
}
func findExecutables() {
err := filepath.WalkDir(viper.GetString("directories.executables"), func(path string, d fs.DirEntry, err error) error {
switch d.Name() {
case "pg_dump":
pathToDump = path
case "pg_restore":
pathToRestore = path
default:
}
return nil
})
if err != nil || pathToRestore == "" || pathToDump == "" {
log.Println("Failed to find the postgres executables (pg_dump and/or pg_restore)")
log.Fatal(err)
}
}