-
Notifications
You must be signed in to change notification settings - Fork 100
backup: add backupts file as lock #151
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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)) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add a test. |
||
| 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"), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you log the time in backupTS? |
||
| BackupTS: backupTS, | ||
| } | ||
| fileBytes, err := json.MarshalIndent(f, "", "\t") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return bc.storage.Write(ctx, utils.TSFile, fileBytes) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it fail if there is TSFile already? |
||
| } | ||
|
|
||
| // 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) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ import ( | |
| const ( | ||
| // MetaFile represents file name | ||
| MetaFile = "backupmeta" | ||
| // TSFile represents backup ts file, it generated before backup | ||
| TSFile = "backupts" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| // MetaJSONFile represents backup meta json file name | ||
| MetaJSONFile = "backupmeta.json" | ||
| ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we still need to log the backup TS if we write it to a human-readable file.