diff --git a/pkg/task/restore.go b/pkg/task/restore.go index 7a2973b0e..ac3fb1b71 100644 --- a/pkg/task/restore.go +++ b/pkg/task/restore.go @@ -169,8 +169,8 @@ func RunRestore(c context.Context, g glue.Glue, cmdName string, cfg *RestoreConf } // nothing to restore, maybe only ddl changes in incremental restore - if len(files) == 0 { - log.Info("all files are filtered out from the backup archive, nothing to restore") + if len(dbs) == 0 && len(tables) == 0 { + log.Info("nothing to restore, all databases and tables are filtered out") // even nothing to restore, we show a success message since there is no failure. summary.SetSuccessStatus(true) return nil @@ -187,6 +187,12 @@ func RunRestore(c context.Context, g glue.Glue, cmdName string, cfg *RestoreConf if err != nil { return err } + if len(files) == 0 { + log.Info("no files, empty databases and tables are restored") + summary.SetSuccessStatus(true) + return nil + } + placementRules, err := client.GetPlacementRules(cfg.PD) if err != nil { return err @@ -335,6 +341,9 @@ func filterRestoreFiles( tables = append(tables, table) } } + if len(dbs) == 0 && len(tables) != 0 { + err = errors.New("invalid backup, contain tables but no databases") + } return } diff --git a/tests/br_backup_empty/run.sh b/tests/br_backup_empty/run.sh index a7fa1f233..fda894616 100644 --- a/tests/br_backup_empty/run.sh +++ b/tests/br_backup_empty/run.sh @@ -14,10 +14,11 @@ # limitations under the License. set -eu +DB="$TEST_NAME" # backup empty. echo "backup start..." -run_br --pd $PD_ADDR backup full -s "local://$TEST_DIR/empty" --ratelimit 5 --concurrency 4 +run_br --pd $PD_ADDR backup full -s "local://$TEST_DIR/empty_db" --ratelimit 5 --concurrency 4 if [ $? -ne 0 ]; then echo "TEST: [$TEST_NAME] failed on backup empty cluster!" exit 1 @@ -25,10 +26,28 @@ fi # restore empty. echo "restore start..." -run_br restore full -s "local://$TEST_DIR/empty" --pd $PD_ADDR --ratelimit 1024 +run_br restore full -s "local://$TEST_DIR/empty_db" --pd $PD_ADDR --ratelimit 1024 if [ $? -ne 0 ]; then echo "TEST: [$TEST_NAME] failed on restore empty cluster!" exit 1 fi +# backup and restore empty tables. +run_sql "CREATE DATABASE $DB;" +run_sql "CREATE TABLE $DB.usertable1 ( \ + YCSB_KEY varchar(64) NOT NULL, \ + FIELD0 varchar(1) DEFAULT NULL, \ + PRIMARY KEY (YCSB_KEY) \ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;" + +echo "backup start..." +run_br --pd $PD_ADDR backup full -s "local://$TEST_DIR/empty_table" --ratelimit 5 --concurrency 4 + +run_sql "DROP DATABASE $DB;" +echo "restore start..." +run_br --pd $PD_ADDR restore full -s "local://$TEST_DIR/empty_table" --ratelimit 5 --concurrency 4 + +# insert one row to make sure table is restored. +run_sql "INSERT INTO $DB.usertable1 VALUES (\"a\", \"b\");" + echo "TEST: [$TEST_NAME] successed!"