Skip to content

Commit

Permalink
Merge pull request pingcap#16 from 5kbpers/backup-full
Browse files Browse the repository at this point in the history
back up table by table in backup full
  • Loading branch information
5kbpers authored Oct 17, 2019
2 parents feb1191 + c086c74 commit 6f5dc03
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
15 changes: 7 additions & 8 deletions cmd/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,15 @@ func newFullBackupCommand() *cobra.Command {
return errors.New("at least one thread required")
}

err = client.BackupAllSchemas(backupTS)
ranges, err := client.GetAllBackupTableRanges(backupTS)
if err != nil {
return err
}

done := make(chan struct{}, 1)

startKey := []byte("")
endKey := []byte("")

// the count of regions need to backup
approximateRegions, err := client.GetRangeRegionCount(startKey, nil)
approximateRegions, err := client.GetRangeRegionCount([]byte{}, []byte{})
if err != nil {
return err
}
Expand All @@ -83,9 +80,11 @@ func newFullBackupCommand() *cobra.Command {
client.PrintBackupProgress("Full Backup", int64(approximateRegions), done)
}()

err = client.BackupRange(startKey, endKey, u, backupTS, rate, concurrency)
if err != nil {
return err
for _, r := range ranges {
err := client.BackupRange(r.StartKey, r.EndKey, u, backupTS, rate, concurrency)
if err != nil {
return err
}
}
done <- struct{}{}
return client.SaveBackupMeta(u)
Expand Down
22 changes: 15 additions & 7 deletions pkg/raw/full.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func buildTableRanges(tbl *model.TableInfo) []tableRange {
}

// BackupAllSchemas fetches all schemas from TiDB.
func (bc *BackupClient) BackupAllSchemas(backupTS uint64) error {
func (bc *BackupClient) GetAllBackupTableRanges(backupTS uint64) ([]Range, error) {
SystemDatabases := [3]string{
"information_schema",
"performance_schema",
Expand All @@ -228,15 +228,16 @@ func (bc *BackupClient) BackupAllSchemas(backupTS uint64) error {

dbSession, err := session.CreateSession(bc.backer.GetTiKV())
if err != nil {
return errors.Trace(err)
return nil, errors.Trace(err)
}
do := domain.GetDomain(dbSession.(sessionctx.Context))
info, err := do.GetSnapshotInfoSchema(backupTS)
if err != nil {
return errors.Trace(err)
return nil, errors.Trace(err)
}

dbInfos := info.AllSchemas()
ranges := make([]Range, 0)
LoadDb:
for _, dbInfo := range dbInfos {
// skip system databases
Expand All @@ -247,18 +248,18 @@ LoadDb:
}
dbData, err := json.Marshal(dbInfo)
if err != nil {
return errors.Trace(err)
return nil, errors.Trace(err)
}
idAlloc := autoid.NewAllocator(bc.backer.GetTiKV(), dbInfo.ID, false)
for _, tableInfo := range dbInfo.Tables {
globalAutoID, err := idAlloc.NextGlobalAutoID(tableInfo.ID)
if err != nil {
return errors.Trace(err)
return nil, errors.Trace(err)
}
tableInfo.AutoIncID = globalAutoID
tableData, err := json.Marshal(tableInfo)
if err != nil {
return errors.Trace(err)
return nil, errors.Trace(err)
}
// Save schema.
backupSchema := &backup.Schema{
Expand All @@ -271,9 +272,16 @@ LoadDb:
zap.Int64("auto_inc_id", globalAutoID),
)
bc.backupMeta.Schemas = append(bc.backupMeta.Schemas, backupSchema)

// TODO: We may need to include [t<tableID>, t<tableID+1>) in order to
// backup global index.
tableRanges := buildTableRanges(tableInfo)
for _, r := range tableRanges {
ranges = append(ranges, r.Range())
}
}
}
return nil
return ranges, nil
}

// BackupRange make a backup of the given key range.
Expand Down

0 comments on commit 6f5dc03

Please sign in to comment.