linux中誤刪除oracle資料檔案的恢復操作

eric0435發表於2013-06-08

下面來模擬誤刪除users表空中的資料檔案users01.dbf

[root@jingyong jingyong]# ls
control01.ctl example01_bak.dbf jy01.dbf redo02.log sysaux01.dbf system01.dbf undotbs01.dbf users01.dbf.bak
control02.ctl example01.dbf redo01.log redo03.log system_01.dbf temp01.dbf users01.dbf

刪除users01.dbf
[root@jingyong jingyong]# rm -rf users01.dbf
SQL> create table testjy(id number(20));
ERROR at line 1:
ORA-01116: error in opening database file 4
ORA-01110: data file 4:
‘/u01/app/oracle/product/11.2.0/oradata/jingyong/users01.dbf’
ORA-27041: unable to open file
Linux Error: 2: No such file or directory
Additional information: 3

檢查dbwr的程式PID
[root@jingyong ~]# ps -ef|grep dbw0|grep -v grep
oracle 2236 1 0 06:40 ? 00:00:01 ora_dbw0_jingyong

dbwr會開啟所有資料檔案的控制程式碼。在proc目錄中可以查到,目錄名是程式PID,fd表示檔案描述符
[root@jingyong ~]# cd /proc/2236/fd

[root@jingyong fd]# ls -l
total 0
lr-x—— 1 oracle oinstall 64 May 31 08:15 0 -> /dev/null
l-wx—— 1 oracle oinstall 64 May 31 08:15 1 -> /dev/null
l-wx—— 1 oracle oinstall 64 May 31 08:15 10 -> /u01/app/oracle/diag/rdbms/jingyong/jingyong/trace/jingyong_ora_2213.trc
l-wx—— 1 oracle oinstall 64 May 31 08:15 11 -> /u01/app/oracle/diag/rdbms/jingyong/jingyong/trace/jingyong_ora_2213.trm
lr-x—— 1 oracle oinstall 64 May 31 08:15 12 -> /u01/app/oracle/product/11.2.0/db/rdbms/mesg/oraus.msb
lr-x—— 1 oracle oinstall 64 May 31 08:15 13 -> /dev/zero
lr-x—— 1 oracle oinstall 64 May 31 08:15 14 -> /proc/2236/fd
lr-x—— 1 oracle oinstall 64 May 31 08:15 15 -> /dev/zero
lrwx—— 1 oracle oinstall 64 May 31 08:15 16 -> /u01/app/oracle/product/11.2.0/db/dbs/hc_jingyong.dat
lrwx—— 1 oracle oinstall 64 May 31 08:15 17 -> /u01/app/oracle/product/11.2.0/db/dbs/lkJINGYONG
lrwx—— 1 oracle oinstall 64 May 31 08:15 18 -> /u01/app/oracle/product/11.2.0/oradata/jingyong/control01.ctl
lrwx—— 1 oracle oinstall 64 May 31 08:15 19 -> /u01/app/oracle/product/11.2.0/oradata/jingyong/control02.ctl
l-wx—— 1 oracle oinstall 64 May 31 08:15 2 -> /dev/null
lrwx—— 1 oracle oinstall 64 May 31 08:15 20 -> /u01/app/oracle/product/11.2.0/oradata/jingyong/system01.dbf
lrwx—— 1 oracle oinstall 64 May 31 08:15 21 -> /u01/app/oracle/product/11.2.0/oradata/jingyong/sysaux01.dbf
lrwx—— 1 oracle oinstall 64 May 31 08:15 22 -> /u01/app/oracle/product/11.2.0/oradata/jingyong/undotbs01.dbf
lrwx—— 1 oracle oinstall 64 May 31 08:15 23 -> /u01/app/oracle/product/11.2.0/oradata/jingyong/users01.dbf (deleted)
lrwx—— 1 oracle oinstall 64 May 31 08:15 24 -> /u01/app/oracle/product/11.2.0/oradata/jingyong/example01.dbf
lrwx—— 1 oracle oinstall 64 May 31 08:15 25 -> /u01/app/oracle/product/11.2.0/oradata/jingyong/jy01.dbf
lrwx—— 1 oracle oinstall 64 May 31 08:15 26 -> /u01/app/oracle/product/11.2.0/oradata/jingyong/temp01.dbf
lr-x—— 1 oracle oinstall 64 May 31 08:15 27 -> /u01/app/oracle/product/11.2.0/db/rdbms/mesg/oraus.msb
l-wx—— 1 oracle oinstall 64 May 31 08:15 3 -> /u01/app/oracle/product/11.2.0/db/rdbms/log/jingyong_ora_2213.trc
lr-x—— 1 oracle oinstall 64 May 31 08:15 4 -> /dev/null
lr-x—— 1 oracle oinstall 64 May 31 08:15 5 -> /dev/null
lr-x—— 1 oracle oinstall 64 May 31 08:15 6 -> /dev/null
lrwx—— 1 oracle oinstall 64 May 31 08:15 7 -> /u01/app/oracle/product/11.2.0/db/dbs/hc_jingyong.dat

