diff --git a/cmd/backup.go b/cmd/backup.go index 73ae6106f..2008bf582 100644 --- a/cmd/backup.go +++ b/cmd/backup.go @@ -2,6 +2,7 @@ package cmd import ( "context" + "fmt" "github.com/pingcap/errors" "github.com/pingcap/log" @@ -108,6 +109,11 @@ func runBackup(flagSet *pflag.FlagSet, cmdName, db, table string) error { return err } + err = client.SaveBackupTS(ctx, backupTS) + if err != nil { + return err + } + defer summary.Summary(cmdName) ranges, backupSchemas, err := backup.BuildBackupRangeAndSchema( @@ -170,6 +176,12 @@ func runBackup(flagSet *pflag.FlagSet, cmdName, db, table string) error { if err != nil { return err } + + content, err := client.GetBackupTS(ctx) + if err != nil { + return err + } + fmt.Println(string(content)) return nil } diff --git a/pkg/backup/client.go b/pkg/backup/client.go index 5cba2d9bf..9fc0e5316 100644 --- a/pkg/backup/client.go +++ b/pkg/backup/client.go @@ -120,6 +120,35 @@ func (bc *Client) SetStorage(ctx context.Context, backend *backup.StorageBackend return nil } +// SaveBackupTS saves the backup ts at the given path. +func (bc *Client) SaveBackupTS(ctx context.Context, backupTS uint64) error { + if exists, _ := bc.storage.FileExists(ctx, utils.TSFile); exists { + return errors.New("this storage has backup file, consider clear this storage or change to new one") + } + backendURL := storage.FormatBackendURL(bc.backend) + + log.Info("save backup ts", zap.Stringer("path", &backendURL)) + type fileContent struct { + StartedAt string `json:"started_at"` + BackupTS uint64 `json:"backup_ts"` + } + f := &fileContent{ + StartedAt: time.Now().Format("2006-01-02 15:04:05"), + BackupTS: backupTS, + } + fileBytes, err := json.MarshalIndent(f, "", "\t") + if err != nil { + return err + } + + return bc.storage.Write(ctx, utils.TSFile, fileBytes) +} + +// GetBackupTS get the backup ts from given path. +func (bc *Client) GetBackupTS(ctx context.Context) ([]byte, error) { + return bc.storage.Read(ctx, utils.TSFile) +} + // SaveBackupMeta saves the current backup meta at the given path. func (bc *Client) SaveBackupMeta(ctx context.Context) error { backupMetaData, err := proto.Marshal(&bc.backupMeta) diff --git a/pkg/utils/schema.go b/pkg/utils/schema.go index 67d28132f..9ade83d57 100644 --- a/pkg/utils/schema.go +++ b/pkg/utils/schema.go @@ -17,6 +17,8 @@ import ( const ( // MetaFile represents file name MetaFile = "backupmeta" + // TSFile represents backup ts file, it generated before backup + TSFile = "backupts" // MetaJSONFile represents backup meta json file name MetaJSONFile = "backupmeta.json" )