Oracle資料庫運行時rm誤刪檔案的解決方案

來源:互聯網
上載者:User

標籤:oracle   誤刪   rm   recover   file description   

    網上可以看到不少有關oracle運行時誤刪資料檔案的討論,對於使用rm作業系統命令刪除的檔案,可以通過copy檔案描述符來恢複檔案,當然這種方式的前提還是資料庫還在運行。

    但在DB還在運行時直接copy檔案描述符來恢複檔案,這樣操作其實有幾個問題:

  1. copy過程中需要額外的儲存空間

  2. 如果誤刪的檔案包括system資料表空間,在checkpoint發起之前,所有copy操作必須完成,否則來不及copy的資料檔案將會因為執行個體終止而完全丟失,還在copy中資料檔案無效;即使不包括system資料表空間,也需要在checkpoint之前完成copy,否則copy下來的資料檔案頭可能損壞

  3. 如果誤刪的檔案包括online redo或undo,這種方式很可能無效

  4. 恢複資料檔案的操作必須將對應的資料表空間下線或關閉資料庫,這勢必影響線上業務



    深入思考一下,資料庫進程讀寫資料檔案,是通過檔案描述符(以下簡稱FD)來操作硬碟上對應的資料檔案的。既然這些誤刪的資料檔案當前還可以在記憶體中讀寫,通過FD操作檔案,而且這些誤刪的資料檔案本身佔用的空間也並沒有釋放出來,那是否可以通過某種類似undelete的方式將FD的內容保留會原先的硬碟上呢?

    其實github上有多重專案就是實現這個的,包括fdlink、vfs-undelete等。但這些方案也有問題:

  1. 需要編譯核心模組來實現。但是這一點,這個要求就比較高,畢竟伺服器上一般是不會安裝核心原始碼的,而且即使要下載也需要時間。

  2. 需要root許可權

  3. 實際效果難說。我實測下來,centos5.6的系統,預設核心2.6.18-238.el5, 刪除單個資料檔案,用fdlink和vfs-undelete都不能成功恢複。

    所以這個方法基本不可行。


    那是否還有其他方式呢?其實再深入思考下,既然FD還能完全操作這些誤刪的資料檔案,完全可以把FD直接當做資料檔案來處理。為了保持原來的資料檔案路徑不變,我們可以建立一個soft link,把資料檔案連結回FD,這樣所有的oracle進程也就可以找到資料檔案,這樣誤刪操作也不會影響到正常業務進行。

    但這樣做的問題又來了,畢竟FD不是硬碟上的檔案,DB或系統重啟、斷電都會使FD失效,誤刪的資料檔案還是會可能丟失,而直接copy FD產生的資料檔案又有本文開頭說的那些問題,怎麼操作才能兼顧呢?

    解決這個問題其實也並不麻煩。只要瞭解FD的特性,只要還有進程持有該FD,對應的資料檔案內容就就可以讀寫。我們可以使用tail命令來始終佔用這個FD,那麼即使DB關閉或者資料檔案offline了,這個FD對應的資料檔案內容還是可以讀取的,而因為DB已經關閉或資料檔案已經offline了,自然這些資料檔案也就沒有寫操作了,此時copy出來的資料檔案才是乾淨安全的。


    下面是對這個過程一個示範(test表在資料表空間users01.dbf上,重要操作加亮顯示):


$ ls
bbed.par    control01.ctl  filelist.txt  redo01.log  redo03.log    system01.dbf  undotbs01.dbf
bifile.bbd  example01.dbf  log.bbd       redo02.log  sysaux01.dbf  temp01.dbf    users01.dbf
$ ps -ef|grep dbw
oracle     574  5117  0 10:48 pts/1    00:00:00 grep dbw
$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Tue Nov 4 10:48:09 2014

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area 1870647296 bytes
Fixed Size                  2229424 bytes
Variable Size             452987728 bytes
Database Buffers         1409286144 bytes
Redo Buffers                6144000 bytes
Database mounted.
Database opened.
SQL> select count(*) from test;

  COUNT(*)
----------
    618237

