Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions service/mongodb/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " "))
Expand Down
22 changes: 16 additions & 6 deletions service/mysql/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -59,17 +60,24 @@ 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)
return err
}
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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down