12c 新特性之单表恢复

时间:2020-12-26 21:40:40

首先要有个数据库完全备份

RMAN> list backup;


List of Backup Sets
===================


BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
1       Full    1.11G      DISK        00:01:41     23-JUN-16      
        BP Key: 1   Status: AVAILABLE  Compressed: NO  Tag: TAG20160623T202948
        Piece Name: /u02/app/oracle/fra/ORCL2/backupset/2016_06_23/o1_mf_nnndf_TAG20160623T202948_cpqokwwn_.bkp
  List of Datafiles in backup set 1
  File LV Type Ckp SCN    Ckp Time  Name
  ---- -- ---- ---------- --------- ----
  1       Full 2158832    23-JUN-16 /u02/app/oracle/orcl2/ORCL2/datafile/o1_mf_system_cpo3tvhx_.dbf
  3       Full 2158832    23-JUN-16 /u02/app/oracle/orcl2/ORCL2/datafile/o1_mf_sysaux_cpo3rt7b_.dbf
  4       Full 2158832    23-JUN-16 /u02/app/oracle/orcl2/ORCL2/datafile/o1_mf_undotbs1_cpo3wy5w_.dbf
  6       Full 2158832    23-JUN-16 /u02/app/oracle/orcl2/ORCL2/datafile/o1_mf_users_cpo3wwsy_.dbf

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
2       Full    596.79M    DISK        00:00:35     23-JUN-16      
        BP Key: 2   Status: AVAILABLE  Compressed: NO  Tag: TAG20160623T202948
        Piece Name: /u02/app/oracle/fra/ORCL2/35DECF1E89340D51E053C8C4A8C0F458/backupset/2016_06_23/o1_mf_nnndf_TAG20160623T202948_cpqoojb6_.bkp
  List of Datafiles in backup set 2
  Container ID: 3, PDB Name: ORCL2PDB
  File LV Type Ckp SCN    Ckp Time  Name
  ---- -- ---- ---------- --------- ----
  8       Full 2158067    23-JUN-16 /u02/app/oracle/orcl2/ORCL2/35DECF1E89340D51E053C8C4A8C0F458/datafile/o1_mf_system_cpo4636q_.dbf
  9       Full 2158067    23-JUN-16 /u02/app/oracle/orcl2/ORCL2/35DECF1E89340D51E053C8C4A8C0F458/datafile/o1_mf_sysaux_cpo46379_.dbf
  10      Full 2158067    23-JUN-16 /u02/app/oracle/orcl2/ORCL2/35DECF1E89340D51E053C8C4A8C0F458/datafile/o1_mf_users_cpo472hm_.dbf

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
3       Full    596.74M    DISK        00:00:23     23-JUN-16      
        BP Key: 3   Status: AVAILABLE  Compressed: NO  Tag: TAG20160623T202948
        Piece Name: /u02/app/oracle/fra/ORCL2/35DF89C6D1B814BAE053C8C4A8C09E79/backupset/2016_06_23/o1_mf_nnndf_TAG20160623T202948_cpqopxm4_.bkp
  List of Datafiles in backup set 3
  Container ID: 4, PDB Name: SALESPDB
  File LV Type Ckp SCN    Ckp Time  Name
  ---- -- ---- ---------- --------- ----
  11      Full 1949411    23-JUN-16 /u02/app/oracle/orcl2/ORCL2/35DF89C6D1B814BAE053C8C4A8C09E79/datafile/o1_mf_system_cpo77y1q_.dbf
  12      Full 1949411    23-JUN-16 /u02/app/oracle/orcl2/ORCL2/35DF89C6D1B814BAE053C8C4A8C09E79/datafile/o1_mf_sysaux_cpo77y22_.dbf
  13      Full 1949411    23-JUN-16 /u02/app/oracle/orcl2/ORCL2/35DF89C6D1B814BAE053C8C4A8C09E79/datafile/o1_mf_users_cpo77y24_.dbf

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
4       Full    594.30M    DISK        00:00:28     23-JUN-16      
        BP Key: 4   Status: AVAILABLE  Compressed: NO  Tag: TAG20160623T202948
        Piece Name: /u02/app/oracle/fra/ORCL2/35DEC3F899680A82E053C8C4A8C0792A/backupset/2016_06_23/o1_mf_nnndf_TAG20160623T202948_cpqor18l_.bkp
  List of Datafiles in backup set 4
  Container ID: 2, PDB Name: PDB$SEED
  File LV Type Ckp SCN    Ckp Time  Name
  ---- -- ---- ---------- --------- ----
  5       Full 1602940    22-JUN-16 /u02/app/oracle/orcl2/ORCL2/datafile/o1_mf_system_cpo3yld2_.dbf
  7       Full 1602940    22-JUN-16 /u02/app/oracle/orcl2/ORCL2/datafile/o1_mf_sysaux_cpo3ylcs_.dbf

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
5       Full    17.20M     DISK        00:00:01     23-JUN-16      
        BP Key: 5   Status: AVAILABLE  Compressed: NO  Tag: TAG20160623T203340
        Piece Name: /u02/app/oracle/fra/ORCL2/autobackup/2016_06_23/o1_mf_s_915309220_cpqos5jo_.bkp
  SPFILE Included: Modification time: 23-JUN-16
  SPFILE db_unique_name: ORCL2
  Control File Included: Ckp SCN: 2158935      Ckp time: 23-JUN-16