$ ll
total 2218432
-rw-r--r-- 1 oracle oinstall        74 Oct 30 12:04 bbed.par
-rw-r--r-- 1 oracle oinstall     18432 Oct 30 13:48 bifile.bbd
-rw-r----- 1 oracle oinstall   9814016 Nov  4 10:48 control01.ctl
-rw-r----- 1 oracle oinstall 362422272 Nov  4 10:48 example01.dbf
-rw-r--r-- 1 oracle oinstall       259 Oct 30 12:03 filelist.txt
-rw-r--r-- 1 oracle oinstall      1725 Oct 30 13:48 log.bbd
-rw-r----- 1 oracle oinstall  52429312 Nov  4 10:48 redo01.log
-rw-r----- 1 oracle oinstall  52429312 Nov  4 10:48 redo02.log
-rw-r----- 1 oracle oinstall  52429312 Nov  4 10:48 redo03.log
-rw-r----- 1 oracle oinstall 639639552 Nov  4 10:48 sysaux01.dbf
-rw-r----- 1 oracle oinstall 943726592 Nov  4 10:48 system01.dbf
-rw-r----- 1 oracle oinstall  20979712 Nov  4 10:48 temp01.dbf
-rw-r----- 1 oracle oinstall  57679872 Nov  4 10:48 undotbs01.dbf
-rw-r----- 1 oracle oinstall  83894272 Nov  4 10:48 users01.dbf
$ rm -f users01.dbf 

$ ps -ef|grep ora_dbw
oracle     628     1  0 10:48 ?        00:00:00 ora_dbw0_KV
oracle     773  5117  0 10:48 pts/1    00:00:00 grep ora_dbw$ ll /proc/628/fd   
total 0
lr-x------ 1 oracle oinstall 64 Nov  4 10:48 0 -> /dev/null
l-wx------ 1 oracle oinstall 64 Nov  4 10:48 1 -> /dev/null
lr-x------ 1 oracle oinstall 64 Nov  4 10:48 10 -> /dev/zero
lr-x------ 1 oracle oinstall 64 Nov  4 10:48 11 -> /dev/zero
lrwx------ 1 oracle oinstall 64 Nov  4 10:48 12 -> /u01/app/oracle/product/11.2.0/dbhome_1/dbs/hc_KV.dat
lr-x------ 1 oracle oinstall 64 Nov  4 10:48 13 -> /u01/app/oracle/product/11.2.0/dbhome_1/rdbms/mesg/oraus.msb
lr-x------ 1 oracle oinstall 64 Nov  4 10:48 14 -> /proc/628/fd
lr-x------ 1 oracle oinstall 64 Nov  4 10:48 15 -> /dev/zero
lrwx------ 1 oracle oinstall 64 Nov  4 10:48 16 -> /u01/app/oracle/product/11.2.0/dbhome_1/dbs/hc_KV.dat
lrwx------ 1 oracle oinstall 64 Nov  4 10:48 17 -> /u01/app/oracle/product/11.2.0/dbhome_1/dbs/lkKV
lr-x------ 1 oracle oinstall 64 Nov  4 10:48 18 -> /u01/app/oracle/product/11.2.0/dbhome_1/rdbms/mesg/oraus.msb
l-wx------ 1 oracle oinstall 64 Nov  4 10:48 2 -> /dev/null
lrwx------ 1 oracle oinstall 64 Nov  4 10:48 256 -> /u01/app/oracle/oradata/KV/control01.ctl
lrwx------ 1 oracle oinstall 64 Nov  4 10:48 257 -> /u01/app/oracle/fast_recovery_area/KV/control02.ctl
lrwx------ 1 oracle oinstall 64 Nov  4 10:48 258 -> /u01/app/oracle/oradata/KV/system01.dbf
lrwx------ 1 oracle oinstall 64 Nov  4 10:48 259 -> /u01/app/oracle/oradata/KV/sysaux01.dbf
lrwx------ 1 oracle oinstall 64 Nov  4 10:48 260 -> /u01/app/oracle/oradata/KV/undotbs01.dbf
lrwx------ 1 oracle oinstall 64 Nov  4 10:48 261 -> /u01/app/oracle/oradata/KV/users01.dbf (deleted)
lrwx------ 1 oracle oinstall 64 Nov  4 10:48 262 -> /u01/app/oracle/oradata/KV/example01.dbf
lrwx------ 1 oracle oinstall 64 Nov  4 10:48 263 -> /u01/app/oracle/oradata/KV/temp01.dbf
lr-x------ 1 oracle oinstall 64 Nov  4 10:48 3 -> /dev/null
lr-x------ 1 oracle oinstall 64 Nov  4 10:48 4 -> /dev/null
lr-x------ 1 oracle oinstall 64 Nov  4 10:48 5 -> /dev/null
lr-x------ 1 oracle oinstall 64 Nov  4 10:48 6 -> /dev/null
lrwx------ 1 oracle oinstall 64 Nov  4 10:48 7 -> /u01/app/oracle/product/11.2.0/dbhome_1/dbs/hc_KV.dat
lr-x------ 1 oracle oinstall 64 Nov  4 10:48 8 -> /dev/null
lr-x------ 1 oracle oinstall 64 Nov  4 10:48 9 -> /dev/null
$ tail -n +0 -f /proc/628/fd/261 >/dev/null  &
[1] 785
$ !sqlplus
sqlplus / as sysdba  

