Skip to content

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

feat(PITR): add new backup type for PITR (#1723)

Yilong Lieff285dd081f
diff --git a/api/backup.go b/api/backup.go
index 7a78b78812..107a79cf28 100644
--- a/api/backup.go
+++ b/api/backup.go
@@ -37,6 +37,8 @@ type BackupType string
const (
// BackupTypeAutomatic is the type for automatic backup.
BackupTypeAutomatic BackupType = "AUTOMATIC"
+ // BackupTypePITR is the type of backup taken at PITR cutover stage.
+ BackupTypePITR BackupType = "PITR"
// BackupTypeManual is the type for manual backup.
BackupTypeManual BackupType = "MANUAL"
)
@@ -45,10 +47,13 @@ func (e BackupType) String() string {
switch e {
case BackupTypeAutomatic:
return "AUTOMATIC"
+ case BackupTypePITR:
+ return "PITR"
case BackupTypeManual:
return "MANUAL"
+ default:
+ return "UNKNOWN"
}
- return "UNKNOWN"
}
// BackupStorageBackend is the storage backend of a backup.
diff --git a/plugin/db/mysql/dump.go b/plugin/db/mysql/dump.go
index cc93a265c4..77943ddb43 100644
--- a/plugin/db/mysql/dump.go
+++ b/plugin/db/mysql/dump.go
@@ -88,12 +88,12 @@ func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer,
if !schemaOnly {
log.Debug("flush tables in database with read locks",
zap.String("database", database))
- if err := flushTablesWithReadLock(ctx, conn, database); err != nil {
+ if err := FlushTablesWithReadLock(ctx, conn, database); err != nil {
log.Error("flush tables failed", zap.Error(err))
return "", err
}
- binlog, err := getBinlogInfo(ctx, conn)
+ binlog, err := GetBinlogInfo(ctx, conn)
if err != nil {
return "", err
}
@@ -134,7 +134,8 @@ func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer,
return string(payloadBytes), nil
}
-func flushTablesWithReadLock(ctx context.Context, conn *sql.Conn, database string) error {
+// FlushTablesWithReadLock runs FLUSH TABLES table1, table2, ... WITH READ LOCK for all the tables in the database.
+func FlushTablesWithReadLock(ctx context.Context, conn *sql.Conn, database string) error {
// The lock acquiring could take a long time if there are concurrent exclusive locks on the tables.
// We ensures that the execution is canceled after 30 seconds, otherwise we may get dead lock and stuck forever.
ctxWithTimeout, cancel := context.WithTimeout(ctx, 30*time.Second)
@@ -172,6 +173,7 @@ func flushTablesWithReadLock(ctx context.Context, conn *sql.Conn, database strin
}
func dumpTxn(ctx context.Context, txn *sql.Tx, database string, out io.Writer, schemaOnly bool) error {
+ log.Debug("begin to dump database", zap.String("database", database))
// Find all dumpable databases
dbNames, err := getDatabases(ctx, txn)
if err != nil {
@@ -286,7 +288,8 @@ func excludeSchemaAutoIncrementValue(s string) string {
return excludeAutoIncrement.ReplaceAllString(s, ``)
}
-func getBinlogInfo(ctx context.Context, conn *sql.Conn) (api.BinlogInfo, error) {
+// GetBinlogInfo queries current binlog info from MySQL server.
+func GetBinlogInfo(ctx context.Context, conn *sql.Conn) (api.BinlogInfo, error) {
rows, err := conn.QueryContext(ctx, "SHOW MASTER STATUS;")
if err != nil {
return api.BinlogInfo{}, err
diff --git a/plugin/db/mysql/restore.go b/plugin/db/mysql/restore.go
index 36c9eca35b..ea9085dbc7 100644
--- a/plugin/db/mysql/restore.go
+++ b/plugin/db/mysql/restore.go
@@ -334,12 +334,13 @@ func SwapPITRDatabase(ctx context.Context, conn *sql.Conn, database string, suff
pitrOldDatabase := getPITROldDatabaseName(database, suffixTs)
// Handle the case that the original database does not exist, because user could drop a database and want to restore it.
- log.Debug("Check database exists", zap.String("database", database))
+ log.Debug("Checking database exists.", zap.String("database", database))
dbExists, err := databaseExists(ctx, conn, database)
if err != nil {
return pitrDatabaseName, pitrOldDatabase, fmt.Errorf("failed to check whether database %q exists, error: %w", database, err)
}
+ log.Debug("Turning binlog OFF.")
// Set OFF the session variable sql_log_bin so that the writes in the following SQL statements will not be recorded in the binlog.
if _, err := conn.ExecContext(ctx, "SET sql_log_bin=OFF"); err != nil {
return pitrDatabaseName, pitrOldDatabase, err
@@ -352,6 +353,7 @@ func SwapPITRDatabase(ctx context.Context, conn *sql.Conn, database string, suff
}
}
+ log.Debug("Getting tables in the original and PITR databases.")
tables, err := getTables(ctx, conn, database)
if err != nil {
return pitrDatabaseName, pitrOldDatabase, fmt.Errorf("failed to get tables of database %q, error: %w", database, err)
diff --git a/server/task_executor_database_backup.go b/server/task_executor_database_backup.go
index 605980bae4..f550085830 100644
--- a/server/task_executor_database_backup.go
+++ b/server/task_executor_database_backup.go
@@ -54,7 +54,7 @@ func (exec *DatabaseBackupTaskExecutor) RunOnce(ctx context.Context, server *Ser
backupPatch.Comment = backupErr.Error()
}
if _, err := server.store.PatchBackup(ctx, &backupPatch); err != nil {
- return true, nil, fmt.Errorf("failed to patch backup: %w", err)
+ return true, nil, fmt.Errorf("failed to patch backup, error: %w", err)
}
if backupErr != nil {
diff --git a/server/task_executor_pitr_cutover.go b/server/task_executor_pitr_cutover.go
index 092de2955b..f8a488f6fa 100644
--- a/server/task_executor_pitr_cutover.go
+++ b/server/task_executor_pitr_cutover.go
@@ -32,7 +32,6 @@ func (exec *PITRCutoverTaskExecutor) RunOnce(ctx context.Context, server *Server
log.Info("Run PITR cutover task", zap.String("task", task.Name))
issue, err := getIssueByPipelineID(ctx, server.store, task.PipelineID)
-
if err != nil {
log.Error("failed to fetch containing issue doing pitr cutover task", zap.Error(err))
return true, nil, err
@@ -111,7 +110,7 @@ func (exec *PITRCutoverTaskExecutor) pitrCutover(ctx context.Context, task *api.
}
log.Debug("Finish swapping the original and PITR database", zap.String("original_database", task.Database.Name), zap.String("pitr_database", pitrDatabaseName), zap.String("old_database", pitrOldDatabaseName))
- log.Debug("Appending new migration history record...")
+ log.Debug("Appending new migration history record")
m := &db.MigrationInfo{
ReleaseVersion: server.profile.Version,
Version: common.DefaultMigrationVersion(),