@@ -18,6 +18,7 @@ package mysqlctl
1818
1919import (
2020 "bufio"
21+ "bytes"
2122 "context"
2223 "encoding/json"
2324 "errors"
@@ -32,6 +33,7 @@ import (
3233 "sync"
3334 "time"
3435
36+ "github.com/google/shlex"
3537 "github.com/spf13/pflag"
3638
3739 "vitess.io/vitess/go/fileutil"
@@ -41,6 +43,7 @@ import (
4143 "vitess.io/vitess/go/vt/log"
4244 "vitess.io/vitess/go/vt/mysqlctl/backupstorage"
4345 tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
46+ vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
4447 "vitess.io/vitess/go/vt/servenv"
4548 "vitess.io/vitess/go/vt/vterrors"
4649)
@@ -140,14 +143,20 @@ func (be *MySQLShellBackupEngine) ExecuteBackup(ctx context.Context, params Back
140143 return BackupUnusable , vterrors .Wrap (err , "can't get MySQL version" )
141144 }
142145
143- args := []string {}
144- if mysqlShellFlags != "" {
145- args = append (args , strings .Fields (mysqlShellFlags )... )
146+ args , err := shlex .Split (mysqlShellFlags )
147+ if err != nil {
148+ return BackupUnusable , vterrors .Wrap (err , "failed to parse --mysql-shell-flags" )
149+ }
150+
151+ // compact and validate the json input from mysqlShellDumpFlags.
152+ var compactDumpFlags bytes.Buffer
153+ if err := json .Compact (& compactDumpFlags , []byte (mysqlShellDumpFlags )); err != nil {
154+ return BackupUnusable , vterrors .Errorf (vtrpcpb .Code_INVALID_ARGUMENT , "failed to parse --mysql-shell-dump-flags as JSON: %v" , err )
146155 }
147156
148157 args = append (args , "-e" , fmt .Sprintf ("util.dumpInstance(%q, %s)" ,
149158 location ,
150- mysqlShellDumpFlags ,
159+ compactDumpFlags . String () ,
151160 ))
152161
153162 // to be able to get the consistent GTID sets, we will acquire a global read lock before starting mysql shell.
@@ -362,15 +371,20 @@ func (be *MySQLShellBackupEngine) ExecuteRestore(ctx context.Context, params Res
362371 }
363372 defer resetFunc ()
364373
365- args := []string {}
374+ args , err := shlex .Split (mysqlShellFlags )
375+ if err != nil {
376+ return nil , vterrors .Wrap (err , "failed to parse --mysql-shell-flags" )
377+ }
366378
367- if mysqlShellFlags != "" {
368- args = append (args , strings .Fields (mysqlShellFlags )... )
379+ // compact and validate the json input from mysqlShellLoadFlags.
380+ var compactLoadFlags bytes.Buffer
381+ if err := json .Compact (& compactLoadFlags , []byte (mysqlShellLoadFlags )); err != nil {
382+ return nil , vterrors .Errorf (vtrpcpb .Code_INVALID_ARGUMENT , "failed to parse --mysql-shell-load-flags as JSON: %v" , err )
369383 }
370384
371385 args = append (args , "-e" , fmt .Sprintf ("util.loadDump(%q, %s)" ,
372386 location ,
373- mysqlShellLoadFlags ,
387+ compactLoadFlags . String () ,
374388 ))
375389
376390 cmd := exec .CommandContext (ctx , "mysqlsh" , args ... )
0 commit comments