Linux中误删除数据文件和归档日志的恢复方法
程序员文章站
2022-04-03 20:25:41
Linux误删除数据文件和归档日志的恢复方法,需要的朋友可以参考下... 13-01-22...
误删除前,归档日志2个,用户数据4480064条
[oracle@station90 datafile]$ sqlplus /nolog
sql> conn hr/hr
connected.
sql> host ls -l /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/*.arc
-rw-r----- 1 oracle oinstall 50048000 jan 17 16:56 /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/o1_mf_1_5_8hhh5hhp_.arc
-rw-r----- 1 oracle oinstall 50049536 jan 17 16:56 /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/o1_mf_1_6_8hhh5x0t_.arc
sql> select count(*) from test;
count(*)
----------
4480064
2、用户继续插入200w条数据,归档日志最终是3个
sql> insert into test select * from test where rownum<=2000000;
2000000 rows created.
sql> commit;
commit complete.
sql> host ls -l /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/*.arc
-rw-r----- 1 oracle oinstall 50048000 jan 17 16:56 /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/o1_mf_1_5_8hhh5hhp_.arc
-rw-r----- 1 oracle oinstall 50049536 jan 17 16:56 /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/o1_mf_1_6_8hhh5x0t_.arc
-rw-r----- 1 oracle oinstall 50049024 jan 17 17:08 /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/o1_mf_1_7_8hhhx0h7_.arc
3、删除所有的归档日志,其中一个归档是新插入了200w记录产生的归档
sql> host rm /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/*.arc
sql> host ls -l /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/*.arc
ls: /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/*.arc: no such file or directory
4、最终test的数据量是6480064
sql> select count(*) from test;
count(*)
----------
6480064
5、删除users表空间下的所有数据文件
sql> conn /as sysdba
connected.
sql> select file_name from dba_data_files where tablespace_name='users';
file_name
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf
/u01/app/oracle/oradata/orcl/datafile/users01.dbf
sql> host rm /u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf
sql> host rm /u01/app/oracle/oradata/orcl/datafile/users01.dbf
5、在users表空间创建表,报错了
sql> conn hr/hr
connected.
sql> create table test33 (a number);
create table test33 (a number)
*
error at line 1:
ora-01116: error in opening database file 4
ora-01110: data file 4:
'/u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf'
ora-27041: unable to open file
linux-x86_64 error: 2: no such file or directory
additional information: 3
sql> quit
disconnected from oracle database 10g enterprise edition release 10.2.0.1.0 - 64bit production
with the partitioning, olap and data mining options
5、查看dbwr进程pid
[oracle@station90 ~]$ ps -ef | grep ora_dbw | grep -v grep
oracle 9878 1 0 16:20 ? 00:00:02 ora_dbw0_orcl
6、dbwr进程会打开所有数据文件的句柄。在/proc目录中可以查到,目录名是进程pid,fd表示文件描述符
[oracle@station90 ~]$ cd /proc/9878/fd
[oracle@station90 fd]$ ls -l
total 0
lr-x------ 1 oracle oinstall 64 jan 17 17:11 0 -> /dev/null
lr-x------ 1 oracle oinstall 64 jan 17 17:11 1 -> /dev/null
lrwx------ 1 oracle oinstall 64 jan 17 17:11 10 -> /u01/app/oracle/admin/orcl/adump/ora_9870.aud
lr-x------ 1 oracle oinstall 64 jan 17 17:11 11 -> /dev/zero
lr-x------ 1 oracle oinstall 64 jan 17 17:11 12 -> /dev/zero
lr-x------ 1 oracle oinstall 64 jan 17 17:11 13 -> /u01/app/oracle/product/10.2.0/db_1/rdbms/mesg/oraus.msb
lrwx------ 1 oracle oinstall 64 jan 17 17:11 14 -> /u01/app/oracle/product/10.2.0/db_1/dbs/hc_orcl.dat
lrwx------ 1 oracle oinstall 64 jan 17 17:11 15 -> /u01/app/oracle/product/10.2.0/db_1/dbs/lkorcl
lrwx------ 1 oracle oinstall 64 jan 17 17:11 16 -> /u01/app/oracle/oradata/orcl/controlfile/o1_mf_5qd60qj5_.ctl
lrwx------ 1 oracle oinstall 64 jan 17 17:11 17 -> /u01/app/oracle/flash_recovery_area/orcl/controlfile/o1_mf_5qd60qq6_.ctl
lrwx------ 1 oracle oinstall 64 jan 17 17:11 18 -> /u01/app/oracle/oradata/orcl/datafile/o1_mf_system_5qd5zcop_.dbf
lrwx------ 1 oracle oinstall 64 jan 17 17:11 19 -> /u01/app/oracle/oradata/orcl/datafile/o1_mf_undotbs1_5qd5zcrn_.dbf
lr-x------ 1 oracle oinstall 64 jan 17 17:11 2 -> /dev/null
lrwx------ 1 oracle oinstall 64 jan 17 17:11 20 -> /u01/app/oracle/oradata/orcl/datafile/o1_mf_sysaux_5qd5zcot_.dbf
lrwx------ 1 oracle oinstall 64 jan 17 17:11 21 -> socket:[138219]
lrwx------ 1 oracle oinstall 64 jan 17 17:11 22 -> /u01/app/oracle/oradata/orcl/datafile/o1_mf_example_5qd616wf_.dbf
lrwx------ 1 oracle oinstall 64 jan 17 17:11 23 -> /u01/app/oracle/oradata/orcl/datafile/o1_mf_temp_5qd614y5_.tmp
lr-x------ 1 oracle oinstall 64 jan 17 17:11 24 -> /u01/app/oracle/product/10.2.0/db_1/rdbms/mesg/oraus.msb
lrwx------ 1 oracle oinstall 64 jan 17 17:11 25 -> /u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf (deleted)
lrwx------ 1 oracle oinstall 64 jan 17 17:11 26 -> /u01/app/oracle/oradata/orcl/datafile/users01.dbf (deleted)
lr-x------ 1 oracle oinstall 64 jan 17 17:11 3 -> /dev/null
lr-x------ 1 oracle oinstall 64 jan 17 17:11 4 -> /dev/null
l-wx------ 1 oracle oinstall 64 jan 17 17:11 5 -> /u01/app/oracle/admin/orcl/udump/orcl_ora_9870.trc
l-wx------ 1 oracle oinstall 64 jan 17 17:11 6 -> /u01/app/oracle/admin/orcl/bdump/alert_orcl.log
lrwx------ 1 oracle oinstall 64 jan 17 17:11 7 -> /u01/app/oracle/product/10.2.0/db_1/dbs/lkinstorcl (deleted)
l-wx------ 1 oracle oinstall 64 jan 17 17:11 8 -> /u01/app/oracle/admin/orcl/bdump/alert_orcl.log
lrwx------ 1 oracle oinstall 64 jan 17 17:11 9 -> /u01/app/oracle/product/10.2.0/db_1/dbs/hc_orcl.dat
[oracle@station90 fd]$ ls -l | grep users
lrwx------ 1 oracle oinstall 64 jan 17 17:11 25 -> /u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf (deleted)
lrwx------ 1 oracle oinstall 64 jan 17 17:11 26 -> /u01/app/oracle/oradata/orcl/datafile/users01.dbf (deleted)
注意其中"/u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf(deleted) "字样,表示该文件已经被删除
7、直接cp该句柄文件名回原位置
[oracle@station90fd]$cp25 /u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf
[oracle@station90 fd]$ cp 26 /u01/app/oracle/oradata/orcl/datafile/users01.dbf
8、恢复数据文件
[oracle@station90 fd]$ sqlplus /nolog
sql*plus: release 10.2.0.1.0 - production on thu jan 17 17:12:27 2013
copyright (c) 1982, 2005, oracle. all rights reserved.
sql> conn /as sysdba
connected.
sql> select file_id,file_name from dba_data_files where tablespace_name='users';
file_id
----------
file_name
--------------------------------------------------------------------------------
4
/u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf
6
/u01/app/oracle/oradata/orcl/datafile/users01.dbf
sql> alter database datafile 4 offline;
database altered.
sql> recover datafile 4;
media recovery complete.
sql> alter database datafile 4 online;
database altered.
sql> alter database datafile 6 offline;
database altered.
sql> recover datafile 6;
media recovery complete.
sql> alter database datafile 6 online;
database altered.
9、数据完全恢复
sql> conn hr/hr
connected.
sql> select count(*) from test;
count(*)
----------
6480064
恢复的原理是,在linux操作系统中,如果文件从操作系统级别被rm掉,之前打开该文件的进程仍然持有相应的文件句柄,所指向的文件仍然可以读写,并且该文件的文件描述符可以从/proc目录中获得。但是要注意的是,此时如果关闭数据库,则此句柄会消失,那么除了扫描磁盘进行文件恢复之外就没有其它方法了,因此在数据库出现问题的时候,如果不确认情况的复杂程度,千万不要随便关闭数据库。重启数据库往往是没有意义的,甚至是致命的,
另外,若控制文件被rm了,通过这种方式是无法恢复的,
同时,通过linux的rm删除了数据文件,甚至连归档日志都全部删除了,只要数据库没有崩溃,dbw0进程还在,就可以将数据完全恢复,和是否有归档日志没有关系,但是和是否开启了归档是有区别的
复制代码
代码如下:[oracle@station90 datafile]$ sqlplus /nolog
sql> conn hr/hr
connected.
sql> host ls -l /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/*.arc
-rw-r----- 1 oracle oinstall 50048000 jan 17 16:56 /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/o1_mf_1_5_8hhh5hhp_.arc
-rw-r----- 1 oracle oinstall 50049536 jan 17 16:56 /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/o1_mf_1_6_8hhh5x0t_.arc
sql> select count(*) from test;
count(*)
----------
4480064
2、用户继续插入200w条数据,归档日志最终是3个
复制代码
代码如下:sql> insert into test select * from test where rownum<=2000000;
2000000 rows created.
sql> commit;
commit complete.
sql> host ls -l /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/*.arc
-rw-r----- 1 oracle oinstall 50048000 jan 17 16:56 /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/o1_mf_1_5_8hhh5hhp_.arc
-rw-r----- 1 oracle oinstall 50049536 jan 17 16:56 /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/o1_mf_1_6_8hhh5x0t_.arc
-rw-r----- 1 oracle oinstall 50049024 jan 17 17:08 /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/o1_mf_1_7_8hhhx0h7_.arc
3、删除所有的归档日志,其中一个归档是新插入了200w记录产生的归档
复制代码
代码如下:sql> host rm /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/*.arc
sql> host ls -l /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/*.arc
ls: /u01/app/oracle/flash_recovery_area/orcl/archivelog/2013_01_17/*.arc: no such file or directory
4、最终test的数据量是6480064
复制代码
代码如下:sql> select count(*) from test;
count(*)
----------
6480064
5、删除users表空间下的所有数据文件
复制代码
代码如下:sql> conn /as sysdba
connected.
sql> select file_name from dba_data_files where tablespace_name='users';
file_name
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf
/u01/app/oracle/oradata/orcl/datafile/users01.dbf
sql> host rm /u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf
sql> host rm /u01/app/oracle/oradata/orcl/datafile/users01.dbf
5、在users表空间创建表,报错了
复制代码
代码如下:sql> conn hr/hr
connected.
sql> create table test33 (a number);
create table test33 (a number)
*
error at line 1:
ora-01116: error in opening database file 4
ora-01110: data file 4:
'/u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf'
ora-27041: unable to open file
linux-x86_64 error: 2: no such file or directory
additional information: 3
sql> quit
disconnected from oracle database 10g enterprise edition release 10.2.0.1.0 - 64bit production
with the partitioning, olap and data mining options
5、查看dbwr进程pid
复制代码
代码如下:[oracle@station90 ~]$ ps -ef | grep ora_dbw | grep -v grep
oracle 9878 1 0 16:20 ? 00:00:02 ora_dbw0_orcl
6、dbwr进程会打开所有数据文件的句柄。在/proc目录中可以查到,目录名是进程pid,fd表示文件描述符
复制代码
代码如下:[oracle@station90 ~]$ cd /proc/9878/fd
[oracle@station90 fd]$ ls -l
total 0
lr-x------ 1 oracle oinstall 64 jan 17 17:11 0 -> /dev/null
lr-x------ 1 oracle oinstall 64 jan 17 17:11 1 -> /dev/null
lrwx------ 1 oracle oinstall 64 jan 17 17:11 10 -> /u01/app/oracle/admin/orcl/adump/ora_9870.aud
lr-x------ 1 oracle oinstall 64 jan 17 17:11 11 -> /dev/zero
lr-x------ 1 oracle oinstall 64 jan 17 17:11 12 -> /dev/zero
lr-x------ 1 oracle oinstall 64 jan 17 17:11 13 -> /u01/app/oracle/product/10.2.0/db_1/rdbms/mesg/oraus.msb
lrwx------ 1 oracle oinstall 64 jan 17 17:11 14 -> /u01/app/oracle/product/10.2.0/db_1/dbs/hc_orcl.dat
lrwx------ 1 oracle oinstall 64 jan 17 17:11 15 -> /u01/app/oracle/product/10.2.0/db_1/dbs/lkorcl
lrwx------ 1 oracle oinstall 64 jan 17 17:11 16 -> /u01/app/oracle/oradata/orcl/controlfile/o1_mf_5qd60qj5_.ctl
lrwx------ 1 oracle oinstall 64 jan 17 17:11 17 -> /u01/app/oracle/flash_recovery_area/orcl/controlfile/o1_mf_5qd60qq6_.ctl
lrwx------ 1 oracle oinstall 64 jan 17 17:11 18 -> /u01/app/oracle/oradata/orcl/datafile/o1_mf_system_5qd5zcop_.dbf
lrwx------ 1 oracle oinstall 64 jan 17 17:11 19 -> /u01/app/oracle/oradata/orcl/datafile/o1_mf_undotbs1_5qd5zcrn_.dbf
lr-x------ 1 oracle oinstall 64 jan 17 17:11 2 -> /dev/null
lrwx------ 1 oracle oinstall 64 jan 17 17:11 20 -> /u01/app/oracle/oradata/orcl/datafile/o1_mf_sysaux_5qd5zcot_.dbf
lrwx------ 1 oracle oinstall 64 jan 17 17:11 21 -> socket:[138219]
lrwx------ 1 oracle oinstall 64 jan 17 17:11 22 -> /u01/app/oracle/oradata/orcl/datafile/o1_mf_example_5qd616wf_.dbf
lrwx------ 1 oracle oinstall 64 jan 17 17:11 23 -> /u01/app/oracle/oradata/orcl/datafile/o1_mf_temp_5qd614y5_.tmp
lr-x------ 1 oracle oinstall 64 jan 17 17:11 24 -> /u01/app/oracle/product/10.2.0/db_1/rdbms/mesg/oraus.msb
lrwx------ 1 oracle oinstall 64 jan 17 17:11 25 -> /u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf (deleted)
lrwx------ 1 oracle oinstall 64 jan 17 17:11 26 -> /u01/app/oracle/oradata/orcl/datafile/users01.dbf (deleted)
lr-x------ 1 oracle oinstall 64 jan 17 17:11 3 -> /dev/null
lr-x------ 1 oracle oinstall 64 jan 17 17:11 4 -> /dev/null
l-wx------ 1 oracle oinstall 64 jan 17 17:11 5 -> /u01/app/oracle/admin/orcl/udump/orcl_ora_9870.trc
l-wx------ 1 oracle oinstall 64 jan 17 17:11 6 -> /u01/app/oracle/admin/orcl/bdump/alert_orcl.log
lrwx------ 1 oracle oinstall 64 jan 17 17:11 7 -> /u01/app/oracle/product/10.2.0/db_1/dbs/lkinstorcl (deleted)
l-wx------ 1 oracle oinstall 64 jan 17 17:11 8 -> /u01/app/oracle/admin/orcl/bdump/alert_orcl.log
lrwx------ 1 oracle oinstall 64 jan 17 17:11 9 -> /u01/app/oracle/product/10.2.0/db_1/dbs/hc_orcl.dat
[oracle@station90 fd]$ ls -l | grep users
lrwx------ 1 oracle oinstall 64 jan 17 17:11 25 -> /u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf (deleted)
lrwx------ 1 oracle oinstall 64 jan 17 17:11 26 -> /u01/app/oracle/oradata/orcl/datafile/users01.dbf (deleted)
注意其中"/u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf(deleted) "字样,表示该文件已经被删除
7、直接cp该句柄文件名回原位置
复制代码
代码如下:[oracle@station90fd]$cp25 /u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf
[oracle@station90 fd]$ cp 26 /u01/app/oracle/oradata/orcl/datafile/users01.dbf
8、恢复数据文件
复制代码
代码如下:[oracle@station90 fd]$ sqlplus /nolog
sql*plus: release 10.2.0.1.0 - production on thu jan 17 17:12:27 2013
copyright (c) 1982, 2005, oracle. all rights reserved.
sql> conn /as sysdba
connected.
sql> select file_id,file_name from dba_data_files where tablespace_name='users';
file_id
----------
file_name
--------------------------------------------------------------------------------
4
/u01/app/oracle/oradata/orcl/datafile/o1_mf_users_5qd5zcsh_.dbf
6
/u01/app/oracle/oradata/orcl/datafile/users01.dbf
sql> alter database datafile 4 offline;
database altered.
sql> recover datafile 4;
media recovery complete.
sql> alter database datafile 4 online;
database altered.
sql> alter database datafile 6 offline;
database altered.
sql> recover datafile 6;
media recovery complete.
sql> alter database datafile 6 online;
database altered.
9、数据完全恢复
复制代码
代码如下:sql> conn hr/hr
connected.
sql> select count(*) from test;
count(*)
----------
6480064
恢复的原理是,在linux操作系统中,如果文件从操作系统级别被rm掉,之前打开该文件的进程仍然持有相应的文件句柄,所指向的文件仍然可以读写,并且该文件的文件描述符可以从/proc目录中获得。但是要注意的是,此时如果关闭数据库,则此句柄会消失,那么除了扫描磁盘进行文件恢复之外就没有其它方法了,因此在数据库出现问题的时候,如果不确认情况的复杂程度,千万不要随便关闭数据库。重启数据库往往是没有意义的,甚至是致命的,
另外,若控制文件被rm了,通过这种方式是无法恢复的,
同时,通过linux的rm删除了数据文件,甚至连归档日志都全部删除了,只要数据库没有崩溃,dbw0进程还在,就可以将数据完全恢复,和是否有归档日志没有关系,但是和是否开启了归档是有区别的
参考盖国强的链接:
51cto博文索引,共同进步