Oracle RMAN实现“一键式”表空间TSPITR(2)

启动RMAN,登录后执行recover tablespace操作,指定出恢复时间。注意:RMAN的recover tablespace命令本质上是执行一系列的指令脚本,将利用RMAN恢复过程自动化,无需人工介入。

[oracle@SimpleLinux ~]$ rman nocatalog

Recovery Manager: Release 11.2.0.4.0 - Production on Thu Jun 18 13:48:44 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

RMAN> connect target /

connected to target database: ORA11G (DBID=4239941846)

using target database control file instead of recovery catalog

RMAN>  recover tablespace testtbl until time "to_date('2015-06-18 10:51:19','yyyy-mm-dd hh24:mi:ss')" auxiliary destination '/extend/oradata/aux';

Starting recover at 18-JUN-15

allocated channel: ORA_DISK_1

channel ORA_DISK_1: SID=44 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='DDfe' –在目录中创建出一个虚拟的Oracle实例

--启动实例的参数都准备好了。

initialization parameters used for automatic instance:

db_name=ORA11G

db_unique_name=DDfe_tspitr_ORA11G

compatible=11.2.0.4.0

db_block_size=8192

db_files=200

sga_target=1G

processes=80

db_create_file_dest=/extend/oradata/aux

log_archive_dest_1='location=/extend/oradata/aux'

#No auxiliary parameter file used

starting up automatic instance ORA11G

Oracle instance started

Total System Global Area    1071333376 bytes

Fixed Size                    1369420 bytes

Variable Size                281021108 bytes

Database Buffers            784334848 bytes

