Skip to content

Commit

Permalink
restore: support restore empty databases and tables (pingcap#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
overvenus committed May 28, 2020
1 parent bc21c31 commit 2bd3248
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
13 changes: 11 additions & 2 deletions pkg/task/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,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
Expand All @@ -189,6 +189,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
Expand Down Expand Up @@ -341,6 +347,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
}

Expand Down
23 changes: 21 additions & 2 deletions tests/br_backup_empty/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,40 @@
# 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
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!"

0 comments on commit 2bd3248

Please sign in to comment.