Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

backup: add backupts file as lock #151

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
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
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
18 changes: 18 additions & 0 deletions pkg/backup/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"sync"
"time"
Expand Down Expand Up @@ -120,6 +121,23 @@ 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
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.

backendURL := storage.FormatBackendURL(bc.backend)
log.Info("save backup ts", zap.Stringer("path", &backendURL))
fileString := fmt.Sprintf(
"**************************************"+
"\nStarted backup at: %v\nBackupTS: %d\n"+
"**************************************",
Copy link
Member

Choose a reason for hiding this comment

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

Can it be pretty-printed json?

time.Now().Format("2006-01-02 15:04:05"), backupTS)
return bc.storage.Write(ctx, utils.TSFile, []byte(fileString))
}

// 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
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