Linux 平台誤刪 oracle 數據文件的恢復方法 模擬誤操作: 數據庫在正常運行,人工直接rm 掉了數據文件。 --1.測試環境情況: $ cat /etc/redhat-release CentOS release 6.5 (Final) select file_name from dba_data_files; /u01/app/oracle/oradata/orcl/test.dbf $ sqlplus / as sysdba SQL*Plus: Release 11.2.0.4.0 Production on 星期四 3月 5 15:55:14 2015 Copyright (c) 1982, 2013, Oracle. All rights reserved. 連接到: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'; 開啟數據庫歸檔 archive log list; 數據庫日志模式 非存檔模式 自動存檔 禁用 存檔終點 USE_DB_RECOVERY_FILE_DEST 最早的聯機日志序列 3002 當前日志序列 3004 mkdir /u01/arch alter system set log_archive_dest_1='location=/u01/arch' scope=spfile; SQL> shutdown immediate 數據庫已經關閉。 已經卸載數據庫。 ORACLE 例程已經關閉。 SQL> startup mount; ORACLE 例程已經啟動。 Total System Global Area 1.0088E+10 bytes Fixed Size 2261928 bytes Variable Size 1644170328 bytes Database Buffers 8422162432 bytes Redo Buffers 19595264 bytes 數據庫裝載完畢。 SQL> alter database archivelog; 數據庫已更改。 SQL> archive log list; 數據庫日志模式 存檔模式 自動存檔 啟用 存檔終點 /u01/arch 最早的聯機日志序列 3002 下一個存檔日志序列 3004 當前日志序列 3004 SQL> alter database open; 數據庫已更改。 SQL> select open_mode from v$database; OPEN_MODE ------------------------------------------------------------ READ WRITE --2.新建測試數據 select file_name from dba_data_files; FILE_NAME -------------------------------------------------------------------------------- /u01/app/oracle/oradata/orcl/users01.dbf SQL> create tablespace wind datafile '/u01/app/oracle/oradata/orcl/wind01.dbf' size 200m; SQL> create user wind identified by wind01 default tablespace wind; SQL> grant connect,resource,dba to wind; $ sqlplus wind/wind01 create table t1 ( sid int not null primary key, sname varchar2(10) ) tablespace wind; --循環導入數據 declare maxrecords constant int:=100000; i int :=1; begin for i in 1..maxrecords loop insert into t1 values(i,'ocpyang'); end loop; dbms_output.put_line(' 成功錄入數據! '); commit; end; / select count(*) from t1; COUNT(*) ---------- 100000 --3.模擬刪除數據 SQL> col tablespace_name for a20 SQL> select tablespace_name,status from dba_tablespaces; TABLESPACE_NAME STATUS -------------------- --------------------------- SYSTEM ONLINE SYSAUX ONLINE UNDOTBS1 ONLINE TEMP ONLINE USERS ONLINE TTSPACE ONLINE OCPYANG ONLINE OCPYANGINDEX ONLINE LOBOCPYANG ONLINE LOBOCPYANG01 ONLINE LOBOCPYANG02 ONLINE TABLESPACE_NAME STATUS -------------------- --------------------------- WIND ONLINE rm -rf /u01/app/oracle/oradata/orcl/wind01.dbf ls /u01/app/oracle/oradata/orcl/ | grep wind SQL> show user; USER 為 "WIND" SQL> select count(*) from t1; COUNT(*) ---------- 100000 SQL> desc t1 名稱 是否為空? 類型 ----------------------------------------- -------- ---------------------------- SID NOT NULL NUMBER(38) SNAME VARCHAR2(10) SQL> delete from t1 where sid>99000; 已刪除1000行。 SQL> commit; 提交完成。 SQL> select count(*) from t1; COUNT(*) ---------- 99000 --4.恢復 ps -eaf|grep dbw0 |grep -v grep oracle 1928 1 0 15:59 ? 00:00:00 ora_dbw0_orcl SQL> col tablespace_name for a20 SQL> select tablespace_name,status from dba_tablespaces; TABLESPACE_NAME STATUS -------------------- --------------------------- SYSTEM ONLINE SYSAUX ONLINE UNDOTBS1 ONLINE TEMP ONLINE USERS ONLINE TTSPACE ONLINE OCPYANG ONLINE OCPYANGINDEX ONLINE LOBOCPYANG ONLINE LOBOCPYANG01 ONLINE LOBOCPYANG02 ONLINE TABLESPACE_NAME STATUS -------------------- --------------------------- WIND ONLINE cd /proc/1928/fd ll lr-x------ 1 oracle oinstall 64 Mar 5 16:20 0 -> /dev/null l-wx------ 1 oracle oinstall 64 Mar 5 16:20 1 -> /dev/null lrwx------ 1 oracle oinstall 64 Mar 5 16:20 10 -> /u01/app/oracle/product/11.2.0/db_1/dbs/lkORCL lr-x------ 1 oracle oinstall 64 Mar 5 16:20 11 -> /u01/app/oracle/product/11.2.0/db_1/rdbms/mesg/oraus.msb l-wx------ 1 oracle oinstall 64 Mar 5 16:20 2 -> /dev/null lrwx------ 1 oracle oinstall 64 Mar 5 16:20 256 -> /u01/app/oracle/oradata/orcl/control01.ctl lrwx------ 1 oracle oinstall 64 Mar 5 16:20 257 -> /u01/app/oracle/fast_recovery_area/orcl/control02.ctl lrwx------ 1 oracle oinstall 64 Mar 5 16:20 258 -> /u01/app/oracle/oradata/orcl/system01.dbf lrwx------ 1 oracle oinstall 64 Mar 5 16:20 259 -> /u01/app/oracle/oradata/orcl/sysaux01.dbf lrwx------ 1 oracle oinstall 64 Mar 5 16:20 260 -> /u01/app/oracle/oradata/orcl/undotbs01.dbf lrwx------ 1 oracle oinstall 64 Mar 5 16:20 261 -> /u01/app/oracle/oradata/orcl/users01.dbf lrwx------ 1 oracle oinstall 64 Mar 5 16:20 262 -> /u01/app/oracle/oradata/orcl/ttspace01.dbf lrwx------ 1 oracle oinstall 64 Mar 5 16:20 263 -> /u01/app/oracle/oradata/orcl/ocpyang01.dbf lrwx------ 1 oracle oinstall 64 Mar 5 16:20 264 -> /u01/app/oracle/oradata/orcl/ocpyangindex01.dbf lrwx------ 1 oracle oinstall 64 Mar 5 16:20 265 -> /u01/app/oracle/oradata/orcl/lobocpyang01.dbf lrwx------ 1 oracle oinstall 64 Mar 5 16:20 266 -> /u01/app/oracle/oradata/orcl/lobocpyang0101.dbf lrwx------ 1 oracle oinstall 64 Mar 5 16:20 267 -> /u01/app/oracle/oradata/orcl/lobocpyang0202.dbf lrwx------ 1 oracle oinstall 64 Mar 5 16:20 268 -> /u01/app/oracle/oradata/orcl/temp01.dbf lrwx------ 1 oracle oinstall 64 Mar 5 16:20 269 -> /u01/app/oracle/oradata/orcl/wind01.dbf (deleted) --注 lr-x------ 1 oracle oinstall 64 Mar 5 16:20 3 -> /dev/null lr-x------ 1 oracle oinstall 64 Mar 5 16:20 4 -> /dev/null lr-x------ 1 oracle oinstall 64 Mar 5 16:20 5 -> /dev/null lr-x------ 1 oracle oinstall 64 Mar 5 16:20 6 -> /u01/app/oracle/product/11.2.0/db_1/rdbms/mesg/oraus.msb lr-x------ 1 oracle oinstall 64 Mar 5 16:20 7 -> /proc/1928/fd lr-x------ 1 oracle oinstall 64 Mar 5 16:20 8 -> /dev/zero lrwx------ 1 oracle oinstall 64 Mar 5 16:20 9 -> /u01/app/oracle/product/11.2.0/db_1/dbs/hc_orcl.dat 直接cp該句柄文件名回原位置: $ pwd /proc/1928/fd cp /proc/1928/fd/269 /u01/app/oracle/oradata/orcl/wind01.dbf NOTE: 由於數據庫一直是打開使用的,數據在不斷變更則SCN也會不斷的變化, cp數據文件和數據庫當前的信息明顯不一致,此時需要對數據文件進行recover. --脫機數據文件 alter database datafile '/u01/app/oracle/oradata/orcl/wind01.dbf' offline; select count(*) from t1; 第 1 行出現錯誤: ORA-00376: 此時無法讀取文件 11 ORA-01110: 數據文件 11: '/u01/app/oracle/oradata/orcl/wind01.dbf' --恢復數據文件 recover datafile '/u01/app/oracle/oradata/orcl/wind01.dbf'; --聯機數據文件 alter database datafile '/u01/app/oracle/oradata/orcl/wind01.dbf' online; select count(*) from t1; COUNT(*) ---------- 99000 成功恢復. ----5.簡單驗證 ls /u01/app/oracle/oradata/orcl/ | grep wind wind01.dbf select count(*) from t1; COUNT(*) ---------- 99000 alter system switch logfile; SQL> shutdown immediate; 數據庫已經關閉。 已經卸載數據庫。 ORACLE 例程已經關閉。 SQL> startup ORACLE 例程已經啟動。 Total System Global Area 1.0088E+10 bytes Fixed Size 2261928 bytes Variable Size 1644170328 bytes Database Buffers 8422162432 bytes Redo Buffers 19595264 bytes 數據庫裝載完畢。 數據庫已經打開。 SQL> insert into t1 values(9999999,'rmfiletest'); 已創建 1 行。 SQL> commit; 提交完成。 SQL> select * from t1 where rownum=1 order by sid desc; SID SNAME ---------- ------------------------------ 9999999 rmfiletest