RMAN可以用來對整個表空間進行指定SCN恢復的操作,如果誤操作發生在個別的表空間,而且無法用select * from t as of scn來恢復,如果用flash backup database會影響到整個數據庫,這時用RMAN的傳輸表空間進行對指定的表空間進行恢復也是一種辦法,過程中只需要對當前表空間設成只讀,不影響其它的表空間。實際上它是通過建立輔組實例來建立傳輸文件,再將傳輸文件導入到數據庫中,這就要求服務器要有一定的磁盤空餘容量。
測試過程如下:
1.先做一次全備,包括歸檔日誌
備份之前檢查一下測試的表空間是不是自包含的,如果不是的話需要作相應的調整。
SQL> exec dbms_tts.transport_set_check('L5MSPACE',true);
PL/SQL procedure successfully completed.
SQL> select * from transport_set_violations;
no rows selected
開始備份
SQL> alter system archive log current ;
System altered.
RMAN>
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/u01/RMAN0/%F';
CONFIGURE DEVICE TYPE DISK PARALLELISM 2 BACKUP TYPE TO COMPRESSED BACKUPSET;
run {
crosscheck archivelog all;
delete expired archivelog all;
crosscheck backup;
delete expired backup;
allocate channel c1 type disk;
backup incremental level 0 database format '/u01/RMAN0/db0%u_%s_%p.bak' filesperset 3 include current controlfile;
backup spfile tag='spfile' format='/u01/RMAN0/ORCL_spfile_%U_%T';
sql 'alter system archive log current';
backup filesperset 1 format '/u01/RMAN0/arch%u_%s_%p.bak'
archivelog all delete input;
release channel c1;
}
exit;
2.建立測試環境並做一個人爲的誤操作:
SQL> create table t tablespace l5mspace as select * from dba_objects;
Table created.
SQL> alter system archive log current;
System altered.
SQL> select count(*) from t;
COUNT(*)
----------
54362
SQL> set numw 12
SQL> select current_scn,sysdate from v$database;
CURRENT_SCN SYSDATE
------------ ---------
17670295088 26-MAR-20
開始誤操作:
SQL> delete from t where owner='SYSTEM';
454 rows deleted.
SQL> commit;
Commit complete.
SQL> alter system archive log current;
System altered.
將誤操作的日誌部分進行歸檔:
RMAN> backup archivelog all not backed up 1 times format '/u01/RMAN0/arch%u_% s_%p.bak';
Starting backup at 26-MAR-20
current log archived
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=312 devtype=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: sid=324 devtype=DISK
skipping archive log file /u01/app/oracle/admin/orcl/flash_recovery_area/ORCL/ar chivelog/2020_01_07/o1_mf_1_1_h18h4ypm_.arc; already backed up 1 time(s)
skipping archive log file /u01/app/oracle/admin/orcl/flash_recovery_area/ORCL/ar chivelog/2020_01_11/o1_mf_1_1_h1kdyhgv_.arc; already backed up 1 time(s)
skipping archive log file /u01/app/oracle/admin/orcl/flash_recovery_area/ORCL/ar chivelog/2020_01_18/o1_mf_1_2_h23t2h65_.arc; already backed up 1 time(s)
skipping archive log file /u01/app/oracle/admin/orcl/flash_recovery_area/ORCL/ar chivelog/2020_01_24/o1_mf_1_3_h2oq37w5_.arc; already backed up 1 time(s)
skipping archive log file /u01/app/oracle/admin/orcl/flash_recovery_area/ORCL/ar chivelog/2020_01_31/o1_mf_1_4_h372kcw5_.arc; already backed up 1 time(s)
skipping archive log file /u01/app/oracle/admin/orcl/flash_recovery_area/ORCL/ar chivelog/2020_02_06/o1_mf_1_5_h3r6zlr5_.arc; already backed up 1 time(s)
skipping archive log file /u01/app/oracle/admin/orcl/flash_recovery_area/ORCL/ar chivelog/2020_02_12/o1_mf_1_6_h48176ql_.arc; already backed up 1 time(s)
skipping archive log file /u01/app/oracle/admin/orcl/flash_recovery_area/ORCL/ar chivelog/2020_02_18/o1_mf_1_7_h4prb904_.arc; already backed up 1 time(s)
skipping archive log file /u01/app/oracle/admin/orcl/flash_recovery_area/ORCL/ar chivelog/2020_02_24/o1_mf_1_8_h55sr8w1_.arc; already backed up 1 time(s)
skipping archive log file /u01/app/oracle/admin/orcl/flash_recovery_area/ORCL/ar chivelog/2020_03_23/o1_mf_1_9_h7jvfx2h_.arc; already backed up 1 time(s)
skipping archive log file /u01/app/oracle/admin/orcl/flash_recovery_area/ORCL/ar chivelog/2020_03_26/o1_mf_1_10_h7ronrv9_.arc; already backed up 1 time(s)
skipping archive log file /u01/app/oracle/admin/orcl/flash_recovery_area/ORCL/ar chivelog/2020_03_26/o1_mf_1_11_h7rpfh5v_.arc; already backed up 1 time(s)
channel ORA_DISK_1: starting compressed archive log backupset
channel ORA_DISK_1: specifying archive log(s) in backup set
input archive log thread=1 sequence=12 recid=642 stamp=1036078709
channel ORA_DISK_1: starting piece 1 at 26-MAR-20
channel ORA_DISK_2: starting compressed archive log backupset
channel ORA_DISK_2: specifying archive log(s) in backup set
input archive log thread=1 sequence=13 recid=643 stamp=1036078808
input archive log thread=1 sequence=14 recid=644 stamp=1036078884
channel ORA_DISK_2: starting piece 1 at 26-MAR-20
channel ORA_DISK_1: finished piece 1 at 26-MAR-20
piece handle=/u01/RMAN0/archqmus2jp6_854_1.bak tag=TAG20200326T154126 comment=NO NE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:02
channel ORA_DISK_2: finished piece 1 at 26-MAR-20
piece handle=/u01/RMAN0/archqnus2jp6_855_1.bak tag=TAG20200326T154126 comment=NO NE
channel ORA_DISK_2: backup set complete, elapsed time: 00:00:02
Finished backup at 26-MAR-20
Starting Control File and SPFILE Autobackup at 26-MAR-20
piece handle=/u01/RMAN0/c-1166343071-20200326-02 comment=NONE
Finished Control File and SPFILE Autobackup at 26-MAR-20
3.現在用rman的transport命令來進行恢復:
RMAN> transport tablespace l5mspace tablespace destination '/u01/aux' auxiliary destination '/u01/aux' until scn 17670295088;
輸出比較多就不復制了
過程其實就是通過輔組實例+數據庫備份來恢復到特定時間點或者scn
查看一下/u01/aux生成的文件
[oracle@qht115 aux]$ ll
total 1054848
-rw-r----- 1 oracle oinstall 5337088 Mar 26 17:21 dmpfile.dmp
-rw-r--r-- 1 oracle oinstall 1731 Mar 26 17:21 explog.log
-rw-r--r-- 1 oracle oinstall 2037 Mar 26 17:21 impscrpt.sql
-rw-r----- 1 oracle oinstall 1073750016 Mar 26 17:18 l5mspace01.dbf
drwxr-x--- 4 oracle oinstall 4096 Mar 26 17:12 TSPITR_ORCL_QNRP
--//l5mspace.dbf就是傳輸表空間的數據文件.dmpfile.dmp就是源數據,而impscrpt.sql就是如何導入的腳本.
4.導入整個表空間到數據庫
直接導入肯定不行,表名以及表空間衝突,必須重新映射.
SQL> create user l5msys identified by l5msys;
User created.
SQL> grant connect,sysdba,dba,resource to l5msys;
Grant succeeded.
SQL> CREATE DIRECTORY D_AUX as '/u01/aux';
Directory created.
SQL> grant read,write on directory D_AUX to l5msys;
Grant succeeded.
執行導入操作:
[oracle@qht115 aux]$ impdp l5m/l5m dumpfile=dmpfile.dmp directory=D_aux transport_datafiles=/u01/aux/l5mspace01.dbf REMAP_TABLESPACE=l5mspace:l5mspace02 REMAP_SCHEMA=l5m:l5msys logfile=impdp.log
kipped due to table_exists_action of skip
ORA-39151: Table "YELLOWFLAG"."METADATA" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "YELLOWFLAG"."SPACES" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "YELLOWFLAG"."TAGS" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
Processing object type TRANSPORTABLE_EXPORT/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type TRANSPORTABLE_EXPORT/INDEX
Processing object type TRANSPORTABLE_EXPORT/CONSTRAINT/CONSTRAINT
Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS
Processing object type TRANSPORTABLE_EXPORT/TRIGGER
ORA-39082: Object type TRIGGER:"L5MSYS"."PROPERTY_HEADER_DEL" created with compilation warnings
ORA-39082: Object type TRIGGER:"L5MSYS"."PROPERTY_HEADER_DEL" created with compilation warnings
Processing object type TRANSPORTABLE_EXPORT/INDEX/FUNCTIONAL_AND_BITMAP/INDEX
Processing object type TRANSPORTABLE_EXPORT/INDEX/STATISTICS/FUNCTIONAL_AND_BITMAP/INDEX_STATISTICS
Processing object type TRANSPORTABLE_EXPORT/TABLE_STATISTICS
Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
Job "L5M"."SYS_IMPORT_TRANSPORTABLE_01" completed with 493 error(s) at 20:26:15
由於這個表空間除了l5m用戶外,還有其它的用戶,這些用戶會由於衝突無法導入,這裏可以不管,我只需要查看導入的新用戶l5msys的數據是否恢復過來。
驗證一下:
SQL> select count(*) from l5msys.t;
COUNT(*)
----------
54362
這裏有一個知識點,恢復出來數據的rowid與原數據的rowid是一致的。
兩個表的rowid一樣,除了這種情況rowid會一樣,另一種就是cluster table
SQL> select rowid,object_name from l5m.t where rownum=1;
ROWID OBJECT_NAME
-------------------- --------------------
AABmZPAAGAAALTkAAA ICOL$
SQL> select rowid,object_name from l5msys.t where rownum=1;
ROWID OBJECT_NAME
-------------------- --------------------
AABmZPAAGAAALTkAAA ICOL$
參考:
https://www.cnblogs.com/lfree/p/7068974.html