查询故障时间点

 

   SQL> alter session set nls_date_format='yyyymmdd hh24:mi:ss';

Session altered.


SQL> select sysdate from dual; SYSDATE ----------------- 20160623 20:43:02 SQL> drop table t1; Table dropped. SQL> select timestamp_to_scn(to_timestamp('20160623 20:43:02','yyyymmdd hh24:mi:ss')) from dual; TIMESTAMP_TO_SCN(TO_TIMESTAMP('2016062320:43:02','YYYYMMDDHH24:MI:SS')) ----------------------------------------------------------------------- 2159581

模拟数据库误操作:

SQL> drop table t1;

Table dropped.

数据库恢复过程,一条命令搞定,过程大概是创建数据库辅助实例,恢复数据库备份到辅助实例,使用数据泵以scn为时间点导出表,再使用impdp工具导入到产品库。

RMAN> recover table wxc.t1 of pluggable database orcl2pdb until scn 2159581 auxiliary destination '/tmp/' datapump destination '/tmp';

Starting recover at 23-JUN-16
using channel ORA_DISK_1
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='EDud'

initialization parameters used for automatic instance:
db_name=ORCL2
db_unique_name=EDud_pitr_orcl2pdb_ORCL2
compatible=12.1.0.2.0
db_block_size=8192
db_files=200
diagnostic_dest=/u01/app/oracle
_system_trig_enabled=FALSE
sga_target=1552M
processes=200
db_create_file_dest=/tmp/
log_archive_dest_1='location=/tmp/'
enable_pluggable_database=true
_clone_one_pdb_recovery=true
#No auxiliary parameter file used


starting up automatic instance ORCL2

Oracle instance started

Total System Global Area    1627389952 bytes

Fixed Size                     2924976 bytes
Variable Size                419434064 bytes
Database Buffers            1191182336 bytes
Redo Buffers                  13848576 bytes
Automatic instance created

