7
7
package pkg
8
8
9
9
import (
10
+ "fmt"
10
11
"github.com/jkaninda/mysql-bkup/utils"
11
12
"github.com/spf13/cobra"
12
13
"os"
14
+ "strconv"
13
15
)
14
16
15
17
type Config struct {
@@ -29,7 +31,10 @@ type targetDbConfig struct {
29
31
targetDbPassword string
30
32
targetDbName string
31
33
}
32
-
34
+ type TgConfig struct {
35
+ Token string
36
+ ChatId string
37
+ }
33
38
type BackupConfig struct {
34
39
backupFileName string
35
40
backupRetention int
@@ -41,14 +46,6 @@ type BackupConfig struct {
41
46
storage string
42
47
cronExpression string
43
48
}
44
- type RestoreConfig struct {
45
- s3Path string
46
- remotePath string
47
- storage string
48
- file string
49
- bucket string
50
- gpqPassphrase string
51
- }
52
49
type FTPConfig struct {
53
50
host string
54
51
user string
@@ -57,6 +54,24 @@ type FTPConfig struct {
57
54
remotePath string
58
55
}
59
56
57
+ // SSHConfig holds the SSH connection details
58
+ type SSHConfig struct {
59
+ user string
60
+ password string
61
+ hostName string
62
+ port string
63
+ identifyFile string
64
+ }
65
+ type AWSConfig struct {
66
+ endpoint string
67
+ bucket string
68
+ accessKey string
69
+ secretKey string
70
+ region string
71
+ disableSsl bool
72
+ forcePathStyle bool
73
+ }
74
+
60
75
func initDbConfig (cmd * cobra.Command ) * dbConfig {
61
76
//Set env
62
77
utils .GetEnv (cmd , "dbname" , "DB_NAME" )
@@ -74,14 +89,71 @@ func initDbConfig(cmd *cobra.Command) *dbConfig {
74
89
}
75
90
return & dConf
76
91
}
92
+
93
+ // loadSSHConfig loads the SSH configuration from environment variables
94
+ func loadSSHConfig () (* SSHConfig , error ) {
95
+ utils .GetEnvVariable ("SSH_HOST" , "SSH_HOST_NAME" )
96
+ sshVars := []string {"SSH_USER" , "SSH_HOST" , "SSH_PORT" , "REMOTE_PATH" }
97
+ err := utils .CheckEnvVars (sshVars )
98
+ if err != nil {
99
+ return nil , fmt .Errorf ("error missing environment variables: %w" , err )
100
+ }
101
+
102
+ return & SSHConfig {
103
+ user : os .Getenv ("SSH_USER" ),
104
+ password : os .Getenv ("SSH_PASSWORD" ),
105
+ hostName : os .Getenv ("SSH_HOST" ),
106
+ port : os .Getenv ("SSH_PORT" ),
107
+ identifyFile : os .Getenv ("SSH_IDENTIFY_FILE" ),
108
+ }, nil
109
+ }
110
+ func initFtpConfig () * FTPConfig {
111
+ //Initialize data configs
112
+ fConfig := FTPConfig {}
113
+ fConfig .host = utils .GetEnvVariable ("FTP_HOST" , "FTP_HOST_NAME" )
114
+ fConfig .user = os .Getenv ("FTP_USER" )
115
+ fConfig .password = os .Getenv ("FTP_PASSWORD" )
116
+ fConfig .port = os .Getenv ("FTP_PORT" )
117
+ fConfig .remotePath = os .Getenv ("REMOTE_PATH" )
118
+ err := utils .CheckEnvVars (ftpVars )
119
+ if err != nil {
120
+ utils .Error ("Please make sure all required environment variables for FTP are set" )
121
+ utils .Fatal ("Error missing environment variables: %s" , err )
122
+ }
123
+ return & fConfig
124
+ }
125
+ func initAWSConfig () * AWSConfig {
126
+ //Initialize AWS configs
127
+ aConfig := AWSConfig {}
128
+ aConfig .endpoint = utils .GetEnvVariable ("AWS_S3_ENDPOINT" , "S3_ENDPOINT" )
129
+ aConfig .accessKey = utils .GetEnvVariable ("AWS_ACCESS_KEY" , "ACCESS_KEY" )
130
+ aConfig .secretKey = utils .GetEnvVariable ("AWS_SECRET_KEY" , "SECRET_KEY" )
131
+ aConfig .bucket = utils .GetEnvVariable ("AWS_S3_BUCKET_NAME" , "BUCKET_NAME" )
132
+ aConfig .region = os .Getenv ("AWS_REGION" )
133
+ disableSsl , err := strconv .ParseBool (os .Getenv ("AWS_DISABLE_SSL" ))
134
+ if err != nil {
135
+ utils .Fatal ("Unable to parse AWS_DISABLE_SSL env var: %s" , err )
136
+ }
137
+ forcePathStyle , err := strconv .ParseBool (os .Getenv ("AWS_FORCE_PATH_STYLE" ))
138
+ if err != nil {
139
+ utils .Fatal ("Unable to parse AWS_FORCE_PATH_STYLE env var: %s" , err )
140
+ }
141
+ aConfig .disableSsl = disableSsl
142
+ aConfig .forcePathStyle = forcePathStyle
143
+ err = utils .CheckEnvVars (awsVars )
144
+ if err != nil {
145
+ utils .Error ("Please make sure all required environment variables for AWS S3 are set" )
146
+ utils .Fatal ("Error checking environment variables: %s" , err )
147
+ }
148
+ return & aConfig
149
+ }
77
150
func initBackupConfig (cmd * cobra.Command ) * BackupConfig {
78
151
utils .SetEnv ("STORAGE_PATH" , storagePath )
79
152
utils .GetEnv (cmd , "cron-expression" , "BACKUP_CRON_EXPRESSION" )
80
153
utils .GetEnv (cmd , "period" , "BACKUP_CRON_EXPRESSION" )
81
154
utils .GetEnv (cmd , "path" , "REMOTE_PATH" )
82
- remotePath := utils .GetEnvVariable ("REMOTE_PATH" , "SSH_REMOTE_PATH" )
83
-
84
155
//Get flag value and set env
156
+ remotePath := utils .GetEnvVariable ("REMOTE_PATH" , "SSH_REMOTE_PATH" )
85
157
storage = utils .GetEnv (cmd , "storage" , "STORAGE" )
86
158
backupRetention , _ := cmd .Flags ().GetInt ("keep-last" )
87
159
prune , _ := cmd .Flags ().GetBool ("prune" )
@@ -94,6 +166,7 @@ func initBackupConfig(cmd *cobra.Command) *BackupConfig {
94
166
if passphrase != "" {
95
167
encryption = true
96
168
}
169
+
97
170
//Initialize backup configs
98
171
config := BackupConfig {}
99
172
config .backupRetention = backupRetention
@@ -106,16 +179,25 @@ func initBackupConfig(cmd *cobra.Command) *BackupConfig {
106
179
config .cronExpression = cronExpression
107
180
return & config
108
181
}
182
+
183
+ type RestoreConfig struct {
184
+ s3Path string
185
+ remotePath string
186
+ storage string
187
+ file string
188
+ bucket string
189
+ gpqPassphrase string
190
+ }
191
+
109
192
func initRestoreConfig (cmd * cobra.Command ) * RestoreConfig {
110
193
utils .SetEnv ("STORAGE_PATH" , storagePath )
111
194
utils .GetEnv (cmd , "path" , "REMOTE_PATH" )
112
- remotePath := utils .GetEnvVariable ("REMOTE_PATH" , "SSH_REMOTE_PATH" )
113
195
114
196
//Get flag value and set env
115
197
s3Path := utils .GetEnv (cmd , "path" , "AWS_S3_PATH" )
198
+ remotePath := utils .GetEnvVariable ("REMOTE_PATH" , "SSH_REMOTE_PATH" )
116
199
storage = utils .GetEnv (cmd , "storage" , "STORAGE" )
117
200
file = utils .GetEnv (cmd , "file" , "FILE_NAME" )
118
- _ , _ = cmd .Flags ().GetString ("mode" )
119
201
bucket := utils .GetEnvVariable ("AWS_S3_BUCKET_NAME" , "BUCKET_NAME" )
120
202
gpqPassphrase := os .Getenv ("GPG_PASSPHRASE" )
121
203
//Initialize restore configs
@@ -144,18 +226,3 @@ func initTargetDbConfig() *targetDbConfig {
144
226
}
145
227
return & tdbConfig
146
228
}
147
- func initFtpConfig () * FTPConfig {
148
- //Initialize backup configs
149
- fConfig := FTPConfig {}
150
- fConfig .host = os .Getenv ("FTP_HOST_NAME" )
151
- fConfig .user = os .Getenv ("FTP_USER" )
152
- fConfig .password = os .Getenv ("FTP_PASSWORD" )
153
- fConfig .port = os .Getenv ("FTP_PORT" )
154
- fConfig .remotePath = os .Getenv ("REMOTE_PATH" )
155
- err := utils .CheckEnvVars (ftpVars )
156
- if err != nil {
157
- utils .Error ("Please make sure all required environment variables for FTP are set" )
158
- utils .Fatal ("Error checking environment variables: %s" , err )
159
- }
160
- return & fConfig
161
- }
0 commit comments