八、演示表空间时点恢复
-- 演示环境 SQL> select * from v$version where rownum<2; BANNER -------------------------------------------------------------------------------- Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production SQL> select 'Leshami' Author,'http://blog.csdn.net/leshami' Blog, 2 '645746311' QQ from dual; AUTHOR BLOG QQ ------- ---------------------------- --------- Leshami http://blog.csdn.net/leshami 645746311 --查看数据库归档模式 SQL> select log_mode from v$database; LOG_MODE ------------ ARCHIVELOG --创建2个用于测试TSPITR的表空间 SQL> create tablespace tbs_tspitr1 datafile '/app/oracle/ora11g/oradata/ora11g/tbs_tspitr1.dbf' size 20m; SQL> create tablespace tbs_tspitr2 datafile '/app/oracle/ora11g/oradata/ora11g/tbs_tspitr2.dbf' size 20m; --创建测试用户并授权 SQL> create user usr_tbspitr identified by usr_tbspitr; SQL> grant dba to usr_tbspitr; SQL> conn usr_tbspitr/usr_tbspitr --创建用于测试的表,将数据存放到tbs_tspitr1表空间 SQL> create table tb_emp tablespace tbs_tspitr1 as select * from scott.emp; --将索引存放到表空间tbs_tspitr2上 SQL> create unique index pk_tb_emp on tb_emp(empno) tablespace tbs_tspitr2; SQL> alter table tb_emp add primary key(empno) using index pk_tb_emp; SQL> select count(*) from tb_emp; COUNT(*) ---------- 14 --准备辅助实例环境及备份数据库 $ mkdir /tmp/tempbak $ rman target / RMAN> configure controlfile autobackup on; RMAN> backup database format '/tmp/tempbak/db_%d_full_%U' plus archivelog; SQL> set time on; --此时turncate表tb_emp 14:17:05 SQL> truncate table tb_emp; SQL> set time off; --在表空间tbs_tspitr2上创建另外的一张表tb_dept SQL> create table tb_dept tablespace tbs_tspitr2 as select * from scott.dept; SQL> select count(*) from tb_dept; COUNT(*) ---------- 4 --添加索引 SQL> create unique index pk_tb_dept on tb_dept(deptno) tablespace tbs_tspitr2; --添加主键约束 SQL> alter table tb_dept add primary key(deptno) using index pk_tb_dept; --为表tb_emp添加外键 SQL> alter table tb_emp add constraint fk_tb_dept foreign key(deptno) 2 > references tb_dept(deptno); $ mkdir -pv /tmp/aux RMAN> recover tablespace tbs_tspitr1 until time "to_date( '2017-04-19 14:17:05', 'yyyy-mm-dd hh24:mi:ss')" auxiliary destination '/tmp/aux'; --提示违反约束,如下 Running TRANSPORT_SET_CHECK on recovery set tablespaces The following errors need to be fixed before peforming this command Violation: ORA-39906: Constraint FK_TB_DEPT between table USR_TBSPITR.TB_DEPT in tablespace TBS_TSPITR2 and table USR_TBSPITR.TB_EMP in tablespace TBS_TSPITR1. Violation: ORA-39908: Index USR_TBSPITR.PK_TB_EMP in tablespace TBS_TSPITR2 enforces primary constraints of table USR_TBSPITR.TB_EMP in tablespace TBS_TSPITR1. --下面使用DBMS_TTS检查自包含 SQL> conn / as sysdba SQL> BEGIN 2 DBMS_TTS.TRANSPORT_SET_CHECK('TBS_TSPITR1', TRUE,TRUE); 3 END; 4 / PL/SQL procedure successfully completed. --下面的错误提示跟在RMAN执行时一致 SQL> SELECT * FROM TRANSPORT_SET_VIOLATIONS; VIOLATIONS -------------------------------------------------------------------------------- ORA-39906: Constraint FK_TB_DEPT between table USR_TBSPITR.TB_DEPT in tablespace TBS_TSPITR2 and table USR_TBSPITR.TB_EMP in tablespace TBS_TSPITR1. ORA-39908: Index USR_TBSPITR.PK_TB_EMP in tablespace TBS_TSPITR2 enforces primary constraints of table USR_TBSPITR.TB_EMP in tablespace TBS_TSPITR1. --下面将TBS_TSPITR2表空间也增加到自包含检查中 SQL> BEGIN 2 DBMS_TTS.TRANSPORT_SET_CHECK('TBS_TSPITR1,TBS_TSPITR2', TRUE,TRUE); 3 END; 4 / PL/SQL procedure successfully completed. --下面的查询结果为空,也就是上面的2个表空同时做时点恢复具备成功的可能 SQL> SELECT * FROM TRANSPORT_SET_VIOLATIONS; no rows selected --将表空间tbs_tspitr2也加入到时点恢复中,再次执行rman RMAN> recover tablespace tbs_tspitr1,tbs_tspitr2 2> until time "to_date( '2017-04-19 14:17:05', 'yyyy-mm-dd hh24:mi:ss')" 3> auxiliary destination '/tmp/aux'; Starting recover at 19-APR-17 using target database control file instead of recovery catalog allocated channel: ORA_DISK_1 channel ORA_DISK_1: SID=162 device type=DISK RMAN-05026: WARNING: presuming following set of tablespaces applies to specified point-in-time List of tablespaces expected to have UNDO segments Tablespace SYSTEM Tablespace UNDOTBS1 Creating automatic instance, with SID='bjmm' --自动创建一个辅助实例 --以下是辅助实例的初始化参数 initialization parameters used for automatic instance: db_name=ORA11G db_unique_name=bjmm_tspitr_ORA11G compatible=11.2.0.0.0 db_block_size=8192 db_files=200 sga_target=280M processes=50 db_create_file_dest=/tmp/aux log_archive_dest_1='location=/tmp/aux' #No auxiliary parameter file used --启动辅助实例 starting up automatic instance ORA11G Oracle instance started Total System Global Area 292278272 bytes Fixed Size 2227744 bytes Variable Size 100663776 bytes Database Buffers 184549376 bytes Redo Buffers 4837376 bytes Automatic instance created Running TRANSPORT_SET_CHECK on recovery set tablespaces TRANSPORT_SET_CHECK completed successfully --执行脚本,还原控制文件, contents of Memory Script: { # set requested point in time set until time "to_date( '2017-04-19 14:17:05', 'yyyy-mm-dd hh24:mi:ss')"; # restore the controlfile restore clone controlfile; # mount the controlfile sql clone 'alter database mount clone database'; # archive current online log sql 'alter system archive log current'; # avoid unnecessary autobackups for structural changes during TSPITR sql 'begin dbms_backup_restore.AutoBackupFlag(FALSE); end;'; } executing Memory Script executing command: SET until clause Starting restore at 19-APR-17 allocated channel: ORA_AUX_DISK_1 channel ORA_AUX_DISK_1: SID=42 device type=DISK channel ORA_AUX_DISK_1: starting datafile backup set restore channel ORA_AUX_DISK_1: restoring control file channel ORA_AUX_DISK_1: reading from backup piece /app/oracle/ora11g/fast_recovery_area /ORA11G/autobackup/2017_04_19/o1_mf_s_941724971_dhg05dw1_.bkp channel ORA_AUX_DISK_1: piece handle=/app/oracle/ora11g/fast_recovery_area/ORA11G/autobackup /2017_04_19/o1_mf_s_941724971_dhg05dw1_.bkp tag=TAG20170419T141611 channel ORA_AUX_DISK_1: restored backup piece 1 channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01 output file name=/tmp/aux/ORA11G/controlfile/o1_mf_dhg48bwc_.ctl Finished restore at 19-APR-17 sql statement: alter database mount clone database sql statement: alter system archive log current sql statement: begin dbms_backup_restore.AutoBackupFlag(FALSE); end; --设置不完全恢复的时间点,并还原数据文件 contents of Memory Script: { # set requested point in time set until time "to_date( '2017-04-19 14:17:05', 'yyyy-mm-dd hh24:mi:ss')"; plsql <<<-- tspitr_2 declare sqlstatement varchar2(512); offline_not_needed exception; pragma exception_init(offline_not_needed, -01539); begin sqlstatement := 'alter tablespace '|| 'TBS_TSPITR1' ||' offline immediate'; krmicd.writeMsg(6162, sqlstatement); krmicd.execSql(sqlstatement); exception when offline_not_needed then null; end; >>>; plsql <<<-- tspitr_2 declare sqlstatement varchar2(512); offline_not_needed exception; pragma exception_init(offline_not_needed, -01539); begin sqlstatement := 'alter tablespace '|| 'TBS_TSPITR2' ||' offline immediate'; krmicd.writeMsg(6162, sqlstatement); krmicd.execSql(sqlstatement); exception when offline_not_needed then null; end; >>>; # set destinations for recovery set and auxiliary set datafiles set newname for clone datafile 1 to new; set newname for clone datafile 3 to new; set newname for clone datafile 2 to new; set newname for clone tempfile 1 to new; set newname for datafile 6 to "/app/oracle/ora11g/oradata/ora11g/tbs_tspitr1.dbf"; set newname for datafile 7 to "/app/oracle/ora11g/oradata/ora11g/tbs_tspitr2.dbf"; # switch all tempfiles switch clone tempfile all; # restore the tablespaces in the recovery set and the auxiliary set restore clone datafile 1, 3, 2, 6, 7; switch clone datafile all; } executing Memory Script executing command: SET until clause sql statement: alter tablespace TBS_TSPITR1 offline immediate sql statement: alter tablespace TBS_TSPITR2 offline immediate executing command: SET NEWNAME executing command: SET NEWNAME executing command: SET NEWNAME executing command: SET NEWNAME executing command: SET NEWNAME executing command: SET NEWNAME renamed tempfile 1 to /tmp/aux/ORA11G/datafile/o1_mf_temp_%u_.tmp in control file Starting restore at 19-APR-17 using channel ORA_AUX_DISK_1 channel ORA_AUX_DISK_1: starting datafile backup set restore channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set channel ORA_AUX_DISK_1: restoring datafile 00001 to /tmp/aux/ORA11G/datafile/o1_mf_system_%u_.dbf channel ORA_AUX_DISK_1: restoring datafile 00003 to /tmp/aux/ORA11G/datafile/o1_mf_undotbs1_%u_.dbf channel ORA_AUX_DISK_1: restoring datafile 00002 to /tmp/aux/ORA11G/datafile/o1_mf_sysaux_%u_.dbf channel ORA_AUX_DISK_1: restoring datafile 00006 to /app/oracle/ora11g/oradata/ora11g/tbs_tspitr1.dbf channel ORA_AUX_DISK_1: restoring datafile 00007 to /app/oracle/ora11g/oradata/ora11g/tbs_tspitr2.dbf channel ORA_AUX_DISK_1: reading from backup piece /tmp/tempbak/db_ORA11G_full_02s2358q_1_1 channel ORA_AUX_DISK_1: piece handle=/tmp/tempbak/db_ORA11G_full_02s2358q_1_1 tag=TAG20170419T141554 channel ORA_AUX_DISK_1: restored backup piece 1 channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:15 Finished restore at 19-APR-17 datafile 1 switched to datafile copy input datafile copy RECID=5 STAMP=941729186 file name=/tmp/aux/ORA11G/datafile/o1_mf_system_dhg48m17_.dbf datafile 3 switched to datafile copy input datafile copy RECID=6 STAMP=941729186 file name=/tmp/aux/ORA11G/datafile/o1_mf_undotbs1_dhg48m2y_.dbf datafile 2 switched to datafile copy input datafile copy RECID=7 STAMP=941729186 file name=/tmp/aux/ORA11G/datafile/o1_mf_sysaux_dhg48m25_.dbf --将辅助实例相关数据文件联机,做介质恢复,最后以resetlogs方式打开数据库 contents of Memory Script: { # set requested point in time set until time "to_date( '2017-04-19 14:17:05', 'yyyy-mm-dd hh24:mi:ss')"; # online the datafiles restored or switched sql clone "alter database datafile 1 online"; sql clone "alter database datafile 3 online"; sql clone "alter database datafile 2 online"; sql clone "alter database datafile 6 online"; sql clone "alter database datafile 7 online"; # recover and open resetlogs recover clone database tablespace "TBS_TSPITR1", "TBS_TSPITR2", "SYSTEM", "UNDOTBS1", "SYSAUX" delete archivelog; alter clone database open resetlogs; } executing Memory Script executing command: SET until clause sql statement: alter database datafile 1 online sql statement: alter database datafile 3 online sql statement: alter database datafile 2 online sql statement: alter database datafile 6 online sql statement: alter database datafile 7 online Starting recover at 19-APR-17 using channel ORA_AUX_DISK_1 starting media recovery archived log for thread 1 with sequence 8 is already on disk as file /app/oracle/ora11g/fast_recovery_area/ORA11G/archivelog/2017_04_19/o1_mf_1_8_dhg05b87_.arc archived log for thread 1 with sequence 9 is already on disk as file /app/oracle/ora11g/fast_recovery_area/ORA11G/archivelog/2017_04_19/o1_mf_1_9_dhg48kow_.arc archived log file name=/app/oracle/ora11g/fast_recovery_area/ORA11G /archivelog/2017_04_19/o1_mf_1_8_dhg05b87_.arc thread=1 sequence=8 archived log file name=/app/oracle/ora11g/fast_recovery_area/ORA11G /archivelog/2017_04_19/o1_mf_1_9_dhg48kow_.arc thread=1 sequence=9 media recovery complete, elapsed time: 00:00:01 Finished recover at 19-APR-17 database opened contents of Memory Script: { # make read only the tablespace that will be exported sql clone 'alter tablespace TBS_TSPITR1 read only'; sql clone 'alter tablespace TBS_TSPITR2 read only'; # create directory for datapump import sql "create or replace directory TSPITR_DIROBJ_DPDIR as '' /tmp/aux''"; # create directory for datapump export sql clone "create or replace directory TSPITR_DIROBJ_DPDIR as '' /tmp/aux''"; } executing Memory Script sql statement: alter tablespace TBS_TSPITR1 read only sql statement: alter tablespace TBS_TSPITR2 read only sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/tmp/aux'' sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/tmp/aux'' --基于上面创建的目录实现表空间传输导出到dump文件 Performing export of metadata... EXPDP> Starting "SYS"."TSPITR_EXP_bjmm": EXPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK EXPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE EXPDP> Processing object type TRANSPORTABLE_EXPORT/INDEX/INDEX EXPDP> Processing object type TRANSPORTABLE_EXPORT/CONSTRAINT/CONSTRAINT EXPDP> Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS EXPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK EXPDP> Master table "SYS"."TSPITR_EXP_bjmm" successfully loaded/unloaded EXPDP> ****************************************************************************** EXPDP> Dump file set for SYS.TSPITR_EXP_bjmm is: EXPDP> /tmp/aux/tspitr_bjmm_39061.dmp EXPDP> ****************************************************************************** EXPDP> Datafiles required for transportable tablespace TBS_TSPITR1: EXPDP> /app/oracle/ora11g/oradata/ora11g/tbs_tspitr1.dbf EXPDP> Datafiles required for transportable tablespace TBS_TSPITR2: EXPDP> /app/oracle/ora11g/oradata/ora11g/tbs_tspitr2.dbf EXPDP> Job "SYS"."TSPITR_EXP_bjmm" successfully completed at 15:26:53 Export completed --删除目标数据库源表空间 contents of Memory Script: { # shutdown clone before import shutdown clone immediate # drop target tablespaces before importing them back sql 'drop tablespace TBS_TSPITR1 including contents keep datafiles'; sql 'drop tablespace TBS_TSPITR2 including contents keep datafiles'; } executing Memory Script database closed database dismounted Oracle instance shut down sql statement: drop tablespace TBS_TSPITR1 including contents keep datafiles sql statement: drop tablespace TBS_TSPITR2 including contents keep datafiles --导入表空间 Performing import of metadata... IMPDP> Master table "SYS"."TSPITR_IMP_bjmm" successfully loaded/unloaded IMPDP> Starting "SYS"."TSPITR_IMP_bjmm": IMPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK IMPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE IMPDP> Processing object type TRANSPORTABLE_EXPORT/INDEX/INDEX IMPDP> Processing object type TRANSPORTABLE_EXPORT/CONSTRAINT/CONSTRAINT IMPDP> Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS IMPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK IMPDP> Job "SYS"."TSPITR_IMP_bjmm" successfully completed at 15:27:15 Import completed --将表空间联机然后迅速脱机 contents of Memory Script: { # make read write and offline the imported tablespaces sql 'alter tablespace TBS_TSPITR1 read write'; sql 'alter tablespace TBS_TSPITR1 offline'; sql 'alter tablespace TBS_TSPITR2 read write'; sql 'alter tablespace TBS_TSPITR2 offline'; # enable autobackups after TSPITR is finished sql 'begin dbms_backup_restore.AutoBackupFlag(TRUE); end;'; } executing Memory Script sql statement: alter tablespace TBS_TSPITR1 read write sql statement: alter tablespace TBS_TSPITR1 offline sql statement: alter tablespace TBS_TSPITR2 read write sql statement: alter tablespace TBS_TSPITR2 offline sql statement: begin dbms_backup_restore.AutoBackupFlag(TRUE); end; --移除辅助实例产生的相关文件,完成时点恢复 Removing automatic instance Automatic instance removed auxiliary instance file /tmp/aux/ORA11G/datafile/o1_mf_temp_dhg499bd_.tmp deleted auxiliary instance file /tmp/aux/ORA11G/onlinelog/o1_mf_3_dhg497cw_.log deleted auxiliary instance file /tmp/aux/ORA11G/onlinelog/o1_mf_2_dhg496fz_.log deleted auxiliary instance file /tmp/aux/ORA11G/onlinelog/o1_mf_1_dhg495d4_.log deleted auxiliary instance file /tmp/aux/ORA11G/datafile/o1_mf_sysaux_dhg48m25_.dbf deleted auxiliary instance file /tmp/aux/ORA11G/datafile/o1_mf_undotbs1_dhg48m2y_.dbf deleted auxiliary instance file /tmp/aux/ORA11G/datafile/o1_mf_system_dhg48m17_.dbf deleted auxiliary instance file /tmp/aux/ORA11G/controlfile/o1_mf_dhg48bwc_.ctl deleted Finished recover at 19-APR-17 --备份一下时点恢复后的表空调 RMAN> backup tablespace tbs_tspitr1,tbs_tspitr2 format '/tmp/tempbak/tbs_%d_%U'; -- RMAN时点恢复期间,可以查看到辅助实例的bjmm pmon进程 $ ps -ef|grep pmon oracle 16911 1 0 15:25 ? 00:00:00 ora_pmon_bjmm oracle 22747 1 0 09:03 ? 00:00:03 ora_pmon_ora11g --辅助实例目录下产生了相关文件 $ ls -hltr /tmp/aux total 8.0K drwxr-x---. 5 oracle oinstall 4.0K Apr 19 15:26 ORA11G -rw-r-----. 1 oracle oinstall 4.0K Apr 19 15:26 tspitr_bjmm_39061.dmp --查看还原后的表空间 SQL> select tablespace_name,status from dba_tablespaces where tablespace_name like '%TSPI%'; TABLESPACE_NAME STATUS ------------------------------ --------- TBS_TSPITR1 OFFLINE TBS_TSPITR2 OFFLINE --将其联机 SQL> alter tablespace TBS_TSPITR1 online; SQL> alter tablespace TBS_TSPITR2 online; SQL> conn usr_tbspitr/usr_tbspitr --验证结果,truncate的表tb_emp被找回 SQL> select count(*) from tb_emp; COUNT(*) ---------- 14 --通过下面的查询可以得知,表空间TBS_TSPITR2上的对象丢失 SQL> select count(*) from tb_dept; select count(*) from tb_dept * ERROR at line 1: ORA-00942: table or view does not exist |