diff --git a/service/mongodb/restore.go b/service/mongodb/restore.go index 74922fa1..a72dccb7 100644 --- a/service/mongodb/restore.go +++ b/service/mongodb/restore.go @@ -32,6 +32,7 @@ func Restore(ctx context.Context, s3 *s3.Client, service util.Service, binding * command = append(command, "--uri") command = append(command, uri) command = append(command, "--gzip") + command = append(command, "--drop") command = append(command, "--archive") log.Debugf("executing mongodb restore command: %v", strings.Join(command, " ")) diff --git a/service/mysql/backup.go b/service/mysql/backup.go index e23d67ef..e38cc259 100644 --- a/service/mysql/backup.go +++ b/service/mysql/backup.go @@ -45,6 +45,7 @@ func Backup(ctx context.Context, s3 *s3.Client, service util.Service, binding *c if service.DisableColumnStatistics { command = append(command, "--column-statistics=0") } + command = append(command, "--hex-blob=TRUE") command = append(command, "-h") command = append(command, credentials.Hostname) command = append(command, "-P") @@ -59,10 +60,12 @@ func Backup(ctx context.Context, s3 *s3.Client, service util.Service, binding *c } log.Debugf("executing mysql backup command: %v", strings.Join(command, " ")) - cmd := exec.CommandContext(ctx, command[0], command[1:]...) + mysqldumpCmd := exec.CommandContext(ctx, command[0], command[1:]...) + sedCmd := exec.CommandContext(ctx, "sed", "-e", "s/DEFINER=[^*]*\\*/\\*/g") // capture stdout to pass to gzipping buffer - outPipe, err := cmd.StdoutPipe() + outPipe, _ := sedCmd.StdoutPipe() + _, err := mysqldumpCmd.StdoutPipe() if err != nil { log.Errorf("could not get stdout pipe for mysqldump: %v", err) state.BackupFailure(service) @@ -70,6 +73,11 @@ func Backup(ctx context.Context, s3 *s3.Client, service util.Service, binding *c } defer outPipe.Close() + // pipe mysqldumpCmd to sedCmd + sedPipeR, sedPipeW := io.Pipe() + mysqldumpCmd.Stdout = sedPipeW + sedCmd.Stdin = sedPipeR + var uploadWait sync.WaitGroup uploadCtx, uploadCancel := context.WithCancel(context.Background()) // allows upload to be cancelable, in case backup times out defer uploadCancel() // cancel upload in case Backup() exits before uploadWait is done @@ -108,15 +116,16 @@ func Backup(ctx context.Context, s3 *s3.Client, service util.Service, binding *c // capture and read stderr in case an error occurs var errBuf bytes.Buffer - cmd.Stderr = &errBuf + mysqldumpCmd.Stderr = &errBuf - if err := cmd.Start(); err != nil { + if err := mysqldumpCmd.Start(); err != nil { log.Errorf("could not run mysqldump: %v", err) state.BackupFailure(service) return err } + sedCmd.Start() - if err := cmd.Wait(); err != nil { + if err := mysqldumpCmd.Wait(); err != nil { state.BackupFailure(service) // check for timeout error if ctx.Err() == context.DeadlineExceeded { @@ -126,7 +135,8 @@ func Backup(ctx context.Context, s3 *s3.Client, service util.Service, binding *c log.Errorln(strings.TrimRight(errBuf.String(), "\r\n")) return fmt.Errorf("mysqldump: %v", err) } - + sedPipeW.Close() + sedCmd.Wait() uploadWait.Wait() // wait for upload to have finished if err == nil { state.BackupSuccess(service)