注意其中”/u01/app/oracle/product/11.2.0/oradata/jingyong/users01.dbf (deleted)”字樣,表示該檔案已經被刪除,
直接cp該控制程式碼檔名回原位置
[root@jingyong fd]# cp 23 /u01/app/oracle/product/11.2.0/oradata/jingyong/users01.dbf

資料檔案users01.dbf恢復回來了,因為了用的是root使用者操作的要修改一下許可權
[root@jingyong jingyong]# ls -lrt
total 2564428
-rw-r—– 1 root root 723525632 May 16 13:33 system_01.dbf
-rw-r—– 1 root root 104865792 May 22 15:46 example01_bak.dbf
-rw-r—– 1 oracle oinstall 52429312 May 31 06:40 redo02.log
-rw-r—– 1 oracle oinstall 52429312 May 31 06:40 redo01.log
-rw-r—– 1 oracle oinstall 1056768 May 31 06:40 jy01.dbf
-rw-r—– 1 oracle oinstall 104865792 May 31 06:40 example01.dbf
-rw-r—– 1 root root 24911872 May 31 08:16 users01.dbf.bak
-rw-r—– 1 oracle oinstall 31465472 May 31 08:20 temp01.dbf
-rw-r—– 1 oracle oinstall 608182272 May 31 08:21 sysaux01.dbf
-rw-r—– 1 oracle oinstall 104865792 May 31 08:22 undotbs01.dbf
-rw-r—– 1 oracle oinstall 723525632 May 31 08:22 system01.dbf
-rw-r—– 1 oracle oinstall 52429312 May 31 08:22 redo03.log
-rw-r—– 1 root root 24911872 May 31 08:23 users01.dbf
-rw-r—– 1 oracle oinstall 10076160 May 31 08:23 control02.ctl
-rw-r—– 1 oracle oinstall 10076160 May 31 08:23 control01.ctl

[root@jingyong jingyong]# chown oracle:oinstall users01.dbf
[root@jingyong jingyong]# chmod 777 users01.dbf
[root@jingyong jingyong]# ls -lrt
total 2564428
-rw-r—– 1 root root 723525632 May 16 13:33 system_01.dbf
-rw-r—– 1 root root 104865792 May 22 15:46 example01_bak.dbf
-rw-r—– 1 oracle oinstall 52429312 May 31 06:40 redo02.log
-rw-r—– 1 oracle oinstall 52429312 May 31 06:40 redo01.log
-rw-r—– 1 oracle oinstall 1056768 May 31 06:40 jy01.dbf
-rw-r—– 1 oracle oinstall 104865792 May 31 06:40 example01.dbf
-rw-r—– 1 root root 24911872 May 31 08:16 users01.dbf.bak
-rw-r—– 1 oracle oinstall 31465472 May 31 08:20 temp01.dbf
-rw-r—– 1 oracle oinstall 723525632 May 31 08:22 system01.dbf
-rwxrwxrwx 1 oracle oinstall 24911872 May 31 08:23 users01.dbf
-rw-r—– 1 oracle oinstall 104865792 May 31 08:23 undotbs01.dbf
-rw-r—– 1 oracle oinstall 608182272 May 31 08:23 sysaux01.dbf
-rw-r—– 1 oracle oinstall 52429312 May 31 08:23 redo03.log
-rw-r—– 1 oracle oinstall 10076160 May 31 08:23 control02.ctl
-rw-r—– 1 oracle oinstall 10076160 May 31 08:23 control01.ctl

進行資料檔案恢復
[oracle@jingyong ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Fri May 31 08:24:35 2013

Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 – Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> alter database datafile 4 offline;

Database altered.

SQL> recover datafile 4;
Media recovery complete.
SQL> alter database datafile 4 online;

Database altered.

恢復的原理是,在Linux作業系統中,如果檔案從作業系統級別被rm掉,之前開啟該檔案的程式仍然持有相應的檔案控制程式碼,所指向的檔案仍然可以讀寫,並且該檔案的檔案描述符可以從/proc目錄中獲得。但是要注意的是,此時如果關閉資料庫,則此控制程式碼會消失.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26015009/viewspace-763506/,如需轉載,請註明出處,否則將追究法律責任。

相關文章