contents of Memory Script:
{
# set requested point in time
set until  scn 2159581;
# 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 23-JUN-16
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=6 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 /u02/app/oracle/fra/ORCL2/autobackup/2016_06_23/o1_mf_s_915309220_cpqos5jo_.bkp
channel ORA_AUX_DISK_1: piece handle=/u02/app/oracle/fra/ORCL2/autobackup/2016_06_23/o1_mf_s_915309220_cpqos5jo_.bkp tag=TAG20160623T203340
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/ORCL2/controlfile/o1_mf_cpqq5qwd_.ctl
Finished restore at 23-JUN-16

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 2159581;
# 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 datafile  8 to new;
set newname for clone datafile  9 to new;
set newname for clone tempfile  1 to new;
set newname for clone tempfile  3 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, 8, 9;
 
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

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

renamed tempfile 1 to /tmp/ORCL2/datafile/o1_mf_temp_%u_.tmp in control file
renamed tempfile 3 to /tmp/ORCL2/datafile/o1_mf_temp_%u_.tmp in control file

Starting restore at 23-JUN-16
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/ORCL2/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00004 to /tmp/ORCL2/datafile/o1_mf_undotbs1_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00003 to /tmp/ORCL2/datafile/o1_mf_sysaux_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u02/app/oracle/fra/ORCL2/backupset/2016_06_23/o1_mf_nnndf_TAG20160623T202948_cpqokwwn_.bkp
channel ORA_AUX_DISK_1: piece handle=/u02/app/oracle/fra/ORCL2/backupset/2016_06_23/o1_mf_nnndf_TAG20160623T202948_cpqokwwn_.bkp tag=TAG20160623T202948
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:46
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 00008 to /tmp/ORCL2/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00009 to /tmp/ORCL2/datafile/o1_mf_sysaux_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u02/app/oracle/fra/ORCL2/35DECF1E89340D51E053C8C4A8C0F458/backupset/2016_06_23/o1_mf_nnndf_TAG20160623T202948_cpqoojb6_.bkp
channel ORA_AUX_DISK_1: piece handle=/u02/app/oracle/fra/ORCL2/35DECF1E89340D51E053C8C4A8C0F458/backupset/2016_06_23/o1_mf_nnndf_TAG20160623T202948_cpqoojb6_.bkp tag=TAG20160623T202948
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:25
Finished restore at 23-JUN-16

datafile 1 switched to datafile copy
input datafile copy RECID=8 STAMP=915310724 file name=/tmp/ORCL2/datafile/o1_mf_system_cpqq5xym_.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=9 STAMP=915310724 file name=/tmp/ORCL2/datafile/o1_mf_undotbs1_cpqq5xz1_.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=10 STAMP=915310724 file name=/tmp/ORCL2/datafile/o1_mf_sysaux_cpqq5xyw_.dbf
datafile 8 switched to datafile copy
input datafile copy RECID=11 STAMP=915310724 file name=/tmp/ORCL2/datafile/o1_mf_system_cpqq7cgq_.dbf
datafile 9 switched to datafile copy
input datafile copy RECID=12 STAMP=915310724 file name=/tmp/ORCL2/datafile/o1_mf_sysaux_cpqq7cg6_.dbf

contents of Memory Script:
{
# set requested point in time
set until  scn 2159581;
# 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";
sql clone 'ORCL2PDB' "alter database datafile 
 8 online";
sql clone 'ORCL2PDB' "alter database datafile 
 9 online";
# recover and open database read only
recover clone database tablespace  "SYSTEM", "UNDOTBS1", "SYSAUX", "ORCL2PDB":"SYSTEM", "ORCL2PDB":"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

sql statement: alter database datafile  8 online

sql statement: alter database datafile  9 online

Starting recover at 23-JUN-16
using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 14 is already on disk as file /u02/app/oracle/fra/ORCL2/archivelog/2016_06_23/o1_mf_1_14_cpqq0t9z_.arc
archived log file name=/u02/app/oracle/fra/ORCL2/archivelog/2016_06_23/o1_mf_1_14_cpqq0t9z_.arc thread=1 sequence=14
media recovery complete, elapsed time: 00:00:01
Finished recover at 23-JUN-16

sql statement: alter database open read only

contents of Memory Script:
{
sql clone 'alter pluggable database  ORCL2PDB open read only';
}
executing Memory Script

sql statement: alter pluggable database  ORCL2PDB 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/ORCL2/controlfile/o1_mf_cpqq5qwd_.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    1627389952 bytes

Fixed Size                     2924976 bytes
Variable Size                436211280 bytes
Database Buffers            1174405120 bytes
Redo Buffers                  13848576 bytes

sql statement: alter system set  control_files =   ''/tmp/ORCL2/controlfile/o1_mf_cpqq5qwd_.ctl'' comment= ''RMAN set'' scope=spfile

Oracle instance shut down

connected to auxiliary database (not started)
Oracle instance started

Total System Global Area    1627389952 bytes

Fixed Size                     2924976 bytes
Variable Size                436211280 bytes
Database Buffers            1174405120 bytes
Redo Buffers                  13848576 bytes

sql statement: alter database mount clone database

contents of Memory Script:
{
# set requested point in time
set until  scn 2159581;
# set destinations for recovery set and auxiliary set datafiles
set newname for datafile  10 to new;
# restore the tablespaces in the recovery set and the auxiliary set
restore clone datafile  10;
 
switch clone datafile all;
}
executing Memory Script

executing command: SET until clause

executing command: SET NEWNAME

Starting restore at 23-JUN-16
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=6 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 00010 to /tmp/EDUD_PITR_ORCL2PDB_ORCL2/datafile/o1_mf_users_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u02/app/oracle/fra/ORCL2/35DECF1E89340D51E053C8C4A8C0F458/backupset/2016_06_23/o1_mf_nnndf_TAG20160623T202948_cpqoojb6_.bkp
channel ORA_AUX_DISK_1: piece handle=/u02/app/oracle/fra/ORCL2/35DECF1E89340D51E053C8C4A8C0F458/backupset/2016_06_23/o1_mf_nnndf_TAG20160623T202948_cpqoojb6_.bkp tag=TAG20160623T202948
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01
Finished restore at 23-JUN-16

datafile 10 switched to datafile copy
input datafile copy RECID=14 STAMP=915310782 file name=/tmp/EDUD_PITR_ORCL2PDB_ORCL2/datafile/o1_mf_users_cpqq9xdn_.dbf

contents of Memory Script:
{
# set requested point in time
set until  scn 2159581;
# online the datafiles restored or switched
sql clone 'ORCL2PDB' "alter database datafile 
 10 online";
# recover and open resetlogs
recover clone database tablespace  "ORCL2PDB":"USERS", "SYSTEM", "UNDOTBS1", "SYSAUX", "ORCL2PDB":"SYSTEM", "ORCL2PDB":"SYSAUX" delete archivelog;
alter clone database open resetlogs;
}
executing Memory Script

executing command: SET until clause

sql statement: alter database datafile  10 online

Starting recover at 23-JUN-16
using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 14 is already on disk as file /u02/app/oracle/fra/ORCL2/archivelog/2016_06_23/o1_mf_1_14_cpqq0t9z_.arc
archived log file name=/u02/app/oracle/fra/ORCL2/archivelog/2016_06_23/o1_mf_1_14_cpqq0t9z_.arc thread=1 sequence=14
media recovery complete, elapsed time: 00:00:00
Finished recover at 23-JUN-16

database opened

contents of Memory Script:
{
sql clone 'alter pluggable database  ORCL2PDB open';
}
executing Memory Script

sql statement: alter pluggable database  ORCL2PDB open

contents of Memory Script:
{
# create directory for datapump import
sql 'ORCL2PDB' "create or replace directory 
TSPITR_DIROBJ_DPDIR as ''
/tmp''";
# create directory for datapump export
sql clone 'ORCL2PDB' "create or replace directory 
TSPITR_DIROBJ_DPDIR as ''
/tmp''";
}
executing Memory Script

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/tmp''

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/tmp''

Performing export of tables...
   EXPDP> Starting "SYS"."TSPITR_EXP_EDud_bamw":  
   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 "WXC"."T1"                                  5.046 KB       1 rows
   EXPDP> Master table "SYS"."TSPITR_EXP_EDud_bamw" successfully loaded/unloaded
   EXPDP> ******************************************************************************
   EXPDP> Dump file set for SYS.TSPITR_EXP_EDud_bamw is:
   EXPDP>   /tmp/tspitr_EDud_26521.dmp
   EXPDP> Job "SYS"."TSPITR_EXP_EDud_bamw" successfully completed at Thu Jun 23 21:00:28 2016 elapsed 0 00:00:18
Export completed


contents of Memory Script:
{
# shutdown clone before import
shutdown clone abort
}
executing Memory Script

Oracle instance shut down

Performing import of tables...
   IMPDP> Master table "SYS"."TSPITR_IMP_EDud_nbEc" successfully loaded/unloaded
   IMPDP> Starting "SYS"."TSPITR_IMP_EDud_nbEc":  
   IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE
   IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
   IMPDP> . . imported "WXC"."T1"                                  5.046 KB       1 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_EDud_nbEc" successfully completed at Thu Jun 23 21:00:42 2016 elapsed 0 00:00:05
Import completed


Removing automatic instance
Automatic instance removed
auxiliary instance file /tmp/ORCL2/datafile/o1_mf_temp_cpqq8f81_.tmp deleted
auxiliary instance file /tmp/ORCL2/datafile/o1_mf_temp_cpqq87tr_.tmp deleted
auxiliary instance file /tmp/EDUD_PITR_ORCL2PDB_ORCL2/onlinelog/o1_mf_3_cpqqb1o4_.log deleted
auxiliary instance file /tmp/EDUD_PITR_ORCL2PDB_ORCL2/onlinelog/o1_mf_2_cpqqb0hh_.log deleted
auxiliary instance file /tmp/EDUD_PITR_ORCL2PDB_ORCL2/onlinelog/o1_mf_1_cpqq9zfj_.log deleted
auxiliary instance file /tmp/EDUD_PITR_ORCL2PDB_ORCL2/datafile/o1_mf_users_cpqq9xdn_.dbf deleted
auxiliary instance file /tmp/ORCL2/datafile/o1_mf_sysaux_cpqq7cg6_.dbf deleted
auxiliary instance file /tmp/ORCL2/datafile/o1_mf_system_cpqq7cgq_.dbf deleted
auxiliary instance file /tmp/ORCL2/datafile/o1_mf_sysaux_cpqq5xyw_.dbf deleted
auxiliary instance file /tmp/ORCL2/datafile/o1_mf_undotbs1_cpqq5xz1_.dbf deleted
auxiliary instance file /tmp/ORCL2/datafile/o1_mf_system_cpqq5xym_.dbf deleted
auxiliary instance file /tmp/ORCL2/controlfile/o1_mf_cpqq5qwd_.ctl deleted
auxiliary instance file tspitr_EDud_26521.dmp deleted
Finished recover at 23-JUN-16

至此,整个恢复过程就结束了,类似于表空间时间点恢复,但是更加灵活和便捷。

检查恢复后的表

SQL> select count(*) from t1;

  COUNT(*)
----------
	 1