Skip to content

Commit e29d03e

Browse files
1gtmhmsayem
andauthored
Add support for backup and restore from different namespace (#559) (#561)
/cherry-pick Signed-off-by: hmsayem <hmsayem@appscode.com> Co-authored-by: Hossain Mahmud <hmsayem@appscode.com>
1 parent 9364b2b commit e29d03e

File tree

126 files changed

+27287
-11309
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+27287
-11309
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ require (
1111
gomodules.xyz/go-sh v0.1.0
1212
gomodules.xyz/logs v0.0.6
1313
gomodules.xyz/x v0.0.8
14-
k8s.io/api v0.21.1
1514
k8s.io/apimachinery v0.21.1
1615
k8s.io/client-go v0.21.1
1716
k8s.io/klog/v2 v2.8.0
18-
kmodules.xyz/custom-resources v0.0.0-20211122142737-3bf3dbd8ac52
17+
kmodules.xyz/client-go v0.0.0-20220108081101-27afc2ac4ebe
18+
kmodules.xyz/custom-resources v0.0.0-20220104123914-3c036dd7c1cd
1919
kmodules.xyz/offshoot-api v0.0.0-20211103060642-3e217667cf41
20-
stash.appscode.dev/apimachinery v0.17.0
20+
stash.appscode.dev/apimachinery v0.17.1-0.20220113052814-7da4b19c88a3
2121
)
2222

2323
replace bitbucket.org/ww/goautoneg => gomodules.xyz/goautoneg v0.0.0-20120707110453-a547fc61f48d

go.sum

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,7 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
550550
github.com/quobyte/api v0.1.8/go.mod h1:jL7lIHrmqQ7yh05OJ+eEEdHr0u/kmT1Ff9iHd+4H6VI=
551551
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
552552
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
553-
github.com/robfig/cron v1.1.0 h1:jk4/Hud3TTdcrJgUOBgsqrZBarcxl6ADIjSC2iniwLY=
554553
github.com/robfig/cron v1.1.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
555-
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
556-
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
557554
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
558555
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
559556
github.com/rubiojr/go-vhd v0.0.0-20200706105327-02e210299021/go.mod h1:DM5xW0nvfNNm2uytzsvhI3OnX8uzaRAg8UX/CnDqbto=
@@ -848,8 +845,9 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
848845
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
849846
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
850847
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
851-
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
852848
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
849+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
850+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
853851
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
854852
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
855853
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
@@ -1007,6 +1005,7 @@ k8s.io/cloud-provider v0.21.1/go.mod h1:GgiRu7hOsZh3+VqMMbfLJJS9ZZM9A8k/YiZG8zkW
10071005
k8s.io/cluster-bootstrap v0.21.1/go.mod h1:izdXmPTfqI9gkjQLf9PQ1Y9/DSqG54zU2UByEzgdajs=
10081006
k8s.io/code-generator v0.18.2/go.mod h1:+UHX5rSbxmR8kzS+FAv7um6dtYrZokQvjHpDSYRVkTc=
10091007
k8s.io/code-generator v0.21.1/go.mod h1:hUlps5+9QaTrKx+jiM4rmq7YmH8wPOIko64uZCHDh6Q=
1008+
k8s.io/component-base v0.21.1 h1:iLpj2btXbR326s/xNQWmPNGu0gaYSjzn7IN/5i28nQw=
10101009
k8s.io/component-base v0.21.1/go.mod h1:NgzFZ2qu4m1juby4TnrmpR8adRk6ka62YdH5DkIIyKA=
10111010
k8s.io/component-helpers v0.21.1/go.mod h1:FtC1flbiQlosHQrLrRUulnKxE4ajgWCGy/67fT2GRlQ=
10121011
k8s.io/controller-manager v0.21.1/go.mod h1:8ugs8DCcHqybiwdVERhnnyGoS5Ksq/ea1p2B0CosHyc=
@@ -1041,20 +1040,21 @@ k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/
10411040
kmodules.xyz/client-go v0.0.0-20210617233340-13d22e91512b/go.mod h1:A6GAK6xP5zBuWK6A/vUkkjKzcuywkms7fIxRf5wblO4=
10421041
kmodules.xyz/client-go v0.0.0-20211013093146-1fbfd52e78c9/go.mod h1:0gkPeALtYjB27OHt4rd6+ZmMgoVTHVLtEJQeU23/gtA=
10431042
kmodules.xyz/client-go v0.0.0-20211107190155-5bb4090d2728/go.mod h1:ENUu8pPK19xzBkVpAJHoGCI2QRvb1SqffWRt0K2sV5I=
1044-
kmodules.xyz/client-go v0.0.0-20211122091731-6c471b24a4ea h1:z5Li57oxum0018ryWpI5w5HYVFgI2S2cVj27R76IRnU=
1045-
kmodules.xyz/client-go v0.0.0-20211122091731-6c471b24a4ea/go.mod h1:ENUu8pPK19xzBkVpAJHoGCI2QRvb1SqffWRt0K2sV5I=
1043+
kmodules.xyz/client-go v0.0.0-20220104114408-2a3a05dbe89f/go.mod h1:xxl1ve1Obe4xaW+XjXsNHyLTni4QPIvHn9TfnYEoQRo=
1044+
kmodules.xyz/client-go v0.0.0-20220108081101-27afc2ac4ebe h1:EZE/eC9UF5/wcdHiinzQDVshOceIP9uRxT7qM4BnUQg=
1045+
kmodules.xyz/client-go v0.0.0-20220108081101-27afc2ac4ebe/go.mod h1:xxl1ve1Obe4xaW+XjXsNHyLTni4QPIvHn9TfnYEoQRo=
10461046
kmodules.xyz/constants v0.0.0-20210218100002-2c304bfda278/go.mod h1:DbiFk1bJ1KEO94t1SlAn7tzc+Zz95rSXgyUKa2nzPmY=
10471047
kmodules.xyz/crd-schema-fuzz v0.0.0-20210618002152-fae23aef5fb4/go.mod h1:IIkUctlfoptoci0BOrsUf8ya+MOG5uaeh1PE4uzaIbA=
1048-
kmodules.xyz/custom-resources v0.0.0-20211122142737-3bf3dbd8ac52 h1:UWVpU7y5znTUusU+JhPB+ojh26f6K2v8sNb37U1DolQ=
1049-
kmodules.xyz/custom-resources v0.0.0-20211122142737-3bf3dbd8ac52/go.mod h1:yHLFe4wVYxepTnN00CFUf29xH+jEHDokq6d2fbp9pks=
1048+
kmodules.xyz/custom-resources v0.0.0-20220104123914-3c036dd7c1cd h1:EMcK5eA42CO9Cn290VOy2WIt2YSEbQItlMq8lU+831M=
1049+
kmodules.xyz/custom-resources v0.0.0-20220104123914-3c036dd7c1cd/go.mod h1:/XjDeILFV2wBota5kHo21DMvOt08nSAk1vm6buCuwt4=
10501050
kmodules.xyz/objectstore-api v0.0.0-20211116180107-8720be0c9bf7 h1:JIAEFjN3GDhLEG1Fh1zYpy/QFyyN337mJTM+ODEGosg=
10511051
kmodules.xyz/objectstore-api v0.0.0-20211116180107-8720be0c9bf7/go.mod h1:IICnDdPFOEeGXdaPVHOGYfdwD1cyh/p1I/TWMkyNTIE=
10521052
kmodules.xyz/offshoot-api v0.0.0-20210829122105-6f4d481b0c61/go.mod h1:3LECbAL3FgbyK80NP3V3Pmiuo/a3hFWg/PR6SPFhTns=
10531053
kmodules.xyz/offshoot-api v0.0.0-20211103060642-3e217667cf41 h1:GCDyb1fbgCHUNuFk9MecSFvzMq/WCeGk8pDEtqivziA=
10541054
kmodules.xyz/offshoot-api v0.0.0-20211103060642-3e217667cf41/go.mod h1:sJYyxf84ZvbVz4SivxMgSelGRYn19wOLUtObiEncCxk=
10551055
kmodules.xyz/prober v0.0.0-20210618020259-5836fb959027 h1:rtSPsruEkoBOLcbJkxMQjnPafpv8/vJnfWlTIj9yRBg=
10561056
kmodules.xyz/prober v0.0.0-20210618020259-5836fb959027/go.mod h1:H4NcvS1RQxeXtQONALbU6r9OT3Xa7cMhfamX/k9t9wo=
1057-
kmodules.xyz/resource-metrics v0.0.5/go.mod h1:6Dv63HDgp83DhA+lZNB7GIQR6PLjNrYW6ghQKioQzII=
1057+
kmodules.xyz/resource-metrics v0.0.6/go.mod h1:M7rWuo2qh3BpHhogiEVPnvGY9Xx4Pfygqn1Rex8YbgM=
10581058
modernc.org/cc v1.0.0/go.mod h1:1Sk4//wdnYJiUIxnW8ddKpaOJCF37yAdqYnkxUpaYxw=
10591059
modernc.org/golex v1.0.0/go.mod h1:b/QX9oBD/LhixY6NDh+IdGv17hgB+51fET1i2kPSmvk=
10601060
modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k=
@@ -1079,5 +1079,5 @@ sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
10791079
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
10801080
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
10811081
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=
1082-
stash.appscode.dev/apimachinery v0.17.0 h1:v0razjOko1K0npOwRzvtymdMByL/cWSNQBgaIq7KaoU=
1083-
stash.appscode.dev/apimachinery v0.17.0/go.mod h1:hmxBy6Ei6RjBLgXw4A1hE4eyEgsa43H2LCs1yDfI3GM=
1082+
stash.appscode.dev/apimachinery v0.17.1-0.20220113052814-7da4b19c88a3 h1:jWOAWMKDc15vVPqq4pToYyRkitlGh6Y2+1V5QgGZsYY=
1083+
stash.appscode.dev/apimachinery v0.17.1-0.20220113052814-7da4b19c88a3/go.mod h1:BGM/ztGKtXZrtz/voZzRqop8KbbZ+pFI4YeSLVb2MB0=

pkg/backup.go

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ package pkg
1818

1919
import (
2020
"context"
21-
"fmt"
22-
"io/ioutil"
23-
"os"
2421
"path/filepath"
25-
"strings"
2622

2723
api_v1beta1 "stash.appscode.dev/apimachinery/apis/stash/v1beta1"
2824
stash "stash.appscode.dev/apimachinery/client/clientset/versioned"
@@ -67,7 +63,7 @@ func NewCmdBackup() *cobra.Command {
6763
Short: "Takes a backup of MySQL DB",
6864
DisableAutoGenTag: true,
6965
RunE: func(cmd *cobra.Command, args []string) error {
70-
flags.EnsureRequiredFlags(cmd, "appbinding", "provider", "secret-dir")
66+
flags.EnsureRequiredFlags(cmd, "appbinding", "provider", "storage-secret-name", "storage-secret-namespace")
7167

7268
// prepare client
7369
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
@@ -135,10 +131,11 @@ func NewCmdBackup() *cobra.Command {
135131
cmd.Flags().StringVar(&opt.setupOptions.Endpoint, "endpoint", opt.setupOptions.Endpoint, "Endpoint for s3/s3 compatible backend or REST backend URL")
136132
cmd.Flags().StringVar(&opt.setupOptions.Region, "region", opt.setupOptions.Region, "Region for s3/s3 compatible backend")
137133
cmd.Flags().StringVar(&opt.setupOptions.Path, "path", opt.setupOptions.Path, "Directory inside the bucket where backup will be stored")
138-
cmd.Flags().StringVar(&opt.setupOptions.SecretDir, "secret-dir", opt.setupOptions.SecretDir, "Directory where storage secret has been mounted")
139134
cmd.Flags().StringVar(&opt.setupOptions.ScratchDir, "scratch-dir", opt.setupOptions.ScratchDir, "Temporary directory")
140135
cmd.Flags().BoolVar(&opt.setupOptions.EnableCache, "enable-cache", opt.setupOptions.EnableCache, "Specify whether to enable caching for restic")
141136
cmd.Flags().Int64Var(&opt.setupOptions.MaxConnections, "max-connections", opt.setupOptions.MaxConnections, "Specify maximum concurrent connections for GCS, Azure and B2 backend")
137+
cmd.Flags().StringVar(&opt.storageSecret.Name, "storage-secret-name", opt.storageSecret.Name, "Name of the storage secret")
138+
cmd.Flags().StringVar(&opt.storageSecret.Namespace, "storage-secret-namespace", opt.storageSecret.Namespace, "Namespace of the storage secret")
142139

143140
cmd.Flags().StringVar(&opt.backupOptions.Host, "hostname", opt.backupOptions.Host, "Name of the host machine")
144141

@@ -158,6 +155,12 @@ func NewCmdBackup() *cobra.Command {
158155
}
159156

160157
func (opt *mysqlOptions) backupMySQL(targetRef api_v1beta1.TargetRef) (*restic.BackupOutput, error) {
158+
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+
}
161164
// if any pre-backup actions has been assigned to it, execute them
162165
actionOptions := api_util.ActionOptions{
163166
StashClient: opt.stashClient,
@@ -166,15 +169,18 @@ func (opt *mysqlOptions) backupMySQL(targetRef api_v1beta1.TargetRef) (*restic.B
166169
BackupSessionName: opt.backupSessionName,
167170
Namespace: opt.namespace,
168171
}
169-
err := api_util.ExecutePreBackupActions(actionOptions)
172+
173+
err = api_util.ExecutePreBackupActions(actionOptions)
170174
if err != nil {
171175
return nil, err
172176
}
177+
173178
// wait until the backend repository has been initialized.
174179
err = api_util.WaitForBackendRepository(actionOptions)
175180
if err != nil {
176181
return nil, err
177182
}
183+
178184
// apply nice, ionice settings from env
179185
opt.setupOptions.Nice, err = v1.NiceSettingsFromEnv()
180186
if err != nil {
@@ -185,68 +191,41 @@ func (opt *mysqlOptions) backupMySQL(targetRef api_v1beta1.TargetRef) (*restic.B
185191
return nil, err
186192
}
187193

188-
// get app binding
189194
appBinding, err := opt.catalogClient.AppcatalogV1alpha1().AppBindings(opt.namespace).Get(context.TODO(), opt.appBindingName, metav1.GetOptions{})
190195
if err != nil {
191196
return nil, err
192197
}
193-
// get secret
194-
appBindingSecret, err := opt.kubeClient.CoreV1().Secrets(opt.namespace).Get(context.TODO(), appBinding.Spec.Secret.Name, metav1.GetOptions{})
198+
199+
session := opt.newSessionWrapper(MySqlDumpCMD)
200+
201+
err = session.setDatabaseCredentials(opt.kubeClient, appBinding)
195202
if err != nil {
196203
return nil, err
197204
}
198205

199-
// transform secret
200-
err = appBinding.TransformSecret(opt.kubeClient, appBindingSecret.Data)
206+
err = session.setDatabaseConnectionParameters(appBinding)
201207
if err != nil {
202208
return nil, err
203209
}
204210

205-
// init restic wrapper
206-
resticWrapper, err := restic.NewResticWrapper(opt.setupOptions)
211+
err = session.setTLSParameters(appBinding, opt.setupOptions.ScratchDir)
207212
if err != nil {
208213
return nil, err
209214
}
210215

211-
// set env for mysqldump
212-
resticWrapper.SetEnv(EnvMySqlPassword, string(appBindingSecret.Data[MySqlPassword]))
213-
// setup pipe command
214-
backupCmd := restic.Command{
215-
Name: MySqlDumpCMD,
216-
Args: []interface{}{
217-
"-u", string(appBindingSecret.Data[MySqlUser]),
218-
"-h", appBinding.Spec.ClientConfig.Service.Name,
219-
},
220-
}
221-
// if port is specified, append port in the arguments
222-
if appBinding.Spec.ClientConfig.Service.Port != 0 {
223-
backupCmd.Args = append(backupCmd.Args, fmt.Sprintf("--port=%d", appBinding.Spec.ClientConfig.Service.Port))
224-
}
225-
for _, arg := range strings.Fields(opt.myArgs) {
226-
backupCmd.Args = append(backupCmd.Args, arg)
216+
err = session.waitForDBReady(opt.waitTimeout)
217+
if err != nil {
218+
return nil, err
227219
}
228220

229-
// if ssl enabled, add ca.crt in the arguments
230-
if appBinding.Spec.ClientConfig.CABundle != nil {
231-
if err := ioutil.WriteFile(filepath.Join(opt.setupOptions.ScratchDir, MySQLTLSRootCA), appBinding.Spec.ClientConfig.CABundle, os.ModePerm); err != nil {
232-
return nil, err
233-
}
234-
tlsCreds := []interface{}{
235-
fmt.Sprintf("--ssl-ca=%v", filepath.Join(opt.setupOptions.ScratchDir, MySQLTLSRootCA)),
236-
}
237-
238-
backupCmd.Args = append(backupCmd.Args, tlsCreds...)
239-
}
221+
session.setUserArgs(opt.myArgs)
240222

241-
// wait for DB ready
242-
err = opt.waitForDBReady(appBinding, appBindingSecret)
223+
// add backup command in the pipeline
224+
opt.backupOptions.StdinPipeCommands = append(opt.backupOptions.StdinPipeCommands, *session.cmd)
225+
resticWrapper, err := restic.NewResticWrapperFromShell(opt.setupOptions, session.sh)
243226
if err != nil {
244227
return nil, err
245228
}
246229

247-
// add backup command in the pipeline
248-
opt.backupOptions.StdinPipeCommands = append(opt.backupOptions.StdinPipeCommands, backupCmd)
249-
250-
// Run backup
251230
return resticWrapper.RunBackup(opt.backupOptions, targetRef)
252231
}

pkg/restore.go

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
/*
22
Copyright AppsCode Inc. and Contributors
3-
43
Licensed under the AppsCode Free Trial License 1.0.0 (the "License");
54
you may not use this file except in compliance with the License.
65
You may obtain a copy of the License at
7-
86
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Free-Trial-1.0.0.md
9-
107
Unless required by applicable law or agreed to in writing, software
118
distributed under the License is distributed on an "AS IS" BASIS,
129
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,11 +15,7 @@ package pkg
1815

1916
import (
2017
"context"
21-
"fmt"
22-
"io/ioutil"
23-
"os"
2418
"path/filepath"
25-
"strings"
2619

2720
api_v1beta1 "stash.appscode.dev/apimachinery/apis/stash/v1beta1"
2821
"stash.appscode.dev/apimachinery/pkg/restic"
@@ -60,7 +53,7 @@ func NewCmdRestore() *cobra.Command {
6053
Short: "Restores MySQL DB Backup",
6154
DisableAutoGenTag: true,
6255
RunE: func(cmd *cobra.Command, args []string) error {
63-
flags.EnsureRequiredFlags(cmd, "appbinding", "provider", "secret-dir")
56+
flags.EnsureRequiredFlags(cmd, "appbinding", "provider", "storage-secret-name", "storage-secret-namespace")
6457

6558
// prepare client
6659
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
@@ -118,13 +111,14 @@ func NewCmdRestore() *cobra.Command {
118111
cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
119112
cmd.Flags().StringVar(&opt.namespace, "namespace", "default", "Namespace of Backup/Restore Session")
120113
cmd.Flags().StringVar(&opt.appBindingName, "appbinding", opt.appBindingName, "Name of the app binding")
121-
122114
cmd.Flags().StringVar(&opt.setupOptions.Provider, "provider", opt.setupOptions.Provider, "Backend provider (i.e. gcs, s3, azure etc)")
123115
cmd.Flags().StringVar(&opt.setupOptions.Bucket, "bucket", opt.setupOptions.Bucket, "Name of the cloud bucket/container (keep empty for local backend)")
124116
cmd.Flags().StringVar(&opt.setupOptions.Endpoint, "endpoint", opt.setupOptions.Endpoint, "Endpoint for s3/s3 compatible backend or REST backend URL")
125117
cmd.Flags().StringVar(&opt.setupOptions.Region, "region", opt.setupOptions.Region, "Region for s3/s3 compatible backend")
126118
cmd.Flags().StringVar(&opt.setupOptions.Path, "path", opt.setupOptions.Path, "Directory inside the bucket where backup will be stored")
127-
cmd.Flags().StringVar(&opt.setupOptions.SecretDir, "secret-dir", opt.setupOptions.SecretDir, "Directory where storage secret has been mounted")
119+
cmd.Flags().StringVar(&opt.storageSecret.Name, "storage-secret-name", opt.storageSecret.Name, "Name of the storage secret")
120+
cmd.Flags().StringVar(&opt.storageSecret.Namespace, "storage-secret-namespace", opt.storageSecret.Namespace, "Namespace of the storage secret")
121+
128122
cmd.Flags().StringVar(&opt.setupOptions.ScratchDir, "scratch-dir", opt.setupOptions.ScratchDir, "Temporary directory")
129123
cmd.Flags().BoolVar(&opt.setupOptions.EnableCache, "enable-cache", opt.setupOptions.EnableCache, "Specify whether to enable caching for restic")
130124
cmd.Flags().Int64Var(&opt.setupOptions.MaxConnections, "max-connections", opt.setupOptions.MaxConnections, "Specify maximum concurrent connections for GCS, Azure and B2 backend")
@@ -140,8 +134,14 @@ func NewCmdRestore() *cobra.Command {
140134
}
141135

142136
func (opt *mysqlOptions) restoreMySQL(targetRef api_v1beta1.TargetRef) (*restic.RestoreOutput, error) {
143-
// apply nice, ionice settings from env
137+
144138
var err error
139+
opt.setupOptions.StorageSecret, err = opt.kubeClient.CoreV1().Secrets(opt.storageSecret.Namespace).Get(context.TODO(), opt.storageSecret.Name, metav1.GetOptions{})
140+
if err != nil {
141+
return nil, err
142+
}
143+
144+
// apply nice, ionice settings from env
145145
opt.setupOptions.Nice, err = v1.NiceSettingsFromEnv()
146146
if err != nil {
147147
return nil, err
@@ -151,68 +151,40 @@ func (opt *mysqlOptions) restoreMySQL(targetRef api_v1beta1.TargetRef) (*restic.
151151
return nil, err
152152
}
153153

154-
// get app binding
155154
appBinding, err := opt.catalogClient.AppcatalogV1alpha1().AppBindings(opt.namespace).Get(context.TODO(), opt.appBindingName, metav1.GetOptions{})
156155
if err != nil {
157156
return nil, err
158157
}
159-
// get secret
160-
appBindingSecret, err := opt.kubeClient.CoreV1().Secrets(opt.namespace).Get(context.TODO(), appBinding.Spec.Secret.Name, metav1.GetOptions{})
158+
159+
session := opt.newSessionWrapper(MySqlRestoreCMD)
160+
161+
err = session.setDatabaseCredentials(opt.kubeClient, appBinding)
161162
if err != nil {
162163
return nil, err
163164
}
164165

165-
// transform secret
166-
err = appBinding.TransformSecret(opt.kubeClient, appBindingSecret.Data)
166+
err = session.setDatabaseConnectionParameters(appBinding)
167167
if err != nil {
168168
return nil, err
169169
}
170170

171-
// init restic wrapper
172-
resticWrapper, err := restic.NewResticWrapper(opt.setupOptions)
171+
err = session.setTLSParameters(appBinding, opt.setupOptions.ScratchDir)
173172
if err != nil {
174173
return nil, err
175174
}
176175

177-
// set env for mysql
178-
resticWrapper.SetEnv(EnvMySqlPassword, string(appBindingSecret.Data[MySqlPassword]))
179-
// setup pipe command
180-
restoreCmd := restic.Command{
181-
Name: MySqlRestoreCMD,
182-
Args: []interface{}{
183-
"-u", string(appBindingSecret.Data[MySqlUser]),
184-
"-h", appBinding.Spec.ClientConfig.Service.Name,
185-
},
186-
}
187-
// if port is specified, append port in the arguments
188-
if appBinding.Spec.ClientConfig.Service.Port != 0 {
189-
restoreCmd.Args = append(restoreCmd.Args, fmt.Sprintf("--port=%d", appBinding.Spec.ClientConfig.Service.Port))
190-
}
191-
for _, arg := range strings.Fields(opt.myArgs) {
192-
restoreCmd.Args = append(restoreCmd.Args, arg)
176+
err = session.waitForDBReady(opt.waitTimeout)
177+
if err != nil {
178+
return nil, err
193179
}
194180

195-
// if ssl enabled, add ca.crt in the arguments
196-
if appBinding.Spec.ClientConfig.CABundle != nil {
197-
if err := ioutil.WriteFile(filepath.Join(opt.setupOptions.ScratchDir, MySQLTLSRootCA), appBinding.Spec.ClientConfig.CABundle, os.ModePerm); err != nil {
198-
return nil, err
199-
}
200-
tlsCreds := []interface{}{
201-
fmt.Sprintf("--ssl-ca=%v", filepath.Join(opt.setupOptions.ScratchDir, MySQLTLSRootCA)),
202-
}
181+
session.setUserArgs(opt.myArgs)
203182

204-
restoreCmd.Args = append(restoreCmd.Args, tlsCreds...)
205-
}
206-
207-
// wait for DB ready
208-
err = opt.waitForDBReady(appBinding, appBindingSecret)
183+
// append the restore command to the pipeline
184+
opt.dumpOptions.StdoutPipeCommands = append(opt.dumpOptions.StdoutPipeCommands, *session.cmd)
185+
resticWrapper, err := restic.NewResticWrapperFromShell(opt.setupOptions, session.sh)
209186
if err != nil {
210187
return nil, err
211188
}
212-
213-
// append the restore command to the pipeline
214-
opt.dumpOptions.StdoutPipeCommands = append(opt.dumpOptions.StdoutPipeCommands, restoreCmd)
215-
216-
// Run dump
217189
return resticWrapper.Dump(opt.dumpOptions, targetRef)
218190
}

0 commit comments

Comments
 (0)