Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#1207
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
Relax4Life authored and ti-chi-bot committed Jun 16, 2021
1 parent 5fcf445 commit 9acb4ad
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
14 changes: 11 additions & 3 deletions pkg/task/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,13 @@ func CheckRestoreDBAndTable(client *restore.Client, cfg *RestoreConfig) error {
schemasMap := make(map[string]struct{})
tablesMap := make(map[string]struct{})
for _, db := range schemas {
schemasMap[utils.EncloseName(db.Info.Name.O)] = struct{}{}
dbName := db.Info.Name.O
if name, ok := utils.GetSysDBName(db.Info.Name); utils.IsSysDB(name) && ok {
dbName = name
}
schemasMap[utils.EncloseName(dbName)] = struct{}{}
for _, table := range db.Tables {
tablesMap[utils.EncloseDBAndTable(db.Info.Name.O, table.Info.Name.O)] = struct{}{}
tablesMap[utils.EncloseDBAndTable(dbName, table.Info.Name.O)] = struct{}{}
}
}
restoreSchemas := cfg.Schemas
Expand Down Expand Up @@ -463,8 +467,12 @@ func filterRestoreFiles(
) (files []*backuppb.File, tables []*utils.Table, dbs []*utils.Database) {
for _, db := range client.GetDatabases() {
createdDatabase := false
dbName := db.Info.Name.O
if name, ok := utils.GetSysDBName(db.Info.Name); utils.IsSysDB(name) && ok {
dbName = name
}
for _, table := range db.Tables {
if !cfg.TableFilter.MatchTable(db.Info.Name.O, table.Info.Name.O) {
if !cfg.TableFilter.MatchTable(dbName, table.Info.Name.O) {
continue
}
if !createdDatabase {
Expand Down
15 changes: 14 additions & 1 deletion pkg/utils/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/pingcap/br/pkg/logutil"
)

<<<<<<< HEAD
const (
// LockFile represents file name
LockFile = "backup.lock"
Expand Down Expand Up @@ -45,6 +46,10 @@ type Table struct {
func (tbl *Table) NoChecksum() bool {
return tbl.Crc64Xor == 0 && tbl.TotalKvs == 0 && tbl.TotalBytes == 0
}
=======
// temporaryDBNamePrefix is the prefix name of system db, e.g. mysql system db will be rename to __TiDB_BR_Temporary_mysql
const temporaryDBNamePrefix = "__TiDB_BR_Temporary_"
>>>>>>> 2a9727fa (restore: Fix system tables are filtered bug (#1207))

// NeedAutoID checks whether the table needs backing up with an autoid.
func NeedAutoID(tblInfo *model.TableInfo) bool {
Expand Down Expand Up @@ -171,5 +176,13 @@ func IsSysDB(dbLowerName string) bool {

// TemporaryDBName makes a 'private' database name.
func TemporaryDBName(db string) model.CIStr {
return model.NewCIStr("__TiDB_BR_Temporary_" + db)
return model.NewCIStr(temporaryDBNamePrefix + db)
}

// GetSysDBName get the original name of system DB
func GetSysDBName(tempDB model.CIStr) (string, bool) {
if ok := strings.HasPrefix(tempDB.O, temporaryDBNamePrefix); !ok {
return tempDB.O, false
}
return tempDB.O[len(temporaryDBNamePrefix):], true
}
43 changes: 43 additions & 0 deletions tests/br_systables/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ modify_systables() {
run_sql "ANALYZE TABLE mysql.usertable;"
}

add_user() {
run_sql "CREATE USER 'newuser' IDENTIFIED BY 'newuserpassword';"
}

delete_user() {
run_sql "DROP USER 'newuser'"
}

add_test_data() {
run_sql "CREATE DATABASE usertest;"
run_sql "CREATE TABLE usertest.test(pk int primary key auto_increment, field varchar(255));"
run_sql "INSERT INTO usertest.test(field) VALUES $test_data"
}

delete_test_data() {
run_sql "DROP TABLE usertest.test;"
}

rollback_modify() {
run_sql "DROP TABLE mysql.foo;"
run_sql "DROP TABLE mysql.bar;"
Expand All @@ -47,8 +65,33 @@ check() {
# TODO check stats after supportting.
}

check2() {
run_sql "SELECT count(*) from usertest.test;" | grep 11
run_sql "SELECT user FROM mysql.user WHERE user='newuser';" | grep 'newuser'
}

modify_systables
run_br backup full -s "local://$backup_dir"
rollback_modify
run_br restore full -f '*.*' -f '!mysql.bar' -s "local://$backup_dir"
check

run_br restore full -f 'mysql.bar' -s "local://$backup_dir"
run_sql "SELECT count(*) from mysql.bar;" | grep 11

rollback_modify
run_br restore full -f "mysql*.*" -f '!mysql.bar' -s "local://$backup_dir"
check

add_user
add_test_data
run_br backup full -s "local://${backup_dir}1"
delete_user
delete_test_data
run_br restore full -f "mysql*.*" -f "usertest.*" -s "local://${backup_dir}1"
check2

delete_user
run_br restore db --db mysql -s "local://${backup_dir}1"
check2

0 comments on commit 9acb4ad

Please sign in to comment.