@@ -18,11 +18,7 @@ package pkg
18
18
19
19
import (
20
20
"context"
21
- "fmt"
22
- "io/ioutil"
23
- "os"
24
21
"path/filepath"
25
- "strings"
26
22
27
23
api_v1beta1 "stash.appscode.dev/apimachinery/apis/stash/v1beta1"
28
24
stash "stash.appscode.dev/apimachinery/client/clientset/versioned"
@@ -68,7 +64,7 @@ func NewCmdBackup() *cobra.Command {
68
64
Short : "Takes a backup of MariaDB DB" ,
69
65
DisableAutoGenTag : true ,
70
66
RunE : func (cmd * cobra.Command , args []string ) error {
71
- flags .EnsureRequiredFlags (cmd , "appbinding" , "provider" , "secret-dir " )
67
+ flags .EnsureRequiredFlags (cmd , "appbinding" , "provider" , "storage- secret-name" , "storage-secret-namespace " )
72
68
73
69
// prepare client
74
70
config , err := clientcmd .BuildConfigFromFlags (masterURL , kubeconfigPath )
@@ -130,13 +126,14 @@ func NewCmdBackup() *cobra.Command {
130
126
cmd .Flags ().StringVar (& opt .namespace , "namespace" , "default" , "Namespace of Backup/Restore Session" )
131
127
cmd .Flags ().StringVar (& opt .backupSessionName , "backupsession" , opt .backupSessionName , "Name of the Backup Session" )
132
128
cmd .Flags ().StringVar (& opt .appBindingName , "appbinding" , opt .appBindingName , "Name of the app binding" )
129
+ cmd .Flags ().StringVar (& opt .storageSecret .Name , "storage-secret-name" , opt .storageSecret .Name , "Name of the storage secret" )
130
+ cmd .Flags ().StringVar (& opt .storageSecret .Namespace , "storage-secret-namespace" , opt .storageSecret .Namespace , "Namespace of the storage secret" )
133
131
134
132
cmd .Flags ().StringVar (& opt .setupOptions .Provider , "provider" , opt .setupOptions .Provider , "Backend provider (i.e. gcs, s3, azure etc)" )
135
133
cmd .Flags ().StringVar (& opt .setupOptions .Bucket , "bucket" , opt .setupOptions .Bucket , "Name of the cloud bucket/container (keep empty for local backend)" )
136
134
cmd .Flags ().StringVar (& opt .setupOptions .Endpoint , "endpoint" , opt .setupOptions .Endpoint , "Endpoint for s3/s3 compatible backend or REST backend URL" )
137
135
cmd .Flags ().StringVar (& opt .setupOptions .Region , "region" , opt .setupOptions .Region , "Region for s3/s3 compatible backend" )
138
136
cmd .Flags ().StringVar (& opt .setupOptions .Path , "path" , opt .setupOptions .Path , "Directory inside the bucket where backup will be stored" )
139
- cmd .Flags ().StringVar (& opt .setupOptions .SecretDir , "secret-dir" , opt .setupOptions .SecretDir , "Directory where storage secret has been mounted" )
140
137
cmd .Flags ().StringVar (& opt .setupOptions .ScratchDir , "scratch-dir" , opt .setupOptions .ScratchDir , "Temporary directory" )
141
138
cmd .Flags ().BoolVar (& opt .setupOptions .EnableCache , "enable-cache" , opt .setupOptions .EnableCache , "Specify whether to enable caching for restic" )
142
139
cmd .Flags ().Int64Var (& opt .setupOptions .MaxConnections , "max-connections" , opt .setupOptions .MaxConnections , "Specify maximum concurrent connections for GCS, Azure and B2 backend" )
@@ -159,6 +156,11 @@ func NewCmdBackup() *cobra.Command {
159
156
}
160
157
161
158
func (opt * mariadbOptions ) backupMariaDB (targetRef api_v1beta1.TargetRef ) (* restic.BackupOutput , error ) {
159
+ var err error
160
+ opt .setupOptions .StorageSecret , err = opt .kubeClient .CoreV1 ().Secrets (opt .storageSecret .Namespace ).Get (context .TODO (), opt .storageSecret .Name , metav1.GetOptions {})
161
+ if err != nil {
162
+ return nil , err
163
+ }
162
164
// if any pre-backup actions has been assigned to it, execute them
163
165
actionOptions := api_util.ActionOptions {
164
166
StashClient : opt .stashClient ,
@@ -167,7 +169,7 @@ func (opt *mariadbOptions) backupMariaDB(targetRef api_v1beta1.TargetRef) (*rest
167
169
BackupSessionName : opt .backupSessionName ,
168
170
Namespace : opt .namespace ,
169
171
}
170
- err : = api_util .ExecutePreBackupActions (actionOptions )
172
+ err = api_util .ExecutePreBackupActions (actionOptions )
171
173
if err != nil {
172
174
return nil , err
173
175
}
@@ -186,68 +188,40 @@ func (opt *mariadbOptions) backupMariaDB(targetRef api_v1beta1.TargetRef) (*rest
186
188
return nil , err
187
189
}
188
190
189
- // get app binding
190
191
appBinding , err := opt .catalogClient .AppcatalogV1alpha1 ().AppBindings (opt .namespace ).Get (context .TODO (), opt .appBindingName , metav1.GetOptions {})
191
192
if err != nil {
192
193
return nil , err
193
194
}
194
- // get secret
195
- appBindingSecret , err := opt .kubeClient .CoreV1 ().Secrets (opt .namespace ).Get (context .TODO (), appBinding .Spec .Secret .Name , metav1.GetOptions {})
195
+
196
+ session := opt .newSessionWrapper (MariaDBDumpCMD )
197
+
198
+ err = session .setDatabaseCredentials (opt .kubeClient , appBinding )
196
199
if err != nil {
197
200
return nil , err
198
201
}
199
202
200
- // transform secret
201
- err = appBinding .TransformSecret (opt .kubeClient , appBindingSecret .Data )
203
+ err = session .setDatabaseConnectionParameters (appBinding )
202
204
if err != nil {
203
205
return nil , err
204
206
}
205
207
206
- // init restic wrapper
207
- resticWrapper , err := restic .NewResticWrapper (opt .setupOptions )
208
+ err = session .setTLSParameters (appBinding , opt .setupOptions .ScratchDir )
208
209
if err != nil {
209
210
return nil , err
210
211
}
211
212
212
- // set env for mariadbdump
213
- resticWrapper .SetEnv (EnvMariaDBPassword , string (appBindingSecret .Data [MariaDBPassword ]))
214
- // setup backup command
215
- backupCmd := restic.Command {
216
- Name : MariaDBDumpCMD ,
217
- Args : []interface {}{
218
- "-u" , string (appBindingSecret .Data [MariaDBUser ]),
219
- "-h" , appBinding .Spec .ClientConfig .Service .Name ,
220
- },
221
- }
222
- // if port is specified, append port in the arguments
223
- if appBinding .Spec .ClientConfig .Service .Port != 0 {
224
- backupCmd .Args = append (backupCmd .Args , fmt .Sprintf ("--port=%d" , appBinding .Spec .ClientConfig .Service .Port ))
225
- }
226
- for _ , arg := range strings .Fields (opt .myArgs ) {
227
- backupCmd .Args = append (backupCmd .Args , arg )
213
+ err = session .waitForDBReady ()
214
+ if err != nil {
215
+ return nil , err
228
216
}
229
217
230
- // if ssl enabled, add ca.crt in the arguments
231
- if appBinding .Spec .ClientConfig .CABundle != nil {
232
- if err := ioutil .WriteFile (filepath .Join (opt .setupOptions .ScratchDir , MariaDBTLSRootCA ), appBinding .Spec .ClientConfig .CABundle , os .ModePerm ); err != nil {
233
- return nil , err
234
- }
235
- tlsCreds := []interface {}{
236
- fmt .Sprintf ("--ssl-ca=%v" , filepath .Join (opt .setupOptions .ScratchDir , MariaDBTLSRootCA )),
237
- }
238
-
239
- backupCmd .Args = append (backupCmd .Args , tlsCreds ... )
240
- }
218
+ session .setUserArgs (opt .myArgs )
241
219
242
- // wait for DB ready
243
- err = opt .waitForDBReady (appBinding , appBindingSecret )
220
+ // append the backup command to the pipeline
221
+ opt .backupOptions .StdinPipeCommands = append (opt .backupOptions .StdinPipeCommands , * session .cmd )
222
+ resticWrapper , err := restic .NewResticWrapperFromShell (opt .setupOptions , session .sh )
244
223
if err != nil {
245
224
return nil , err
246
225
}
247
-
248
- // append the backup command to the pipeline
249
- opt .backupOptions .StdinPipeCommands = append (opt .backupOptions .StdinPipeCommands , backupCmd )
250
-
251
- // Run backup
252
226
return resticWrapper .RunBackup (opt .backupOptions , targetRef )
253
227
}
0 commit comments