如何恢复在未停机情况下误删除的Oracle数据文件

如果自己搞不定可以找诗檀软件专业ORACLE数据库修复团队成员帮您恢复!

诗檀软件专业数据库修复团队

服务热线 : 13764045638    QQ号:47079569    邮箱:service@parnassusdata.com

 

如果你不小心从生产数据库移动了一个数据文件? 首先,不要惊慌!只要你的数据库还在有很容易的方法恢复删除的数据文件,下面的程序在linux上工作,但是这个方法有望在其他平台上工作,该程序甚至同样起作用如果你的数据库处于 NOARCHIVELOG 模式下。

 

你可能通过谷歌看到了这个帖子,并急于找到解决问题地方法,这里就是了。

在两个阶段下完成恢复

阶段 1: 立即恢复防止出现 Oracle 错误 ( ORA-27041 “不能打开文件”, ORA-01110, ORA-01116)

  • 为正确的数据库找到DBWRITER PID 

ps -ef | grep dbw0_SID_OF_YOUR_DB

oracle   12272     1  0 10:55 ?        00:00:00 ora_dbw0_test

oracle   12941 11501  0 12:36 pts/0    00:00:00 grep dbw0_test

  • DBWRITER 过程列出被删除的文件句柄

ls -l /proc/_PID_OF_DBWRITER/fd | grep deleted

lrwx——  1 oracle oinstall 64 Oct 15 11:24 10 -> /home/oracle/product/10.2.0.2/dbs/lkinsttest (deleted)

lrwx——  1 oracle oinstall 64 Oct 15 11:24 23 -> /ra5a/orabkp/test/TEST/datafile/o1_mf_lost_3k6xzjpm_.dbf (deleted)

  • 与原来的名称一起创建符号链接到你的数据文件。

ln -s /proc/PID_OF_DBWRITER/fd/23 /ra5a/orabkp/test/TEST/datafile/o1_mf_lost_3k6xzjpm_.dbf

就这样,现在你再也不会收到错误,但是,如果你的数据库现在出现故障,你将永远丢失数据文件。

阶段 2: 复原文件

归档日志数据库

  • (可选的) 发出一个检查点使文件在线会减少恢复时间,取决于数据文件的活动,不幸的是你不能对单个文件进行检查点操作,因此检查点操作会花费些时间。

alter system checkpoint;

  • rman备份数据文件为什么是 rman? 这比你想象得要容易对不活跃的数据文件整个停机时间只有一秒,活跃的数据文件可能需要的时间更多(连同写入)

rman target /

report schema;

backup as copy datafile YOUR_DATAFILE_NUMBER format ‘/location_of_your_database/new_name_for_File.dbf’;

sql ‘alter database datafile YOUR_DATAFILE_NUMBER offline’;

switch datafile YOUR_DATAFILE_NUMBER to copy;

recover datafile YOUR_DATAFILE_NUMBER;

sql ‘alter database datafile YOUR_DATAFILE_NUMBER online’;

exit;

非归档日志数据库

  • 用只读数据文件创建表空间

select distinct tablespace_name from dba_data_files where file_name = ‘YOUR_DELETED_FILE’;

alter tablespace THE_TABLESPACE read only;

  • 将文件从符号链接复制到一个新的名字

cp SIM_LINK_DATA_FILE NEW_NAME_FOR_DATAFILE.dbf

  • 警告: 确保完成复制! 然后,使数据库崩溃。

/*WAIT FOR COPY!!!*/

shutdown abort;

  • 现在移动无效的符号链接,将数据文件重命名为原来的名称,现在小心不要移动错误的文件那将会是一个灾难:

rm -i SIM_LINK_DATA_FILE

mv NEW_NAME_FOR_DATAFILE.dbf SIM_LINK_DATA_FILE

  • 正常启动数据库,使表空间读取/写入

startup

alter tablespace THE_TABLESPACE read write;

我希望这可以帮助你摆脱讨厌的局面。