SQL*Plus: Release 11.2.0.3.0 Production on Tue Nov 4 10:49:36 2014

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> alter system checkpoint;
alter system checkpoint
*
ERROR at line 1:
ORA-03113: end-of-file on communication channel
Process ID: 792
Session ID: 142 Serial number: 5


SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
                                                                   
$ tac /u01/app/oracle/diag/rdbms/kv/KV/trace/alert_KV.log |less
Instance terminated by CKPT, pid = 636
Dumping diagnostic data in directory=[cdmp_20141104104959], requested by (instance=1, osid=636 (CKPT)), summary=[abnormal instance termination].
CKPT (ospid: 636): terminating the instance due to error 63999
System State dumped to trace file /u01/app/oracle/diag/rdbms/kv/KV/trace/KV_diag_611.trc
System state dump requested by (instance=1, osid=636 (CKPT)), summary=[abnormal instance termination].
Tue Nov 04 10:49:59 2014
Additional information: 3
Linux-x86_64 Error: 2: No such file or directory
ORA-27041: unable to open file
ORA-01110: data file 4: ‘/u01/app/oracle/oradata/KV/users01.dbf‘
ORA-01116: error in opening database file 4
ORA-63999: data file suffered media failure
Errors in file /u01/app/oracle/diag/rdbms/kv/KV/trace/KV_ckpt_636.trc:
Additional information: 3
Linux-x86_64 Error: 2: No such file or directory
ORA-27041: unable to open file
ORA-01110: data file 4: ‘/u01/app/oracle/oradata/KV/users01.dbf‘
ORA-01116: error in opening database file 4
ORA-63999: data file suffered media failure
Errors in file /u01/app/oracle/diag/rdbms/kv/KV/trace/KV_ckpt_636.trc:
Tue Nov 04 10:49:58 2014
CJQ0 started with pid=27, OS id=726
Tue Nov 04 10:48:23 2014
Starting background process CJQ0
space available in the underlying filesystem or ASM diskgroup.
database for recovery-related files, and does not reflect the amount of
user-specified limit on the amount of space that will be used by this
db_recovery_file_dest_size of 20000 MB is 8.36% used. This is a
Tue Nov 04 10:48:22 2014
ARC0: Becoming the heartbeat ARCH
ARC0: STARTING ARCH PROCESSES COMPLETE
ARC3: Archival started
ARC2: Archival started
Completed: ALTER DATABASE OPEN
QMNC started with pid=24, OS id=697
Tue Nov 04 10:48:21 2014
Starting background process QMNC
replication_dependency_tracking turned off (no async multimaster replication found)
No Resource Manager plan active
Database Characterset is WE8MSWIN1252
SMON: enabling tx recovery
Verifying 11g file header compatibility for tablespace encryption completed
Verifying file header compatibility for 11g tablespace encryption..
Undo initialization finished serial:0 start:429462324 end:429462404 diff:80 (0 seconds)
[676] Successfully onlined Undo Tablespace 2.
ARC3 started with pid=23, OS id=693
Tue Nov 04 10:48:20 2014

