######################################################################
# Test restoring backups with in- or excluding tables or databases
######################################################################

-- source include/have_ndb.inc
-- source include/ndb_default_cluster.inc

--echo *************************************
--echo Creating various databases and tables
--echo *************************************
create database db1;
create database db2;

use db1;
create table tab1 (a int primary key) engine=ndb;
create table tab2 (a int primary key,
        b varchar(20),
        c int,
        d text,
        unique(c)) engine=ndb;
insert into tab1 values (1),(2),(3),(4),(5),(6),(7),(8),(9),(0);
insert into tab2 values (1, "Donnington", 1, repeat("O", 1000)),
(2, "Basingstoke", 2, repeat("P", 1000)),
(3, "George", 3, repeat("Q", 1000)),
(4, "Spottiswoode", 4, repeat("R", 1000)),
(5, "Sciennes", 5, repeat("S", 1000)),
(6, "Cairns", 6, repeat("T", 1000)),
(7, "Highlea", 7, repeat("U", 1000));

# checksum table tab1;
# checksum table tab2;
select * from tab1 order by a;
select * from tab2 order by a;

use db2;

create table tab1 (a int primary key) engine=ndb;
create table tab2 (a int primary key, b varchar(20), c int, unique(c), d text) engine=ndb;

insert into tab1 select * from db1.tab1;
insert into tab2 select * from db1.tab2;

# checksum table tab1;
# checksum table tab2;
select * from tab1 order by a;
select * from tab2 order by a;

--echo ***********************
--echo Backing up created data
--echo ***********************
--source include/ndb_backup.inc

drop table db1.tab1, db1.tab2, db2.tab1, db2.tab2;

--echo **************************************
--echo Check various include/exclude variants
--echo **************************************
--echo Normal full restore
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 1 -r -m  $NDB_BACKUPS-$the_backup_id >> $NDB_TOOLS_OUTPUT
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 2 -r     $NDB_BACKUPS-$the_backup_id >> $NDB_TOOLS_OUTPUT

use db1;
--sorted_result
show tables;
# checksum table tab1, tab2;
select * from tab1 order by a;
select * from tab2 order by a;

use db2;
--sorted_result
show tables;
# checksum table tab1, tab2;
select * from tab1 order by a;
select * from tab2 order by a;

drop table db1.tab1, db1.tab2, db2.tab1, db2.tab2;

--echo ****************
--echo Include only db2
--echo ****************
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 1 -r -m  $NDB_BACKUPS-$the_backup_id --include-databases db2>> $NDB_TOOLS_OUTPUT
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 2 -r     $NDB_BACKUPS-$the_backup_id --include-databases db2 >> $NDB_TOOLS_OUTPUT

use db1;
--sorted_result
show tables;
use db2;
--sorted_result
show tables;
# checksum table tab1, tab2;
select * from tab1 order by a;
select * from tab2 order by a;

drop table db2.tab1, db2.tab2;

--echo ****************
--echo Exclude only db2
--echo ****************
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 1 -r -m  $NDB_BACKUPS-$the_backup_id --exclude-databases db2>> $NDB_TOOLS_OUTPUT
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 2 -r     $NDB_BACKUPS-$the_backup_id --exclude-databases db2 >> $NDB_TOOLS_OUTPUT


use db1;
--sorted_result
show tables;
# checksum table tab1, tab2;
select * from tab1 order by a;
select * from tab2 order by a;

use db2;
--sorted_result
show tables;

drop table db1.tab1, db1.tab2;

--echo ************
--echo Exclude both
--echo ************
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 1 -r -m  $NDB_BACKUPS-$the_backup_id --exclude-databases db2,db1>> $NDB_TOOLS_OUTPUT
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 2 -r     $NDB_BACKUPS-$the_backup_id --exclude-databases db2,db1 >> $NDB_TOOLS_OUTPUT


use db1;
--sorted_result
show tables;
use db2;
--sorted_result
show tables;

--echo **********************************
--echo Include only db1.tab1 and db2.tab2
--echo **********************************

--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 1 -r -m  $NDB_BACKUPS-$the_backup_id --include-tables db1.tab1,db2.tab2 >> $NDB_TOOLS_OUTPUT
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 2 -r     $NDB_BACKUPS-$the_backup_id --include-tables db1.tab1,db2.tab2 >> $NDB_TOOLS_OUTPUT


use db1;
--sorted_result
show tables;
# checksum table tab1;
select * from tab1 order by a;

use db2;
--sorted_result
show tables;
# checksum table tab2;
select * from tab2 order by a;


drop table db1.tab1, db2.tab2;

--echo *****************************
--echo Exclude db1.tab1 and db2.tab2
--echo *****************************
--echo Should result in db1.tab2 and db2.tab1 being restored
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 1 -r -m  $NDB_BACKUPS-$the_backup_id --exclude-tables db1.tab1,db2.tab2 >> $NDB_TOOLS_OUTPUT
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 2 -r     $NDB_BACKUPS-$the_backup_id --exclude-tables db1.tab1,db2.tab2 >> $NDB_TOOLS_OUTPUT


use db1;
--sorted_result
show tables;
# checksum table tab2;
select * from tab2 order by a;

use db2;
--sorted_result
show tables;
# checksum table tab1;
select * from tab1 order by a;

drop table db1.tab2, db2.tab1;

--echo *********************************
--echo Include db1.tab1, but exclude db1
--echo *********************************

--echo Should result in only db1.tab1 being restored
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 1 -r -m  $NDB_BACKUPS-$the_backup_id --exclude-databases db1 --include-tables db1.tab1 >> $NDB_TOOLS_OUTPUT
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 2 -r     $NDB_BACKUPS-$the_backup_id  --exclude-databases db1 --include-tables db1.tab1>> $NDB_TOOLS_OUTPUT