这两种情况是完全从终端捕获的,注意,我正在使用的是Oracle管理文件,这不会改变步骤。

/ra5a/orabkp/test/TEST/datafile> sqlplus ‘/ as sysdba’

SQL*Plus: Release 10.2.0.2.0 – Production on Mon Oct 15 12:31:55 2007

Copyright (c) 1982, 2005, Oracle.  All Rights Reserved.

Connected to:

Oracle Database 10g Release 10.2.0.2.0 – 64bit Production

SQL>

SQL> select tablespace_name, file_name from dba_data_files

TABLESPACE_NAME                FILE_NAME

—————————— ————————————————————

USERS                          /ra5a/orabkp/test/TEST/datafile/o1_mf_users_3k6xgwhb_.dbf

SYSAUX                         /ra5a/orabkp/test/TEST/datafile/o1_mf_sysaux_3k6xgwdf_.dbf

UNDOTBS1                       /ra5a/orabkp/test/TEST/datafile/o1_mf_undotbs1_3k6xgwg9_.dbf

SYSTEM                         /ra5a/orabkp/test/TEST/datafile/o1_mf_system_3k6xgwd4_.dbf

EXAMPLE                        /ra5a/orabkp/test/TEST/datafile/o1_mf_example_3k6xjdjw_.dbf

LOST                           /ra5a/orabkp/test/TEST/datafile/o1_mf_lost_3k74mq08_.dbf

6 rows selected.

SQL> select tablespace_name, table_name from dba_tables where owner = ‘TESTING’;

TABLESPACE_NAME                TABLE_NAME

—————————— ——————————

LOST                           LOST_TABLE

SQL> connect testing/testing

SQL> select count(*) from lost_table;

  COUNT(*)

———-

     50070

SQL> alter system flush buffer_cache;

System altered.

SQL> select count(*) from lost_table;

  COUNT(*)

———-

     50070

SQL> Disconnected from Oracle Database 10g Release 10.2.0.2.0 – 64bit Production

/ra5a/orabkp/test/TEST/datafile> ls -lF

total 1015132

-rw-r—–  1 oracle oinstall 157294592 Oct 15 12:22 o1_mf_example_3k6xjdjw_.dbf

-rw-r—–  1 oracle oinstall 104865792 Oct 15 12:22 o1_mf_lost_3k74mq08_.dbf

-rw-r—–  1 oracle oinstall 241180672 Oct 15 12:32 o1_mf_sysaux_3k6xgwdf_.dbf

-rw-r—–  1 oracle oinstall 503324672 Oct 15 12:32 o1_mf_system_3k6xgwd4_.dbf

-rw-r—–  1 oracle oinstall  20979712 Oct 15 10:17 o1_mf_temp_3k6xj9xn_.tmp

-rw-r—–  1 oracle oinstall  26222592 Oct 15 12:32 o1_mf_undotbs1_3k6xgwg9_.dbf

-rw-r—–  1 oracle oinstall   5251072 Oct 15 12:22 o1_mf_users_3k6xgwhb_.dbf

/ra5a/orabkp/test/TEST/datafile> rm o1_mf_lost_3k74mq08_.dbf

/ra5a/orabkp/test/TEST/datafile> sqlplus testing/testing

SQL*Plus: Release 10.2.0.2.0 – Production on Mon Oct 15 12:35:24 2007

Copyright (c) 1982, 2005, Oracle.  All Rights Reserved.

Connected to:

Oracle Database 10g Release 10.2.0.2.0 – 64bit Production

SQL> select count(*) from lost_table;

  COUNT(*)

———-

     50070

SQL> alter system flush buffer_cache;

System altered.

SQL> select count(*) from lost_table;

select count(*) from lost_table

*

ERROR at line 1:

ORA-01116: error in opening database file 6

ORA-01110: data file 6:

‘/ra5a/orabkp/test/TEST/datafile/o1_mf_lost_3k74mq08_.dbf’

ORA-27041: unable to open file

