生產案例:開發不當心把某個表數據清了,沒有邏輯備份,有物理備份

剛到公司坐下,開發人員就匆匆跑過來跟我說:「因爲不知道哪一天不當心把某某表的數據清空了,可否幫我恢復一下。」 第一反應,只要備份,那很簡單,因此下面是個人解決辦法。
 

思路一:利用閃回找回

select * from INFO_FEEDBACK_ORCLE as of timestamp to_timestamp('2016-12-06 12:00:30','YYYY-MM-DD HH24:MI:SS')
--提示閃回時間已好久

如今時間是2016年12月8號10點,我立刻用閃回查詢到能查到的最先時間12月6號12:00,發現那時的數據就已經清空了,因此閃回找不到了,放棄。sql

思路二:利用邏輯備份恢復

由於邏輯備份,不管是expdp或者exp都能指定恢復某一張表,因此趕快到服務器的調度任務裏查看有沒有邏輯備份,發現沒有用邏輯備份,只有物理備份RMAN,因此邏輯備份恢復仍是沒法還原。

思路三:RMAN物理備份

最後只能用最麻煩的RMAN物理備份裏,爲何說麻煩呢,由於若是在本機直接用RMAN恢復的話,就會致使其它因此表的數據都恢復了,那最近的數據就沒了,因此這是萬萬不行的,只能利用RMAN異地恢復,而後再exp導出該表,在到服務器導入該表,因此操做有點麻煩,無論如何我答應開發,一個小時搞定,因此得趕快動手,一下是個人恢復步驟:
前提:我有一個安裝oracle的測試環境

一、把原庫的備份文件拷貝到測試庫

備份文件包括archive歸檔,datafile數據,spfile參數文件,control控制文件,原庫備份腳本以下:
run {
allocate channel d1 type disk;
backup as COMPRESSED BACKUPSET database format='/backup/oracle/b2bdb/full_%d_%T_%s_%p.bak';
sql 'alter system archive log current';
backup as COMPRESSED BACKUPSET archivelog all format '/backup/oracle/b2bdb/arch_log_%d_%T_%s_%p.bak' delete all input;
backup current controlfile format='/backup/oracle/b2bdb/controlfile_%d_%T_%s_%p.bak' spfile format='/backup/oracle/b2bdb/spfile_%d_%T_%s_%p.bak';
release channel d1;
}
crosscheck backup;
delete noprompt expired backup;
delete noprompt obsolete;

二、生成密碼文件

orapwd file=?/dbs/orapwb2bdb password=oracle

三、RMAN鏈接備庫,並啓動到nomount狀態,恢復init參數文件

--指定SID
export ORACLE_SID=b2bdb
--鏈接RMAN測試庫
rman target /
--啓動到nomount狀態
startup nomount
--restire spfile
RMAN> restore spfile to pfile'/u01/app/oracle/product/11.2.0/dbhome_1/dbs/initb2bdb.ora' from '/u01/bak/spfile_B2BDB_20161201_959_1.bak';

四、把恢復的參數文件initb2bdb.ora修改裏面的路徑內容

爲了能讓恢復的速度更快,我幾乎不修改裏面的內容,我僅僅把裏面參數文件提到的目錄都在測試環境建一個,與原庫同樣。

五、利用新生產的初始參數文件啓動到nomount

另外開一個窗口 ,用咱們修改會的initb2bdb.ora重啓數據庫倒nomount
sqlplus / as sysdba
--因爲剛開始啓動過,因此先關閉一下
shutdown immediate
--指定咱們恢復的參數文件啓動
startup nomount pfile=?/dbs/initb2bdb.ora

六、恢復控制文件

上面啓動到nomount狀態後,咱們就能夠恢復控制文件了,退出,或者另開一個窗口
--指定SID
[oracle@oratest bak]$ export ORACLE_SID=b2bdb
--鏈接RMAN
[oracle@oratest bak]$ rman target /
 
Recovery Manager: Release 11.2.0.4.0 - Production on Thu Dec 1 01:42:42 2016
 
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.
 
connected to target database: B2BDB (not mounted)
 --恢復控制文件,from指向咱們剛纔拷貝過來的備份文件
RMAN> restore controlfile from '/u01/bak/controlfile_B2BDB_20161201_958_1.bak';
 
Starting restore at 01-DEC-16
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=19 device type=DISK
 
channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
output file name=/u01/app/oracle/oradata/b2bdb/control01.ctl
output file name=/u01/app/oracle/oradata/b2bdb/control02.ctl
Finished restore at 01-DEC-16

七、將數據庫啓動到mount狀態

 
RMAN> alter database mount;
 
database mounted
released channel: ORA_DISK_1

八、restore數據庫(同目錄)

