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

restore: Fix system tables are filtered bug (#1207) #1224

Merged
Merged
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
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
13 changes: 12 additions & 1 deletion pkg/utils/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ 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_"

// NeedAutoID checks whether the table needs backing up with an autoid.
func NeedAutoID(tblInfo *model.TableInfo) bool {
hasRowID := !tblInfo.PKIsHandle && !tblInfo.IsCommonHandle
Expand Down Expand Up @@ -171,5 +174,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