Oracle 12c Recover Table New Feature
从Oracle12cR1开始,oracle支持Recover table。在之前的版本中,如果想基于某个时间点恢复表需要关闭数据库做恢复,或者使用flashback技术来闪回。12cR1中的Recover table可以在数据库联机的情况下对数据库的某些表做恢复。
其执行恢复的大致流程如下:
1.确认你需要恢复表的时间点在备份中存在。
2.创建一个辅助的实例(auxiliary database),并且将你指定恢复的表恢复到这个实例里面。
3.使用数据泵(Data Pump)导出指定状态表的数据。
4.使用数据泵导入步骤3中导出的表数据到tartget database(可选项)
5.重名名表或表空间(可选项)
演示过程:
1.全备数据库
RMAN> backup incremental level 0 database format '/RECO/rman/backup_%d%T%s.bak' plus archivelog format '/RECO/rman/arc_%d%T%s.bak';
Starting backup at 17-JUL-13
current log archived
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=269 device type=DISK
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=38 RECID=27 STAMP=821018028
channel ORA_DISK_1: starting piece 1 at 17-JUL-13
channel ORA_DISK_1: finished piece 1 at 17-JUL-13
piece handle=/RECO/rman/arc_DB12C2013071728.bak tag=TAG20130717T123349 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:02
Finished backup at 17-JUL-13
Starting backup at 17-JUL-13
using channel ORA_DISK_1
channel ORA_DISK_1: starting incremental level 0 datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00003 name=/ORADATA/DB12C/datafile/o1_mf_sysaux_8x51lc6k_.dbf
input datafile file number=00001 name=/ORADATA/DB12C/datafile/o1_mf_system_8x51qtpc_.dbf
input datafile file number=00004 name=/ORADATA/DB12C/datafile/o1_mf_undotbs1_8x51wr6g_.dbf
input datafile file number=00002 name=/ORADATA/DB12C/datafile/db12c01.dbf
input datafile file number=00006 name=/ORADATA/DB12C/datafile/o1_mf_users_8x51wp8d_.dbf
channel ORA_DISK_1: starting piece 1 at 17-JUL-13
channel ORA_DISK_1: finished piece 1 at 17-JUL-13
piece handle=/RECO/rman/backup_DB12C2013071729.bak tag=TAG20130717T123351 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:02:17
channel ORA_DISK_1: starting incremental level 0 datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 17-JUL-13
channel ORA_DISK_1: finished piece 1 at 17-JUL-13
piece handle=/RECO/rman/backup_DB12C2013071730.bak tag=TAG20130717T123351 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 17-JUL-13
Starting backup at 17-JUL-13
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=39 RECID=28 STAMP=821018172
channel ORA_DISK_1: starting piece 1 at 17-JUL-13
channel ORA_DISK_1: finished piece 1 at 17-JUL-13
piece handle=/RECO/rman/arc_DB12C2013071731.bak tag=TAG20130717T123612 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 17-JUL-13
2.查询测试数据
2.1查询当前SCN号及系统时间
CURRENT_SCN CURRENT_TIME
----------- -------------------
1932621 2013-07-17 13:10:28
2.2查询当前表数据
SQL> SELECT * FROM DB12C.T;
ID NAME
---------- ----------
1 SZSCKJ.COM
2 SZSCKJ.COM
3.修改测试数据
SQL> INSERT INTO DB12C.T VALUES(3,'SZSCKJ.COM');
1 row created.
SQL> INSERT INTO DB12C.T VALUES(4,'SZSCKJ.COM');
1 row created.
SQL> INSERT INTO DB12C.T VALUES(5,'SZSCKJ.COM');
1 row created.
SQL> INSERT INTO DB12C.T VALUES(6,'SZSCKJ.COM');
1 row created.
SQL> COMMIT;
Commit complete.
SQL> select dbms_flashback.get_system_change_number CURRENT_SCN,to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') CURRENT_TIME FROM DUAL;
CURRENT_SCN CURRENT_TIME
----------- -------------------
1932709 2013-07-17 13:13:05
SQL> SELECT * FROM DB12C.T;
ID NAME
---------- ----------
1 SZSCKJ.COM
2 SZSCKJ.COM
3 SZSCKJ.COM
4 SZSCKJ.COM
5 SZSCKJ.COM
6 SZSCKJ.COM
6 rows selected
4.确认归档模式
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /RECO/arch/DB12c
Oldest online log sequence 38
Next log sequence to archive 40
Current log sequence 40
5.Recover table
##Start RMAN and connect as TARGET to the target database. You must connect as a
user with the SYSBACKUP or SYSDBA privilege.
这边需要注意的是,需要使用具有sysbackup or sysdba的权限登录rman,即
rman target sys/passwd.不能使用rman target /来连接Recovery Manager
oracle@DB12c:/tmp/oracle/recover>rman target sys/oracle
Recovery Manager: Release 12.1.0.1.0 - Production on Wed Jul 17 13:23:35 2013
Copyright (c) 1982, 2013, Oracle and/or its affiliates. All rights reserved.
connected to target database: DB12C (DBID=1273758963)
RMAN> RECOVER TABLE DB12C.T
2> UNTIL SCN 1932621
3> AUXILIARY DESTINATION '/tmp/oracle/recover'
4> REMAP TABLE 'DB12C'.'T':'T_HISTORY_20130717';
Starting recover at 17-JUL-13
current log archived
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=32 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='qpxE' -----这里开始创建auxiliary instance
initialization parameters used for automatic instance:
db_name=DB12C
db_unique_name=qpxE_pitr_DB12C
compatible=12.1.0.0.0
db_block_size=8192
db_files=200
sga_target=1G
processes=80
diagnostic_dest=/u01/app/oracle
db_create_file_dest=/tmp/oracle/recover
log_archive_dest_1='location=/tmp/oracle/recover'
#No auxiliary parameter file used
starting up automatic instance DB12C
Oracle instance started
Total System Global Area 1068937216 bytes
Fixed Size 2296576 bytes
Variable Size 281019648 bytes
Database Buffers 780140544 bytes
Redo Buffers 5480448 bytes
Automatic instance created
contents of Memory Script.:
{
# set requested point in time
set until scn 1932621;
# 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';
}
executing Memory Script
executing command: SET until clause
Starting restore at 17-JUL-13
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=11 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 /RECO/rman/backup_DB12C2013071730.bak
channel ORA_AUX_DISK_1: piece handle=/RECO/rman/backup_DB12C2013071730.bak tag=TAG20130717T123351
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:02
output file name=/tmp/oracle/recover/DB12C/controlfile/o1_mf_8ydd5v1y_.ctl
Finished restore at 17-JUL-13
sql statement: alter database mount clone database
sql statement: alter system archive log current
contents of Memory Script.:
{
# set requested point in time
set until scn 1932621;
# set destinations for recovery set and auxiliary set datafiles
set newname for clone datafile 1 to new;
set newname for clone datafile 4 to new;
set newname for clone datafile 3 to new;
set newname for clone tempfile 1 to new;
# switch all tempfiles
switch clone tempfile all;
# restore the tablespaces in the recovery set and the auxiliary set
restore clone datafile 1, 4, 3;
switch clone datafile all;
}
executing Memory Script
executing command: SET until clause
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
renamed tempfile 1 to /tmp/oracle/recover/DB12C/datafile/o1_mf_temp_%u_.tmp in control file
Starting restore at 17-JUL-13 -----先restore database
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/oracle/recover/DB12C/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00004 to /tmp/oracle/recover/DB12C/datafile/o1_mf_undotbs1_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00003 to /tmp/oracle/recover/DB12C/datafile/o1_mf_sysaux_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /RECO/rman/backup_DB12C2013071729.bak
channel ORA_AUX_DISK_1: piece handle=/RECO/rman/backup_DB12C2013071729.bak tag=TAG20130717T123351
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:03:05
Finished restore at 17-JUL-13
datafile 1 switched to datafile copy
input datafile copy RECID=4 STAMP=821022844 file name=/tmp/oracle/recover/DB12C/datafile/o1_mf_system_8ydd62nz_.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=5 STAMP=821022844 file name=/tmp/oracle/recover/DB12C/datafile/o1_mf_undotbs1_8ydd62o6_.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=6 STAMP=821022844 file name=/tmp/oracle/recover/DB12C/datafile/o1_mf_sysaux_8ydd62ns_.dbf
contents of Memory Script.:
{
# set requested point in time
set until scn 1932621;
# online the datafiles restored or switched
sql clone "alter database datafile 1 online";
sql clone "alter database datafile 4 online";
sql clone "alter database datafile 3 online";
# recover and open database read only
recover clone database tablespace "SYSTEM", "UNDOTBS1", "SYSAUX";
sql clone 'alter database open read only';
}
executing Memory Script
executing command: SET until clause
sql statement: alter database datafile 1 online
sql statement: alter database datafile 4 online
sql statement: alter database datafile 3 online
Starting recover at 17-JUL-13
using channel ORA_AUX_DISK_1
starting media recovery ----再recover
archived log for thread 1 with sequence 39 is already on disk as file /RECO/arch/DB12c/1_39_819734199.dbf
archived log for thread 1 with sequence 40 is already on disk as file /RECO/arch/DB12c/1_40_819734199.dbf
archived log file name=/RECO/arch/DB12c/1_39_819734199.dbf thread=1 sequence=39
archived log file name=/RECO/arch/DB12c/1_40_819734199.dbf thread=1 sequence=40
media recovery complete, elapsed time: 00:00:08
Finished recover at 17-JUL-13
sql statement: alter database open read only
contents of Memory Script.:
{
sql clone "create spfile from memory";
shutdown clone immediate;
startup clone nomount;
sql clone "alter system set control_files =
''/tmp/oracle/recover/DB12C/controlfile/o1_mf_8ydd5v1y_.ctl'' comment=
''RMAN set'' scope=spfile";
shutdown clone immediate;
startup clone nomount;
# mount database
sql clone 'alter database mount clone database';
}
executing Memory Script
sql statement: create spfile from memory
database closed
database dismounted
Oracle instance shut down
connected to auxiliary database (not started)
Oracle instance started
Total System Global Area 1068937216 bytes
Fixed Size 2296576 bytes
Variable Size 285213952 bytes
Database Buffers 775946240 bytes
Redo Buffers 5480448 bytes
sql statement: alter system set control_files = ''/tmp/oracle/recover/DB12C/controlfile/o1_mf_8ydd5v1y_.ctl'' comment= ''RMAN set'' scope=spfile
Oracle instance shut down
connected to auxiliary database (not started)
Oracle instance started
Total System Global Area 1068937216 bytes
Fixed Size 2296576 bytes
Variable Size 285213952 bytes
Database Buffers 775946240 bytes
Redo Buffers 5480448 bytes
sql statement: alter database mount clone database
contents of Memory Script.:
{
# set requested point in time
set until scn 1932621;
# set destinations for recovery set and auxiliary set datafiles
set newname for datafile 2 to new;
# restore the tablespaces in the recovery set and the auxiliary set
restore clone datafile 2;
switch clone datafile all;
}
executing Memory Script
executing command: SET until clause
executing command: SET NEWNAME
Starting restore at 17-JUL-13
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=11 device type=DISK
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 00002 to /tmp/oracle/recover/QPXE_PITR_DB12C/datafile/o1_mf_db12c_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /RECO/rman/backup_DB12C2013071729.bak
channel ORA_AUX_DISK_1: piece handle=/RECO/rman/backup_DB12C2013071729.bak tag=TAG20130717T123351
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:15
Finished restore at 17-JUL-13
datafile 2 switched to datafile copy
input datafile copy RECID=8 STAMP=821022919 file name=/tmp/oracle/recover/QPXE_PITR_DB12C/datafile/o1_mf_db12c_8yddfr1t_.dbf
contents of Memory Script.:
{
# set requested point in time
set until scn 1932621;
# online the datafiles restored or switched
sql clone "alter database datafile 2 online";
# recover and open resetlogs
recover clone database tablespace "DB12C", "SYSTEM", "UNDOTBS1", "SYSAUX" delete archivelog;
alter clone database open resetlogs;
}
executing Memory Script
executing command: SET until clause
sql statement: alter database datafile 2 online
Starting recover at 17-JUL-13
using channel ORA_AUX_DISK_1
starting media recovery
archived log for thread 1 with sequence 39 is already on disk as file /RECO/arch/DB12c/1_39_819734199.dbf
archived log for thread 1 with sequence 40 is already on disk as file /RECO/arch/DB12c/1_40_819734199.dbf
archived log file name=/RECO/arch/DB12c/1_39_819734199.dbf thread=1 sequence=39
archived log file name=/RECO/arch/DB12c/1_40_819734199.dbf thread=1 sequence=40
media recovery complete, elapsed time: 00:00:00
Finished recover at 17-JUL-13
database opened
contents of Memory Script.:
{
# create directory for datapump import -----使用数据泵导出相关数据
sql "create or replace directory TSPITR_DIROBJ_DPDIR as ''
/tmp/oracle/recover''";
# create directory for datapump export
sql clone "create or replace directory TSPITR_DIROBJ_DPDIR as ''
/tmp/oracle/recover''";
}
executing Memory Script
sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/tmp/oracle/recover''
sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/tmp/oracle/recover''
Performing export of tables...
EXPDP> Starting "SYS"."TSPITR_EXP_qpxE_qCee":
EXPDP> Estimate in progress using BLOCKS method...
EXPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
EXPDP> Total estimation using BLOCKS method: 64 KB
EXPDP> Processing object type TABLE_EXPORT/TABLE/TABLE
EXPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
EXPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
EXPDP> . . exported "DB12C"."T" 5.492 KB 2 rows
EXPDP> Master table "SYS"."TSPITR_EXP_qpxE_qCee" successfully loaded/unloaded
EXPDP> ******************************************************************************
EXPDP> Dump file set for SYS.TSPITR_EXP_qpxE_qCee is:
EXPDP> /tmp/oracle/recover/tspitr_qpxE_29472.dmp
EXPDP> Job "SYS"."TSPITR_EXP_qpxE_qCee" successfully completed at Wed Jul 17 13:57:26 2013 elapsed 0 00:00:49
Export completed
contents of Memory Script.:
{
# shutdown clone before import
shutdown clone abort
}
executing Memory Script
Oracle instance shut down
Performing import of tables... -------向target database导入数据
IMPDP> Master table "SYS"."TSPITR_IMP_qpxE_qbDD" successfully loaded/unloaded
IMPDP> Starting "SYS"."TSPITR_IMP_qpxE_qbDD":
IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE
IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
IMPDP> . . imported "DB12C"."T_HISTORY_20130717" 5.492 KB 2 rows
IMPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
IMPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
IMPDP> Job "SYS"."TSPITR_IMP_qpxE_qbDD" successfully completed at Wed Jul 17 13:58:35 2013 elapsed 0 00:00:47
Import completed
Removing automatic instance
Automatic instance removed ------服务还挺贴心,帮你清理好战场
auxiliary instance file /tmp/oracle/recover/DB12C/datafile/o1_mf_temp_8yddddf0_.tmp deleted
auxiliary instance file /tmp/oracle/recover/QPXE_PITR_DB12C/onlinelog/o1_mf_3_8yddgl7j_.log deleted
auxiliary instance file /tmp/oracle/recover/QPXE_PITR_DB12C/onlinelog/o1_mf_2_8yddgg1f_.log deleted
auxiliary instance file /tmp/oracle/recover/QPXE_PITR_DB12C/onlinelog/o1_mf_1_8yddg97b_.log deleted
auxiliary instance file /tmp/oracle/recover/QPXE_PITR_DB12C/datafile/o1_mf_db12c_8yddfr1t_.dbf deleted
auxiliary instance file /tmp/oracle/recover/DB12C/datafile/o1_mf_sysaux_8ydd62ns_.dbf deleted
auxiliary instance file /tmp/oracle/recover/DB12C/datafile/o1_mf_undotbs1_8ydd62o6_.dbf deleted
auxiliary instance file /tmp/oracle/recover/DB12C/datafile/o1_mf_system_8ydd62nz_.dbf deleted
auxiliary instance file /tmp/oracle/recover/DB12C/controlfile/o1_mf_8ydd5v1y_.ctl deleted
auxiliary instance file tspitr_qpxE_29472.dmp deleted
Finished recover at 17-JUL-13
6.确认数据
SQL> select * from DB12C.T_HISTORY_20130717;
ID NAME
---------- ----------
1 SZSCKJ.COM
2 SZSCKJ.COM
总结:这个新特性好处不用说,弊端就是实质上是在你的系统里面再起一个实例来做不完全恢复,然后再给你导入到target database里面,对资源的消耗比较厉害。
By Guanghui_Zhou
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26169542/viewspace-766487/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/26169542/viewspace-766487/