use db1;
--sorted_result
show tables;
# checksum table tab1;
select * from tab1 order by a;

use db2;
--sorted_result
show tables;

drop table db1.tab1;

--echo *********************************
--echo Exclude db1.tab1, but include db1
--echo *********************************
--echo Should result in only db1.tab2 being restored
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 1 -r -m  $NDB_BACKUPS-$the_backup_id  --include-databases db1 --exclude-tables db1.tab1 >> $NDB_TOOLS_OUTPUT
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 2 -r     $NDB_BACKUPS-$the_backup_id  --include-databases db1 --exclude_tables db1.tab1 >> $NDB_TOOLS_OUTPUT

use db1;
--sorted_result
show tables;
# checksum table tab2;
select * from tab2 order by a;

use db2;
--sorted_result
show tables;

drop table db1.tab2;

--echo **************************************
--echo Exclude db1.tab1, and include db1.tab1
--echo **************************************
--echo Should result in db1.tab1 only restored.
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 1 -r -m  $NDB_BACKUPS-$the_backup_id --exclude-tables db1.tab1 --include-tables db1.tab1 >> $NDB_TOOLS_OUTPUT
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 2 -r     $NDB_BACKUPS-$the_backup_id --exclude-tables db1.tab1 --include-tables db1.tab1 >> $NDB_TOOLS_OUTPUT


use db1;
--sorted_result
show tables;
# checksum table tab1;
select * from tab1 order by a;

use db2;
--sorted_result
show tables;

drop table db1.tab1;

--echo ****************************
--echo Exclude db2, and include db2
--echo ****************************
--echo Should result in db2 only restored.
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 1 -r -m  $NDB_BACKUPS-$the_backup_id --exclude-databases db2 --include-databases db2 >> $NDB_TOOLS_OUTPUT
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 2 -r     $NDB_BACKUPS-$the_backup_id --exclude-databases db2 --include-databases db2 >> $NDB_TOOLS_OUTPUT


use db1;
--sorted_result
show tables;
use db2;
--sorted_result
show tables;
# checksum table tab1, tab2;
select * from tab1 order by a;
select * from tab2 order by a;

drop table db2.tab1, db2.tab2;

--echo *********************************
--echo Include db1, and include db2.tab1
--echo *********************************
--echo Should result in all tables in db1 and db2.tab1 being restored.
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 1 -r -m  $NDB_BACKUPS-$the_backup_id --include-databases db1 --include-tables db2.tab1 >> $NDB_TOOLS_OUTPUT
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 2 -r     $NDB_BACKUPS-$the_backup_id --include-databases db1 --include-tables db2.tab1 >> $NDB_TOOLS_OUTPUT


use db1;
--sorted_result
show tables;
use db2;
--sorted_result
show tables;
# checksum table tab1;
select * from tab1 order by a;

drop table db1.tab1,db1.tab2,db2.tab1;


--echo ************************
--echo Include unknown database
--echo ************************
--echo Should result in nothing restored
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 1 -r -m  $NDB_BACKUPS-$the_backup_id --include-databases unhappy_customer >> $NDB_TOOLS_OUTPUT
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 2 -r     $NDB_BACKUPS-$the_backup_id --include-databases unhappy_customer >> $NDB_TOOLS_OUTPUT

show databases like "db%";
use db1;
--sorted_result
show tables;
use db2;
--sorted_result
show tables;

--echo *********************
--echo Exclude unknown table
--echo *********************
--echo Should result in everything restored
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 1 -r -m  $NDB_BACKUPS-$the_backup_id --exclude-tables db1.unhappy_customer >> $NDB_TOOLS_OUTPUT
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 2 -r     $NDB_BACKUPS-$the_backup_id --exclude-tables db1.unhappy_customer >> $NDB_TOOLS_OUTPUT

show databases like "db%";

use db1;
--sorted_result
show tables;
# checksum table tab1, tab2;
select * from tab1 order by a;
select * from tab2 order by a;
use db2;
--sorted_result
show tables;
# checksum table tab1, tab2;
select * from tab1 order by a;
select * from tab2 order by a;

drop table db1.tab1, db1.tab2;
drop table db2.tab1, db2.tab2;

--echo ************************************
--echo Check accumulative include arguments
--echo ************************************
--echo Should result in both db1.tab1 and db1.tab2 restored.
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 1 -r -m  $NDB_BACKUPS-$the_backup_id --include-tables db1.tab1 --include-tables db1.tab2 >> $NDB_TOOLS_OUTPUT
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 2 -r     $NDB_BACKUPS-$the_backup_id --include-tables db1.tab1 --include-tables db1.tab2 >> $NDB_TOOLS_OUTPUT

use db1;
--sorted_result
show tables;
# checksum table tab2;
select * from tab2 order by a;

use db2;
--sorted_result
show tables;

drop table db1.tab1, db1.tab2;

--echo ************************************
--echo Check accumulative exclude arguments
--echo ************************************
--echo Should result in both db2.tab1 and db2.tab2 restored.
--exec $NDB_RESTORE --no-defaults -b $the_backup_id -n 1 -r -m  $NDB_BACKUPS-$the_backup_id --exclude-tables db1.tab1 --exclude-tables db1.tab2 >> $NDB_TOOLS_OUTPUT
--exec $NDB_RESTORE  --no-defaults -b $the_backup_id -n 2 -r     $NDB_BACKUPS-$the_backup_id --exclude-tables db1.tab1 --exclude-tables db1.tab2 >> $NDB_TOOLS_OUTPUT

use db1;
--sorted_result
show tables;
use db2;
--sorted_result
show tables;

drop table db2.tab1, db2.tab2;

drop database db1;
drop database db2;