Redo Buffers                  4608000 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('2015-06-18 10:51:19','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 18-JUN-15

allocated channel: ORA_AUX_DISK_1

channel ORA_AUX_DISK_1: SID=19 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 /u01/app/fast_recovery_area/ORA11G/autobackup/2015_06_18/o1_mf_s_882701042_br4d3m3c_.bkp

channel ORA_AUX_DISK_1: piece handle=/u01/app/fast_recovery_area/ORA11G/autobackup/2015_06_18/o1_mf_s_882701042_br4d3m3c_.bkp tag=TAG20150618T104402

channel ORA_AUX_DISK_1: restored backup piece 1

channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:02

output file name=/extend/oradata/aux/ORA11G/controlfile/o1_mf_br4q1v2o_.ctl

Finished restore at 18-JUN-15

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('2015-06-18 10:51:19','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 '||  'TESTTBL' ||' 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  5 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  7 to 

"/u01/app/oradata/ORA11G/datafile/o1_mf_testtbl_br48h8wp_.dbf";

# switch all tempfiles

switch clone tempfile all;

# restore the tablespaces in the recovery set and the auxiliary set

restore clone datafile  1, 5, 3, 2, 7;

switch clone datafile all;

}

executing Memory Script

executing command: SET until clause

sql statement: alter tablespace TESTTBL 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 /extend/oradata/aux/ORA11G/datafile/o1_mf_temp_%u_.tmp in control file

Starting restore at 18-JUN-15

using channel ORA_AUX_DISK_1

--只恢复system, sysaux, undo和目标testtbl表空间。

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 /extend/oradata/aux/ORA11G/datafile/o1_mf_system_%u_.dbf

channel ORA_AUX_DISK_1: restoring datafile 00005 to /extend/oradata/aux/ORA11G/datafile/o1_mf_system_%u_.dbf

channel ORA_AUX_DISK_1: restoring datafile 00003 to /extend/oradata/aux/ORA11G/datafile/o1_mf_undotbs1_%u_.dbf

channel ORA_AUX_DISK_1: restoring datafile 00002 to /extend/oradata/aux/ORA11G/datafile/o1_mf_sysaux_%u_.dbf

channel ORA_AUX_DISK_1: restoring datafile 00007 to /u01/app/oradata/ORA11G/datafile/o1_mf_testtbl_br48h8wp_.dbf

channel ORA_AUX_DISK_1: reading from backup piece /u01/app/fast_recovery_area/ORA11G/backupset/2015_06_18/o1_mf_nnndf_TAG20150618T104315_br4d23ty_.bkp

channel ORA_AUX_DISK_1: piece handle=/u01/app/fast_recovery_area/ORA11G/backupset/2015_06_18/o1_mf_nnndf_TAG20150618T104315_br4d23ty_.bkp tag=TAG20150618T104315

channel ORA_AUX_DISK_1: restored backup piece 1

channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:01:05

Finished restore at 18-JUN-15

datafile 1 switched to datafile copy

input datafile copy RECID=5 STAMP=882712324 file name=/extend/oradata/aux/ORA11G/datafile/o1_mf_system_br4q22sy_.dbf

datafile 5 switched to datafile copy

input datafile copy RECID=6 STAMP=882712324 file name=/extend/oradata/aux/ORA11G/datafile/o1_mf_system_br4q230g_.dbf

datafile 3 switched to datafile copy

input datafile copy RECID=7 STAMP=882712324 file name=/extend/oradata/aux/ORA11G/datafile/o1_mf_undotbs1_br4q22z4_.dbf

datafile 2 switched to datafile copy

input datafile copy RECID=8 STAMP=882712324 file name=/extend/oradata/aux/ORA11G/datafile/o1_mf_sysaux_br4q22yx_.dbf

--启动虚拟实例数据,完成恢复。

contents of Memory Script:

{

# set requested point in time

set until  time "to_date('2015-06-18 10:51:19','yyyy-mm-dd hh24:mi:ss')";

# online the datafiles restored or switched

sql clone "alter database datafile  1 online";

sql clone "alter database datafile  5 online";

sql clone "alter database datafile  3 online";

sql clone "alter database datafile  2 online";

sql clone "alter database datafile  7 online";

# recover and open resetlogs

recover clone database tablespace  "TESTTBL", "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  5 online

sql statement: alter database datafile  3 online

sql statement: alter database datafile  2 online

sql statement: alter database datafile  7 online

Starting recover at 18-JUN-15

using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 17 is already on disk as file /u01/app/fast_recovery_area/ORA11G/archivelog/2015_06_18/o1_mf_1_17_br4pw26f_.arc

channel ORA_AUX_DISK_1: starting archived log restore to default destination

channel ORA_AUX_DISK_1: restoring archived log

archived log thread=1 sequence=16

channel ORA_AUX_DISK_1: reading from backup piece /u01/app/fast_recovery_area/ORA11G/backupset/2015_06_18/o1_mf_annnn_TAG20150618T104401_br4d3k9f_.bkp

channel ORA_AUX_DISK_1: piece handle=/u01/app/fast_recovery_area/ORA11G/backupset/2015_06_18/o1_mf_annnn_TAG20150618T104401_br4d3k9f_.bkp tag=TAG20150618T104401

channel ORA_AUX_DISK_1: restored backup piece 1

channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01

archived log file name=/extend/oradata/aux/1_16_846081044.dbf thread=1 sequence=16

channel clone_default: deleting archived log(s)

archived log file name=/extend/oradata/aux/1_16_846081044.dbf RECID=27 STAMP=882712328

archived log file name=/u01/app/fast_recovery_area/ORA11G/archivelog/2015_06_18/o1_mf_1_17_br4pw26f_.arc thread=1 sequence=17

media recovery complete, elapsed time: 00:00:01

Finished recover at 18-JUN-15

database opened

--将表空间导出来

contents of Memory Script:

{

# make read only the tablespace that will be exported

sql clone 'alter tablespace  TESTTBL read only';

# create directory for datapump import

sql "create or replace directory TSPITR_DIROBJ_DPDIR as ''

/extend/oradata/aux''";

# create directory for datapump export

sql clone "create or replace directory TSPITR_DIROBJ_DPDIR as ''

/extend/oradata/aux''";

}

executing Memory Script

sql statement: alter tablespace  TESTTBL read only

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/extend/oradata/aux''

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/extend/oradata/aux''

Performing export of metadata...

EXPDP> Starting "SYS"."TSPITR_EXP_DDfe": 

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/CONSTRAINT/REF_CONSTRAINT

EXPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK

EXPDP> Master table "SYS"."TSPITR_EXP_DDfe" successfully loaded/unloaded

EXPDP> ******************************************************************************

EXPDP> Dump file set for SYS.TSPITR_EXP_DDfe is:

EXPDP>  /extend/oradata/aux/tspitr_DDfe_54026.dmp

EXPDP> ******************************************************************************

EXPDP> Datafiles required for transportable tablespace TESTTBL:

EXPDP>  /u01/app/oradata/ORA11G/datafile/o1_mf_testtbl_br48h8wp_.dbf

EXPDP> Job "SYS"."TSPITR_EXP_DDfe" successfully completed at Thu Jun 18 13:54:10 2015 elapsed 0 00:01:13

Export completed

--关闭虚拟实例

contents of Memory Script:

{

# shutdown clone before import

shutdown clone immediate

# drop target tablespaces before importing them back

sql 'drop tablespace  TESTTBL including contents keep datafiles cascade constraints';

}

executing Memory Script

database closed

database dismounted

Oracle instance shut down

--把原来的表空间删除掉

sql statement: drop tablespace  TESTTBL including contents keep datafiles cascade constraints

--恢复导入数据

Performing import of metadata...

IMPDP> Master table "SYS"."TSPITR_IMP_DDfe" successfully loaded/unloaded

IMPDP> Starting "SYS"."TSPITR_IMP_DDfe": 

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/CONSTRAINT/REF_CONSTRAINT

IMPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK

IMPDP> Job "SYS"."TSPITR_IMP_DDfe" successfully completed at Thu Jun 18 13:54:54 2015 elapsed 0 00:00:10

Import completed

--第三部分脚本,恢复表空间

contents of Memory Script:

{

# make read write and offline the imported tablespaces

sql 'alter tablespace  TESTTBL read write';

sql 'alter tablespace  TESTTBL offline';

# enable autobackups after TSPITR is finished

sql 'begin dbms_backup_restore.AutoBackupFlag(TRUE); end;';

}

executing Memory Script

sql statement: alter tablespace  TESTTBL read write

sql statement: alter tablespace  TESTTBL offline

sql statement: begin dbms_backup_restore.AutoBackupFlag(TRUE); end;

Removing automatic instance

Automatic instance removed

auxiliary instance file /extend/oradata/aux/ORA11G/datafile/o1_mf_temp_br4q4ojl_.tmp deleted

auxiliary instance file /extend/oradata/aux/ORA11G/onlinelog/o1_mf_4_br4q4j17_.log deleted

auxiliary instance file /extend/oradata/aux/ORA11G/onlinelog/o1_mf_3_br4q4ghl_.log deleted

auxiliary instance file /extend/oradata/aux/ORA11G/onlinelog/o1_mf_2_br4q4dt5_.log deleted

auxiliary instance file /extend/oradata/aux/ORA11G/onlinelog/o1_mf_1_br4q4brh_.log deleted

auxiliary instance file /extend/oradata/aux/ORA11G/datafile/o1_mf_sysaux_br4q22yx_.dbf deleted

auxiliary instance file /extend/oradata/aux/ORA11G/datafile/o1_mf_undotbs1_br4q22z4_.dbf deleted

auxiliary instance file /extend/oradata/aux/ORA11G/datafile/o1_mf_system_br4q230g_.dbf deleted

auxiliary instance file /extend/oradata/aux/ORA11G/datafile/o1_mf_system_br4q22sy_.dbf deleted

auxiliary instance file /extend/oradata/aux/ORA11G/controlfile/o1_mf_br4q1v2o_.ctl deleted

Finished recover at 18-JUN-15

RMAN>

上面的步骤可以归纳为如下:

ü  在目录中创建一个虚拟临时数据库实例;

ü  RMAN自动辅助库AUX创建,利用源数据库的备份集合和归档日志进行全库不完全恢复。恢复范围包括system、sysaux、undo和目标表空间;

ü  借助Expdp和Impdp,使用可移动表空间原理步骤,将目标表空间数据导出并导回源数据库;

ü  恢复完成;

下面可以看实际效果。

SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') a from dual;

A

--------------------

2015-06-18 13:55:43

SQL> select group#, sequence# from v$log where status='CURRENT';

GROUP#  SEQUENCE#

---------- ----------

1        20

尝试访问数据表。

SQL> select count(*) from test.emp;

select count(*) from test.emp

ORA-00376: 无法读取数据文件 7

ORA-01110: 数据文件 7: '/u01/app/oradata/ORA11G/datafile/o1_mf_testtbl_br48h8wp_.dbf'

此时,表空间被offline,需要手工进行online操作。

SQL> select tablespace_name, status from dba_tablespaces;

TABLESPACE_NAME                STATUS

------------------------------ ---------

SYSTEM                        ONLINE

SYSAUX                        ONLINE

UNDOTBS1                      ONLINE

TEMP                          ONLINE

USERS                          ONLINE

TESTTBL                        OFFLINE

6 rows selected

SQL> alter tablespace testtbl online;

Tablespace altered

SQL> select tablespace_name, status from dba_tablespaces;

TABLESPACE_NAME                STATUS

------------------------------ ---------

SYSTEM                        ONLINE

SYSAUX                        ONLINE

UNDOTBS1                      ONLINE

TEMP                          ONLINE

USERS                          ONLINE

TESTTBL                        ONLINE

6 rows selected

SQL> select count(*) from test.emp;

COUNT(*)

----------

14

SQL> select count(*) from test.dept;

COUNT(*)

----------

4

5、结论

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/888f9ae8677d5b38cee6a8dc7812bde0.html