Linux-x86_64 Error: 2: No such file or directory

Additional information: 3

SQL>

SQL> Disconnected from Oracle Database 10g Release 10.2.0.2.0 – 64bit Production

/ra5a/orabkp/test/TEST/datafile> ps -ef|grep dbw0_test

oracle   12272     1  0 10:55 ?        00:00:00 ora_dbw0_test

oracle   12941 11501  0 12:36 pts/0    00:00:00 grep dbw0_test

/ra5a/orabkp/test/TEST/datafile> ls -l /proc/12272/fd|grep deleted

lrwx——  1 oracle oinstall 64 Oct 15 11:24 10 -> /home/oracle/product/10.2.0.2/dbs/lkinsttest (deleted)

lrwx——  1 oracle oinstall 64 Oct 15 12:17 26 -> /ra5a/orabkp/test/TEST/datafile/o1_mf_lost_3k74mq08_.dbf (deleted)

/ra5a/orabkp/test/TEST/datafile> ln -s /proc/12272/fd/26 /ra5a/orabkp/test/TEST/datafile/o1_mf_lost_3k74mq08_.dbf

/ra5a/orabkp/test/TEST/datafile> ls -lF

total 912620

-rw-r—–  1 oracle oinstall 157294592 Oct 15 12:22 o1_mf_example_3k6xjdjw_.dbf

lrwxrwxrwx  1 oracle oinstall        17 Oct 15 12:37 o1_mf_lost_3k74mq08_.dbf -> /proc/12272/fd/26

-rw-r—–  1 oracle oinstall 241180672 Oct 15 12:32 o1_mf_sysaux_3k6xgwdf_.dbf

-rw-r—–  1 oracle oinstall 503324672 Oct 15 12:32 o1_mf_system_3k6xgwd4_.dbf

-rw-r—–  1 oracle oinstall  20979712 Oct 15 10:17 o1_mf_temp_3k6xj9xn_.tmp

-rw-r—–  1 oracle oinstall  26222592 Oct 15 12:32 o1_mf_undotbs1_3k6xgwg9_.dbf

-rw-r—–  1 oracle oinstall   5251072 Oct 15 12:22 o1_mf_users_3k6xgwhb_.dbf

/ra5a/orabkp/test/TEST/datafile> sqlplus testing/testing

SQL*Plus: Release 10.2.0.2.0 – Production on Mon Oct 15 12:38:18 2007

Copyright (c) 1982, 2005, Oracle.  All Rights Reserved.

Connected to:

Oracle Database 10g Release 10.2.0.2.0 – 64bit Production

SQL> select count(*) from lost_table;

  COUNT(*)

———-

     50070

SQL> alter system flush buffer_cache;

System altered.

SQL> select count(*) from lost_table;

  COUNT(*)

———-

     50070

SQL>

SQL> Disconnected from Oracle Database 10g Release 10.2.0.2.0 – 64bit Production

/ra5a/orabkp/test/TEST/datafile> rman target /

Recovery Manager: Release 10.2.0.2.0 – Production on Mon Oct 15 12:39:48 2007

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

connected to target database: TEST (DBID=1934173752)

RMAN> report schema;

using target database control file instead of recovery catalog

Report of database schema

List of Permanent Datafiles

===========================

File Size(MB) Tablespace           RB segs Datafile Name

—- ——– ——————– ——- ————————

1    480      SYSTEM               ***     /ra5a/orabkp/test/TEST/datafile/o1_mf_system_3k6xgwd4_.dbf

2    25       UNDOTBS1             ***     /ra5a/orabkp/test/TEST/datafile/o1_mf_undotbs1_3k6xgwg9_.dbf

3    230      SYSAUX               ***     /ra5a/orabkp/test/TEST/datafile/o1_mf_sysaux_3k6xgwdf_.dbf

4    5        USERS                ***     /ra5a/orabkp/test/TEST/datafile/o1_mf_users_3k6xgwhb_.dbf

