diff --git a/cmd/raw.go b/cmd/raw.go index 2e8b99326efb6..26f3dd1433c9f 100644 --- a/cmd/raw.go +++ b/cmd/raw.go @@ -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 } @@ -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) diff --git a/pkg/raw/full.go b/pkg/raw/full.go index f2857c186aba5..8eab91909d7d9 100644 --- a/pkg/raw/full.go +++ b/pkg/raw/full.go @@ -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", @@ -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 @@ -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{ @@ -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, t) 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.