Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: update progress for backup restore (#2499)

Yilong Li92cc3070c5e7
diff --git a/plugin/db/mysql/dump.go b/plugin/db/mysql/dump.go
index 95f95d303f..2fba96cc6b 100644
--- a/plugin/db/mysql/dump.go
+++ b/plugin/db/mysql/dump.go
@@ -685,8 +685,10 @@ func (driver *Driver) restoreImpl(ctx context.Context, backup io.Reader, databas
mysqlCmd := exec.CommandContext(ctx, mysqlutil.GetPath(mysqlutil.MySQL, driver.resourceDir), mysqlArgs...)
var stderr bytes.Buffer
- mysqlCmd.Stdin = backup
+ countingReader := common.NewCountingReader(backup)
+ mysqlCmd.Stdin = countingReader
mysqlCmd.Stderr = &stderr
+ driver.restoredBackupBytes = countingReader
if err := mysqlCmd.Run(); err != nil {
return errors.Wrapf(err, "mysql command fails: %s", stderr.String())
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index c839cc271b..aff7de8c86 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -38,7 +38,8 @@ type Driver struct {
binlogDir string
db *sql.DB
- replayBinlogCounter *common.CountingReader
+ replayedBinlogBytes *common.CountingReader
+ restoredBackupBytes *common.CountingReader
}
func newDriver(dc db.DriverConfig) db.Driver {
diff --git a/plugin/db/mysql/restore.go b/plugin/db/mysql/restore.go
index 04ed9c5b24..976b5e2da9 100644
--- a/plugin/db/mysql/restore.go
+++ b/plugin/db/mysql/restore.go
@@ -192,7 +192,7 @@ func (driver *Driver) replayBinlogFromDir(ctx context.Context, originalDatabase,
mysqlCmd.Stderr = os.Stderr
mysqlCmd.Stdout = os.Stderr
mysqlCmd.Stdin = countingReader
- driver.replayBinlogCounter = countingReader
+ driver.replayedBinlogBytes = countingReader
if err := mysqlbinlogCmd.Start(); err != nil {
return errors.Wrap(err, "cannot start mysqlbinlog command")
@@ -208,12 +208,20 @@ func (driver *Driver) replayBinlogFromDir(ctx context.Context, originalDatabase,
return nil
}
+// GetRestoredBackupBytes gets the restored backup bytes.
+func (driver *Driver) GetRestoredBackupBytes() int64 {
+ if driver.restoredBackupBytes == nil {
+ return 0
+ }
+ return driver.restoredBackupBytes.Count()
+}
+
// GetReplayedBinlogBytes gets the replayed binlog bytes.
func (driver *Driver) GetReplayedBinlogBytes() int64 {
- if driver.replayBinlogCounter == nil {
+ if driver.replayedBinlogBytes == nil {
return 0
}
- return driver.replayBinlogCounter.Count()
+ return driver.replayedBinlogBytes.Count()
}
// ReplayBinlogToDatabase replays the binlog of originDatabaseName to the targetDatabaseName.
@@ -230,8 +238,10 @@ func (driver *Driver) ReplayBinlogToPITRDatabase(ctx context.Context, databaseNa
// RestoreBackupToDatabase create the database named `databaseName` and restores a full backup to the given database.
func (driver *Driver) RestoreBackupToDatabase(ctx context.Context, backup io.Reader, databaseName string) error {
- // Create the pitr database.
- return driver.restoreImpl(ctx, backup, databaseName)
+ if err := driver.restoreImpl(ctx, backup, databaseName); err != nil {
+ return errors.Wrapf(err, "failed to restore backup to the database %s", databaseName)
+ }
+ return nil
}
// RestoreBackupToPITRDatabase restores a full backup to the PITR database.
@@ -248,7 +258,7 @@ func (driver *Driver) RestoreBackupToPITRDatabase(ctx context.Context, backup io
if _, err := db.ExecContext(ctx, stmt); err != nil {
return errors.Wrapf(err, "failed to create the PITR database %s", pitrDatabaseName)
}
- if err := driver.RestoreBackupToDatabase(ctx, backup, pitrDatabaseName); err != nil {
+ if err := driver.restoreImpl(ctx, backup, pitrDatabaseName); err != nil {
return errors.Wrapf(err, "failed to restore backup to the PITR database %s", pitrDatabaseName)
}
return nil
diff --git a/server/task_executor_pitr_restore.go b/server/task_executor_pitr_restore.go
index ba797ce117..d93a3b83c7 100644
--- a/server/task_executor_pitr_restore.go
+++ b/server/task_executor_pitr_restore.go
@@ -413,12 +413,9 @@ func (exec *PITRRestoreTaskExecutor) updateProgress(ctx context.Context, driver
select {
case <-ticker.C:
progressPrev := exec.progress.Load().(api.Progress)
- // TODO(dragonly): Calculate restored backup bytes when using mysqldump.
- restoredBackupFileBytes := backupFileBytes
- replayedBinlogBytes := driver.GetReplayedBinlogBytes()
exec.progress.Store(api.Progress{
TotalUnit: progressPrev.TotalUnit,
- CompletedUnit: restoredBackupFileBytes + replayedBinlogBytes,
+ CompletedUnit: driver.GetRestoredBackupBytes() + driver.GetReplayedBinlogBytes(),
CreatedTs: progressPrev.CreatedTs,
UpdatedTs: time.Now().Unix(),
})