-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (117 loc) · 3.09 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
package main
import (
"flag"
"fmt"
"os"
"github.com/JCoupalK/Archon/utils"
)
var (
// Long-form flags
Type string
username string
password string
hostname string
db string
port int
outputDir string
// Short-form flags
TypeShort string
usernameShort string
passwordShort string
hostnameShort string
dbShort string
portShort int
outputDirShort string
)
func init() {
// Define flags
flag.StringVar(&Type, "type", "", "Backup type (mysql, mariadb)")
flag.StringVar(&username, "user", "", "Database username")
flag.StringVar(&password, "password", "", "Database password")
flag.StringVar(&hostname, "host", "", "Database host")
flag.IntVar(&port, "port", 0, "Database port")
flag.StringVar(&db, "database", "", "Database name")
flag.StringVar(&outputDir, "outputdir", "", "Output directory for the backup files")
// Short-form flags
flag.StringVar(&TypeShort, "t", "", "Backup type (mysql, mariadb)")
flag.StringVar(&usernameShort, "u", "", "Database username")
flag.StringVar(&passwordShort, "p", "", "Database password")
flag.StringVar(&hostnameShort, "h", "", "Database host")
flag.IntVar(&portShort, "P", 0, "Database port")
flag.StringVar(&dbShort, "d", "", "Database name")
flag.StringVar(&outputDirShort, "od", "", "Output directory for the backup files")
// Override the default flag.Usage
flag.Usage = utils.Usage
flag.Parse()
// Apply short-form flags if set
if TypeShort != "" {
Type = TypeShort
}
if usernameShort != "" {
username = usernameShort
}
if passwordShort != "" {
password = passwordShort
}
if hostnameShort != "" {
hostname = hostnameShort
}
if portShort != 0 {
port = portShort
}
if dbShort != "" {
db = dbShort
}
if outputDirShort != "" {
outputDir = outputDirShort
}
}
func main() {
switch Type {
case "mysql", "mariadb":
if username == "" || password == "" || hostname == "" || port == 0 || db == "" {
fmt.Fprintln(os.Stderr, "Error: Required flags are missing.")
utils.Usage()
}
if outputDir == "" {
outputDir, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
utils.BackupMySQL(username, password, hostname, db, outputDir, port)
} else {
pwd, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
outputDir := fmt.Sprintf("%s/%s", pwd, outputDir)
utils.BackupMySQL(username, password, hostname, db, outputDir, port)
}
case "postgresql", "postgres":
if username == "" || password == "" || hostname == "" || port == 0 || db == "" {
fmt.Fprintln(os.Stderr, "Error: Required flags are missing.")
utils.Usage()
}
if outputDir == "" {
outputDir, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
utils.BackupPostgreSQL(username, password, hostname, db, outputDir, port)
} else {
pwd, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
outputDir := fmt.Sprintf("%s/%s", pwd, outputDir)
utils.BackupPostgreSQL(username, password, hostname, db, outputDir, port)
}
default:
fmt.Fprintln(os.Stderr, "Error: Required flags are missing.")
utils.Usage()
}
}