Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"fmt"

"github.com/pingcap/errors"
"github.com/pingcap/log"
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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))
Comment on lines +180 to +184
Copy link
Copy Markdown
Collaborator

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fmt.Println(string(content))
cmd.Println(string(content))

return nil
}

Expand Down
29 changes: 29 additions & 0 deletions pkg/backup/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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"),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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)
Expand Down
2 changes: 2 additions & 0 deletions pkg/utils/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
const (
// MetaFile represents file name
MetaFile = "backupmeta"
// TSFile represents backup ts file, it generated before backup
TSFile = "backupts"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

backupts.json?

// MetaJSONFile represents backup meta json file name
MetaJSONFile = "backupmeta.json"
)
Expand Down