5    150      EXAMPLE              ***     /ra5a/orabkp/test/TEST/datafile/o1_mf_example_3k6xjdjw_.dbf

6    100      LOST                 ***     /ra5a/orabkp/test/TEST/datafile/o1_mf_lost_3k74mq08_.dbf

List of Temporary Files

=======================

File Size(MB) Tablespace           Maxsize(MB) Tempfile Name

—- ——– ——————– ———– ——————–

1    20       TEMP                 32767       /ra5a/orabkp/test/TEST/datafile/o1_mf_temp_3k6xj9xn_.tmp

RMAN> backup as copy datafile 6 format ‘/ra5a/orabkp/test/TEST/datafile/lost.dbf’;

Starting backup at 2007-10-15 12:40:45

allocated channel: ORA_DISK_1

channel ORA_DISK_1: sid=154 devtype=DISK

channel ORA_DISK_1: starting datafile copy

input datafile fno=00006 name=/ra5a/orabkp/test/TEST/datafile/o1_mf_lost_3k74mq08_.dbf

output filename=/ra5a/orabkp/test/TEST/datafile/lost.dbf tag=TAG20071015T124045 recid=13 stamp=636036046

channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01

Finished backup at 2007-10-15 12:40:46

RMAN> sql ‘alter database datafile 6 offline’;

sql statement: alter database datafile 6 offline

RMAN> switch datafile 6 to copy;

datafile 6 switched to datafile copy “/ra5a/orabkp/test/TEST/datafile/lost.dbf”

RMAN> recover datafile 6;

Starting recover at 2007-10-15 12:41:07

using channel ORA_DISK_1

starting media recovery

media recovery complete, elapsed time: 00:00:00

Finished recover at 2007-10-15 12:41:07

RMAN> sql ‘alter database datafile 6 online’;

sql statement: alter database datafile 6 online

RMAN>

Recovery Manager complete.

/ra5a/orabkp/test/TEST/datafile> ls -lF

total 1015132

-rw-r—–  1 oracle oinstall 104865792 Oct 15 12:41 lost.dbf

-rw-r—–  1 oracle oinstall 157294592 Oct 15 12:22 o1_mf_example_3k6xjdjw_.dbf

lrwxrwxrwx  1 oracle oinstall        17 Oct 15 12:37 o1_mf_lost_3k74mq08_.dbf -> /proc/12272/fd/26

-rw-r—–  1 oracle oinstall 241180672 Oct 15 12:32 o1_mf_sysaux_3k6xgwdf_.dbf

-rw-r—–  1 oracle oinstall 503324672 Oct 15 12:38 o1_mf_system_3k6xgwd4_.dbf

-rw-r—–  1 oracle oinstall  20979712 Oct 15 10:17 o1_mf_temp_3k6xj9xn_.tmp

-rw-r—–  1 oracle oinstall  26222592 Oct 15 12:38 o1_mf_undotbs1_3k6xgwg9_.dbf

-rw-r—–  1 oracle oinstall   5251072 Oct 15 12:22 o1_mf_users_3k6xgwhb_.dbf

/ra5a/orabkp/test/TEST/datafile> rm o1_mf_lost_3k74mq08_.dbf

/ra5a/orabkp/test/TEST/datafile> sqlplus testing/testing

SQL*Plus: Release 10.2.0.2.0 – Production on Mon Oct 15 12:42:03 2007

Copyright (c) 1982, 2005, Oracle.  All Rights Reserved.

Connected to:

Oracle Database 10g Release 10.2.0.2.0 – 64bit Production

SQL> select count(*) from lost_table;

  COUNT(*)

———-

     50070

SQL> alter system flush buffer_cache;

System altered.

SQL> select count(*) from lost_table;

  COUNT(*)

———-

     50070

SQL> Disconnected from Oracle Database 10g Release 10.2.0.2.0 – 64bit Production

Comment

*

沪ICP备14014813号-2

沪公网安备 31010802001379号