Skip to content

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

feat: move the logic of restore full backup from database restore executor to pitr restore executor (#2218)

h3n4l0278172a7f92
diff --git a/api/task.go b/api/task.go
index a93f9c3949..080ec604d4 100644
--- a/api/task.go
+++ b/api/task.go
@@ -74,7 +74,7 @@ type TaskDatabasePITRRestorePayload struct {
DatabaseName *string `json:"databaseName,omitempty"`
// TargetInstanceId must be within the same environment as the instance of the original database.
- // Only used when doing PITR to a new database.
+ // Only used when doing PITR to a new database now.
TargetInstanceID *int `json:"targetInstanceId,omitempty"`
// BackupID and PointInTimeTs only allow one non-nil.
diff --git a/server/task_executor_pitr_restore.go b/server/task_executor_pitr_restore.go
index 0d81e54631..92b5da0684 100644
--- a/server/task_executor_pitr_restore.go
+++ b/server/task_executor_pitr_restore.go
@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"os"
+ "path/filepath"
"sync/atomic"
"time"
@@ -38,7 +39,96 @@ func (exec *PITRRestoreTaskExecutor) RunOnce(ctx context.Context, server *Server
return true, nil, fmt.Errorf("invalid PITR restore payload: %s, error: %w", task.Payload, err)
}
- driver, err := server.getAdminDatabaseDriver(ctx, task.Instance, "" /* databaseName */)
+ if (payload.BackupID == nil) == (payload.PointInTimeTs == nil) {
+ return true, nil, fmt.Errorf("only one of BackupID and time point can be set")
+ }
+
+ if payload.BackupID != nil {
+ if payload.DatabaseName == nil {
+ return true, nil, fmt.Errorf("unexpected nil database name for backup restore")
+ }
+ // Restore full backup only
+ backup, err := server.store.GetBackupByID(ctx, *payload.BackupID)
+ if err != nil {
+ return true, nil, fmt.Errorf("failed to find backup with ID %d, error: %w", *payload.BackupID, err)
+ }
+ if backup == nil {
+ return true, nil, fmt.Errorf("backup with ID %d not found", *payload.BackupID)
+ }
+ sourceDatabase, err := server.store.GetDatabase(ctx, &api.DatabaseFind{ID: &backup.DatabaseID})
+ if err != nil {
+ return true, nil, fmt.Errorf("failed to find database for the backup: %w", err)
+ }
+ if sourceDatabase == nil {
+ return true, nil, fmt.Errorf("source database ID not found %v", backup.DatabaseID)
+ }
+
+ targetInstanceID := task.InstanceID
+ if payload.TargetInstanceID != nil {
+ // For now, we just support restore full backup to the same instance with the origin database.
+ // But for generality, we will use TargetInstanceID in payload to find the target instance.
+ targetInstanceID = *payload.TargetInstanceID
+ }
+
+ targetDatabaseFind := &api.DatabaseFind{
+ InstanceID: &targetInstanceID,
+ Name: payload.DatabaseName,
+ }
+
+ targetDatabase, err := server.store.GetDatabase(ctx, targetDatabaseFind)
+ if err != nil {
+ return true, nil, fmt.Errorf("failed to find target database %q in instance %q: %w", *payload.DatabaseName, task.Instance.Name, err)
+ }
+ if targetDatabase == nil {
+ return true, nil, fmt.Errorf("target database %q not found in instance %q: %w", *payload.DatabaseName, task.Instance.Name, err)
+ }
+ log.Debug("Start database restore from backup...",
+ zap.String("source_instance", sourceDatabase.Instance.Name),
+ zap.String("source_database", sourceDatabase.Name),
+ zap.String("target_instance", targetDatabase.Instance.Name),
+ zap.String("target_database", targetDatabase.Name),
+ zap.String("backup", backup.Name),
+ )
+ // Restore the database to the target database.
+ if err := exec.restoreDatabase(ctx, server, targetDatabase.Instance, targetDatabase.Name, backup, server.profile.DataDir); err != nil {
+ return true, nil, err
+ }
+ // TODO(zp): This should be done in the same transaction as restoreDatabase to guarantee consistency.
+ // For now, we do this after restoreDatabase, since this one is unlikely to fail.
+ migrationID, version, err := createBranchMigrationHistory(ctx, server, sourceDatabase, targetDatabase, backup, task)
+ if err != nil {
+ return true, nil, err
+ }
+
+ // Patch the backup id after we successfully restore the database using the backup.
+ // restoringDatabase is changing the customer database instance, while here we are changing our own meta db,
+ // and since we can't guarantee cross database transaction consistency, there is always a chance to have
+ // inconsistent data. We choose to do Patch afterwards since this one is unlikely to fail.
+ databasePatch := &api.DatabasePatch{
+ ID: targetDatabase.ID,
+ UpdaterID: api.SystemBotID,
+ SourceBackupID: &backup.ID,
+ }
+ if _, err = server.store.PatchDatabase(ctx, databasePatch); err != nil {
+ return true, nil, fmt.Errorf("failed to patch database source with ID %d and backup ID %d after restore, error: %w", targetDatabase.ID, backup.ID, err)
+ }
+
+ // Sync database schema after restore is completed.
+ if err := server.syncDatabaseSchema(ctx, targetDatabase.Instance, targetDatabase.Name); err != nil {
+ log.Error("failed to sync database schema",
+ zap.String("instance", targetDatabase.Instance.Name),
+ zap.String("databaseName", targetDatabase.Name),
+ )
+ }
+
+ return true, &api.TaskRunResultPayload{
+ Detail: fmt.Sprintf("Restored database %q from backup %q", targetDatabase.Name, backup.Name),
+ MigrationID: migrationID,
+ Version: version,
+ }, nil
+ }
+
+ driver, err := server.getAdminDatabaseDriver(ctx, task.Instance, "")
if err != nil {
return true, nil, err
}
@@ -205,3 +295,28 @@ func getIssueByPipelineID(ctx context.Context, store *store.Store, pid int) (*ap
}
return issue, nil
}
+
+// restoreDatabase will restore the database from a backup.
+func (*PITRRestoreTaskExecutor) restoreDatabase(ctx context.Context, server *Server, instance *api.Instance, databaseName string, backup *api.Backup, dataDir string) error {
+ driver, err := server.getAdminDatabaseDriver(ctx, instance, databaseName)
+ if err != nil {
+ return err
+ }
+ defer driver.Close(ctx)
+
+ backupPath := backup.Path
+ if !filepath.IsAbs(backupPath) {
+ backupPath = filepath.Join(dataDir, backupPath)
+ }
+
+ f, err := os.Open(backupPath)
+ if err != nil {
+ return fmt.Errorf("failed to open backup file at %s: %w", backupPath, err)
+ }
+ defer f.Close()
+
+ if err := driver.Restore(ctx, f); err != nil {
+ return fmt.Errorf("failed to restore backup: %w", err)
+ }
+ return nil
+}