此處我是爲了能更快速度的恢復,因此數據目錄那些建的跟原庫是同樣的,同目錄狀況下,我只要執行如下命令便可:
restore database;

八、restore數據庫(不一樣目錄)

若須要恢復到不一樣的目錄,須要查出原庫數據文件對應的序列號,而後set如下,以下:
注意:這次恢復不執行以下步驟,由於個人是同目錄
(1)先到原庫查詢一下以下信息:
select file_id,file_name from dba_data_files;
1   /oradata/b2bdb/system01.dbf
2   /oradata/b2bdb/sysaux01.dbf
3   /oradata/b2bdb/undotbs01.dbf
4   /oradata/b2bdb/users01.dbf
5   /oradata/b2bdb/tbs_sso_001.dbf
6   /oradata/b2bdb/tbs_cableex_001.dbf
7   /oradata/b2bdb/tbs_b2b_001.dbf
8   /oradata/b2bdb/tbs_kh_test_001.dbf
9   /oradata/b2bdb/tbs_mmbao2_001.dbf
 
select file_id,file_name from dba_temp_files;
1   /oradata/b2bdb/temp01.dbf

注意,restore 的時候不會對temp 表空間進行restore。因此等restore 以後,咱們須要手工建立temp表空間。數據庫

 利用rman腳本還原
 
RMAN>  run
 {
 set newname for datafile  1 to  "/u01/app/oracle/oradata/system01.dbf";
 set newname for datafile  2 to  "/u01/app/oracle/oradata/sysaux01.dbf";
 set newname for datafile  3 to  "/u01/app/oracle/oradata/undotbs01.dbf";
 set newname for datafile  4 to  "/u01/app/oracle/oradata/users01.dbf";
 set newname for datafile  5 to  "/u01/app/oracle/oradata/tbs_sso_001.dbf";
 set newname for datafile  6 to  "/u01/app/oracle/oradata/tbs_cableex_001.dbf";
 set newname for datafile  7 to  "/u01/app/oracle/oradata/tbs_b2b_001.dbf";
 set newname for datafile  8 to  "/u01/app/oracle/oradata/tbs_kh_test_001.dbf";
 set newname for datafile  9 to  "/u01/app/oracle/oradata/tbs_mmbao2_001.dbf";
 restore database;
 switch datafile all;
 }

九、recover 數據庫

而後恢復數據庫
RMAN> recover database;
 
Starting recover at 01-DEC-16
using channel ORA_DISK_1
 
starting media recovery
 
channel ORA_DISK_1: starting archived log restore to default destination
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=1814
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=1815
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=1816
channel ORA_DISK_1: reading from backup piece /backup/oracle/b2bdb/arch_log_B2BDB_20161201_957_1.bak
channel ORA_DISK_1: errors found reading piece handle=/backup/oracle/b2bdb/arch_log_B2BDB_20161201_957_1.bak
channel ORA_DISK_1: failover to piece handle=/u01/bak/arch_log_B2BDB_20161201_957_1.bak tag=TAG20161201T050152
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:25
archived log file name=/u01/app/oracle/oradata/b2bdb_archivelog/archivelog/1_1814_915284737.arch thread=1 sequence=1814
archived log file name=/u01/app/oracle/oradata/b2bdb_archivelog/archivelog/1_1815_915284737.arch thread=1 sequence=1815
archived log file name=/u01/app/oracle/oradata/b2bdb_archivelog/archivelog/1_1816_915284737.arch thread=1 sequence=1816
unable to find archived log
archived log thread=1 sequence=1817
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of recover command at 12/01/2016 02:20:41
RMAN-06054: media recovery requesting unknown archived log for thread 1 with sequence 1817 and starting SCN of 180140127

若後面報這個錯RMAN-06054: media recovery requesting unknown archived log for thread 1 with sequence 1817 and starting SCN of 180140127服務器

咱們能夠直接恢復到他指定的SCN點
RMAN> recover database until scn 180140127;
Starting recover at 01-DEC-16
using channel ORA_DISK_1
 
starting media recovery
media recovery complete, elapsed time: 00:00:00
 
Finished recover at 01-DEC-16

 十、用open resetlogs 打開數據庫

SQL> alter database open resetlogs;
Database altered.

到此就夠用了,至於後續一些小問題,如臨時空間,日誌路徑問題等看着錯誤去處理。oracle

十一、用exp把須要恢復的那張表備份出來

exp xxx/xxx file=xxx tables=xx

十二、先在原庫裏把那張表備份一下,而後刪除

 

create table xx_bak as select * from xx;
drop table xx;

 

 

1三、用imp把備份恢復進去

 

imp xxx/xxx file=xx fromuser=xxx touser=xxx

 

 

1四、把剛剛備份的數據也插入到剛剛恢復的表裏

insert into xx select * from xx_bak;

 

 
搞定!!
相關文章
相關標籤/搜索