之前寫(xiě)過(guò)1篇?jiǎng)h除oracle home目錄的blog,參考:
Linux 平臺(tái)誤刪 home oracle 根目錄的解決方法
http://blog.csdn.net/tianlesoftware/article/details/43794273
本篇是這邊的引深,本來(lái)應(yīng)當(dāng)是年前整理的,拖到年后了。
摹擬現(xiàn)狀:
數(shù)據(jù)庫(kù)在正常運(yùn)行,誤操作,直接rm 掉了數(shù)據(jù)文件。
測(cè)試環(huán)境:
[oracle@dg1 trace]$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.1(Santiago)
Oracle 11.2.0.3 單實(shí)例。
[oracle@dg1 ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.3.0 Production onWed Aug 27 18:36:32 2014
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise EditionRelease 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Miningand Real Application Testing options
SQL>
這個(gè)問(wèn)題也要分2種情況,1種是歸檔模式,1種是非歸檔模式,歸檔模式處理就容易很多了。但現(xiàn)在很多由開(kāi)發(fā)人員管理的庫(kù)是非歸檔,并且也缺少專業(yè)的運(yùn)維技能,誤操作的幾率也會(huì)增加很多。
SQL> create tablespace dropspacedatafile '/u01/dropspace01.dbf' size 100m;
Tablespace created.
SQL> create user ahzhixinidentified by ahzhixin default tablespace dropspace;
User created.
SQL> grantconnect,resource,dba to ahzhixin;
Grant succeeded.
SQL> conn ahzhixin/ahzhixin
Connected.
SQL> create table test1 as select * fromall_users;
Table created.
SQL> create table test2 as select * fromall_users;
Table created.
SQL> create table test3 as select * fromall_users;
Table created.
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /u01/archivelog
Oldest online log sequence 83
Next log sequence to archive 85
Current log sequence 85
SQL>
在操作系統(tǒng)上直接刪除數(shù)據(jù)文件:
[oracle@dg1 u01]$ rm-rf /u01/dropspace01.dbf
此時(shí)數(shù)據(jù)庫(kù)還是正常運(yùn)行,我們查詢我們之前創(chuàng)建的幾張表:
SQL> select count(1) from test1;
COUNT(1)
----------
31
SQL> select count(1) from test2;
COUNT(1)
----------
31
SQL> select count(1) from test3;
COUNT(1)
----------
31
--insert 也沒(méi)有問(wèn)題:
SQL> insert into test1 select * fromall_users;
31 rows created.
SQL> commit;
Commit complete.
SQL> select count(1) from test1;
COUNT(1)
----------
62
最少?gòu)哪壳翱矗?切都是正常。 這里正常也是由于我們的操作系統(tǒng)是Linux,當(dāng)數(shù)據(jù)文件從操作系統(tǒng)級(jí)別被rm掉,但之前打開(kāi)該文件的進(jìn)程依然持有相應(yīng)的文件句柄,所以指向的文件依然可以讀寫(xiě),并且該文件的文件描寫(xiě)符可以從/proc目錄中取得,也能夠利用這個(gè)句柄恢復(fù)文件。
如果在這個(gè)時(shí)候,重啟了數(shù)據(jù)庫(kù)或操作系統(tǒng),那末句柄就會(huì)消失,也就只能通過(guò)掃描磁盤進(jìn)行文件恢復(fù)。
dbwr進(jìn)程會(huì)打開(kāi)所有數(shù)據(jù)文件的句柄,在proc目錄中可以查到這些數(shù)據(jù)文件的信息,目錄名是進(jìn)程PID,fd表示文件描寫(xiě)符。
檢查dbwr的進(jìn)程PID:
[oracle@dg1 trace]$ ps -ef|grep dbw0|grep-v grep
oracle 9964 1 0 00:49 ? 00:00:03 ora_dbw0_dave
[oracle@dg1 trace]$ cd /proc/9964/fd
[oracle@dg1 fd]$ ls -l
這里的259 就是我們刪掉的數(shù)據(jù)文件。
//直接cp該句柄文件名回原位置:
[oracle@dg1 fd]$ cp 259/u01/dropspace01.dbf
[oracle@dg1 fd]$
由于數(shù)據(jù)庫(kù)1直是open的,那末SCN也會(huì)不斷的變化,我們cp出來(lái)的數(shù)據(jù)文件和數(shù)據(jù)庫(kù)當(dāng)前的信息不1致,所以我們需要進(jìn)行recover:
SQL> alter database datafile'/u01/dropspace01.dbf' offline;
Database altered.
SQL> recover datafile'/u01/dropspace01.dbf';
Media recovery complete.
SQL> alter database datafile'/u01/dropspace01.dbf' online;
Database altered.
SQL>
恢復(fù)正常。
//文件存在:
[oracle@dg1 u01]$ ls -la/u01/dropspace01.dbf
-rw-r----- 1 oracle oinstall 104865792 Aug27 21:41 /u01/dropspace01.dbf
//重啟數(shù)據(jù)庫(kù):
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 814227456 bytes
Fixed Size 2232760 bytes
Variable Size 490737224 bytes
Database Buffers 318767104 bytes
Redo Buffers 2490368 bytes
Database mounted.
Database opened.
SQL>
也正常。 這里有2個(gè)注意的問(wèn)題:數(shù)據(jù)庫(kù)是歸檔模式,數(shù)據(jù)庫(kù)或操作系統(tǒng)沒(méi)有重啟。這2點(diǎn)非常關(guān)鍵。 也正式如此,才讓操作比較簡(jiǎn)單。
如果是非歸檔模式,那就要復(fù)雜很多了。
在非歸檔模式下,如果刪除數(shù)據(jù)文件,并且又觸發(fā)了CKPT,那末CKPT 會(huì)直接把全部實(shí)例中斷掉,也就是說(shuō),如果是比較繁忙的數(shù)據(jù)庫(kù),如果誤刪除數(shù)據(jù)文件,實(shí)例可能會(huì)中斷,1旦實(shí)例中斷,那末用之前講的通過(guò)句柄恢復(fù)就沒(méi)有可能性了。
固然也有另外一種可能性,就是刪除數(shù)據(jù)文件以后,可以先通過(guò)句柄恢復(fù),然后用expdp導(dǎo)出數(shù)據(jù),盡量的挽救部份數(shù)據(jù)。 這個(gè)動(dòng)作就是與時(shí)間賽跑的進(jìn)程了。
總之生產(chǎn)環(huán)境,操作1定要謹(jǐn)慎,還有要開(kāi)歸檔,除非數(shù)據(jù)允許丟失。
--------------------------------------------------------------------------------------------
版權(quán)所有,文章制止轉(zhuǎn)載,否則追究法律責(zé)任!
AboutDave:
--------------------------------------------------------------------------------------------
QQ: 251097186
Email: tianlesoftware@gmail.com
Blog: http://blog.csdn.net/tianlesoftware
Weibo: http://weibo.com/tianlesoftware
Twitter: http://twitter.com/tianlesoftware
Facebook: http://www.facebook.com/tianlesoftware
Linkedin: http://cn.linkedin.com/in/tianlesoftware
Dave 的QQ群:
--------------------------------------------------------------------------------------------
注意:加群必須注明表空間和數(shù)據(jù)文件關(guān)系 | 不要重復(fù)加群
CNDBA_1: 62697850 (空) CNDBA_2: 62697716 (滿) CNDBA_3: 283816689
CNDBA_4: 391125754 CNDBA_5:104207940 CNDBA_6: 62697977 CNDBA_7: 142216823(滿)