From 9dc25b969fa3d586af6f9a6f2c90c3d807b87b6b Mon Sep 17 00:00:00 2001 From: zhangwenjum <2780898229@qq.com> Date: Tue, 15 Jun 2021 15:28:37 +0800 Subject: [PATCH] This is an automated cherry-pick of #1207 Signed-off-by: ti-chi-bot --- pkg/task/restore.go | 14 ++++++++++--- pkg/utils/schema.go | 15 +++++++++++++- tests/br_systables/run.sh | 43 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/pkg/task/restore.go b/pkg/task/restore.go index b083454a9..58a689ef0 100644 --- a/pkg/task/restore.go +++ b/pkg/task/restore.go @@ -99,9 +99,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 @@ -391,8 +395,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 { diff --git a/pkg/utils/schema.go b/pkg/utils/schema.go index 388601c9c..6ece28d4a 100644 --- a/pkg/utils/schema.go +++ b/pkg/utils/schema.go @@ -18,6 +18,7 @@ import ( "github.com/pingcap/br/pkg/logutil" ) +<<<<<<< HEAD const ( // LockFile represents file name LockFile = "backup.lock" @@ -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 { @@ -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 } diff --git a/tests/br_systables/run.sh b/tests/br_systables/run.sh index 250c1ec44..0a53945c4 100644 --- a/tests/br_systables/run.sh +++ b/tests/br_systables/run.sh @@ -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;" @@ -46,8 +64,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 +