Oracle 單一Tablespace回到指定時間點

單一Tablespace回到指定時間點-Tablespace Point-in-Time Recovery(TSPITR):

標籤: 
步驟:
確定復原的正確時間,ex Flashback query、db最後的備份時間
確定復原內容
確認復原之後,那一些資料會不見
 
TSPITR可以讓你快速復原到之前的一個或多個tablespaces
TSPITR不影響其它的Tablespaces或者objects
一般TSPITR用在以下的情況:
一、大量Truncate復原資料
二、Tablespace有資料損壞
三、當有大量job更改了table,需要回復到某一時間點
 
 


 

 
模擬測試方式:
[oracle@C5TDB1 ~]$ echo $ORACLE_SID
testdb
[oracle@C5TDB1 ~]$ sqlplus / as sysdba
SQL> create tablespace frank datafile '/home/oracle/frank.dbf' size 10M;
SQL> drop table hr.emphist purge;ß確認沒有此table
SQL> create table hr.emphist tablespace frankts as select * from hr.employees;
SQL> select count(*) from hr.emphist;
  COUNT(*)
———-
       107
 
–對target database進行whole backup
[oracle@oracleDB ~]$ export ORACLE_SID=testdb
[oracle@oracleDB ~]$ rman target /
connected to target database: ORCL (DBID=1257377122)
RMAN> backup database plus archivelog;
 
SQL>create table hr.temp1 tablespace frankts as select * from hr.employees;
ß備份完才建立temp1,還原此備份之後,這個temp1應該是要不見
 
SQL> select count(*) from hr.temp1;
  COUNT(*)
———-
       107
查詢一下,當ablespace frankts回復2014-01-16:17:00:50時,有哪些table將受到影響
select owner,name,tablespace_name,to_char(creation_time,'YYYY-MM-DD:HH24:MI:SS') creation_time
from ts_pitr_objects_to_be_dropped
where tablespace_name='FRANKTS' and creation_time>to_date('2014-01-16:17:00:50','YYYY-MM-DD:HH24:MI:SS');
 
 
因hr.temp1與frankts同一個tablespace,建議先將hr.temp1 export或直接將此table移到另外的tablespace存放(SQL> alter table hr.temp1 move tablespace users;)
[oracle@oracleDB ~]$ exp hr/hr tables=temp1 file=temp1.dmp
 
備份的暫存空間:
[oracle@oracleDB ~]$mkdir /home/oracle/testdb/
[oracle@oracleDB ~]$rman target /
 
–首先必須確定backup存在(在PITR時間點之前的備份)
RMAN> list backup of database;
 
11g指令:
RMAN> recover tablespace frankts until time '2011-04-07:15:09:50' auxiliary destination '/home/oracle/orcl_tspitr';
 
10g指令:
RMAN>recover tablespace frankts until time "to_timestamp(' 2014-01-16:17:03:17','YYYY-MM-DD:HH24:MI:SS')" auxiliary destination '/home/oracle/testdb';
 
–因為進行TSPITR,所以之前的備份已經不能使用,所以應該是沒有資料
RMAN> list backup of tablespace frankts;
   
 
–建議立刻對frankts進行備份
RMAN> backup tablespace frankts; 
 
做完之後,此tablespace會變成offline,轉換成online
SQL> alter tablespace frankts online;
 
因還原完之後,此temp就不見了,所以要透過其它方式再把它搬回來
SQL> select count(*) from hr.temp1;
select count(*) from hr.temp1
                        *
ERROR at line 1:

 

ORA-00942: table or view does not exist  –可以使用之前所產生的dumpfile,將hr.temp1重新產生