$ ll
total 2136420
-rw-r--r-- 1 oracle oinstall        74 Oct 30 12:04 bbed.par
-rw-r--r-- 1 oracle oinstall     18432 Oct 30 13:48 bifile.bbd
-rw-r----- 1 oracle oinstall   9814016 Nov  4 10:49 control01.ctl
-rw-r----- 1 oracle oinstall 362422272 Nov  4 10:48 example01.dbf
-rw-r--r-- 1 oracle oinstall       259 Oct 30 12:03 filelist.txt
-rw-r--r-- 1 oracle oinstall      1725 Oct 30 13:48 log.bbd
-rw-r----- 1 oracle oinstall  52429312 Nov  4 10:48 redo01.log
-rw-r----- 1 oracle oinstall  52429312 Nov  4 10:48 redo02.log
-rw-r----- 1 oracle oinstall  52429312 Nov  4 10:48 redo03.log
-rw-r----- 1 oracle oinstall 639639552 Nov  4 10:49 sysaux01.dbf
-rw-r----- 1 oracle oinstall 943726592 Nov  4 10:49 system01.dbf
-rw-r----- 1 oracle oinstall  20979712 Nov  4 10:48 temp01.dbf
-rw-r----- 1 oracle oinstall  57679872 Nov  4 10:49 undotbs01.dbf
$ ps -ef|grep tail
oracle     785  5117  0 10:49 pts/1    00:00:00 tail -n +0 -f /proc/628/fd/261
oracle     844  5117  0 10:51 pts/1    00:00:00 grep tail
$ ll /proc/785/fd
total 0
lrwx------ 1 oracle oinstall 64 Nov  4 10:52 0 -> /dev/pts/1
l-wx------ 1 oracle oinstall 64 Nov  4 10:52 1 -> /dev/null
lrwx------ 1 oracle oinstall 64 Nov  4 10:50 2 -> /dev/pts/1
lr-x------ 1 oracle oinstall 64 Nov  4 10:52 3 -> /u01/app/oracle/oradata/KV/users01.dbf (deleted)
$ cp  /proc/785/fd/3 users01.dbf
$ ll
total 2218432
-rw-r--r-- 1 oracle oinstall        74 Oct 30 12:04 bbed.par
-rw-r--r-- 1 oracle oinstall     18432 Oct 30 13:48 bifile.bbd
-rw-r----- 1 oracle oinstall   9814016 Nov  4 10:49 control01.ctl
-rw-r----- 1 oracle oinstall 362422272 Nov  4 10:48 example01.dbf
-rw-r--r-- 1 oracle oinstall       259 Oct 30 12:03 filelist.txt
-rw-r--r-- 1 oracle oinstall      1725 Oct 30 13:48 log.bbd
-rw-r----- 1 oracle oinstall  52429312 Nov  4 10:48 redo01.log
-rw-r----- 1 oracle oinstall  52429312 Nov  4 10:48 redo02.log
-rw-r----- 1 oracle oinstall  52429312 Nov  4 10:48 redo03.log
-rw-r----- 1 oracle oinstall 639639552 Nov  4 10:49 sysaux01.dbf
-rw-r----- 1 oracle oinstall 943726592 Nov  4 10:49 system01.dbf
-rw-r----- 1 oracle oinstall  20979712 Nov  4 10:48 temp01.dbf
-rw-r----- 1 oracle oinstall  57679872 Nov  4 10:49 undotbs01.dbf
-rw-r----- 1 oracle oinstall  83894272 Nov  4 10:52 users01.dbf
$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Tue Nov 4 10:52:39 2014

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area 1870647296 bytes
Fixed Size                  2229424 bytes
Variable Size             452987728 bytes
Database Buffers         1409286144 bytes
Redo Buffers                6144000 bytes
Database mounted.
Database opened.
SQL> select count(*) from test;

  COUNT(*)
----------
    618237

SQL> quit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options



本文出自 “徘徊在1521” 部落格,請務必保留此出處http://kiever.blog.51cto.com/771547/1571603

Oracle資料庫運行時rm誤刪檔案的解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.