Linux(Unix)中誤刪除的文件恢復(fù)方法
發(fā)布時(shí)間:2013-01-22 22:05:10 作者:佚名
我要評(píng)論

今天有客戶(hù)的數(shù)據(jù)庫(kù)意外被刪除了整個(gè)目錄中的數(shù)據(jù)文件,操作系統(tǒng)級(jí)別的刪除,然而幸運(yùn)的是這個(gè)數(shù)據(jù)庫(kù)沒(méi)有崩潰,仍然處于open狀態(tài)的時(shí)候,客戶(hù)就發(fā)現(xiàn)了問(wèn)題,求助到我們,最終完整地恢復(fù)了所有數(shù)據(jù)文件
在Linux下大致重新演示一下恢復(fù)的過(guò)程,恢復(fù)的步驟與數(shù)據(jù)庫(kù)版本沒(méi)有太大關(guān)系,與操作系統(tǒng)的不同會(huì)有所不同。
1. 在數(shù)據(jù)庫(kù)open的時(shí)候,直接刪除users表空間中的數(shù)據(jù)文件。
SQL> select name from v$datafile;
NAME
--------------------------------------------------------------------------------
/app/oracle/oradata/ORCL/datafile/o1_mf_system_555wqbnk_.dbf
/app/oracle/oradata/ORCL/datafile/o1_mf_undotbs1_555wqxgl_.dbf
/app/oracle/oradata/ORCL/datafile/o1_mf_sysaux_555wr5p6_.dbf
/app/oracle/oradata/ORCL/datafile/o1_mf_users_555wrj4o_.dbf
SQL> host rm /app/oracle/oradata/ORCL/datafile/o1_mf_users_555wrj4o_.dbf
2. 嘗試在users表空間中創(chuàng)建表,開(kāi)始報(bào)錯(cuò)。
SQL> create table t tablespace users as select * from dual;
create table t tablespace users as select * from dual
*
ERROR at line 1:
ORA-01116: error in opening database file 4
ORA-01110: data file 4:
'/app/oracle/oradata/ORCL/datafile/o1_mf_users_555wrj4o_.dbf'
ORA-27041: unable to open file
Linux Error: 2: No such file or directory
Additional information: 3
在告警日志中,同樣也可以看到類(lèi)似信息。
Mon Dec 19 21:48:17 CST 2011
Errors in file /app/oracle/admin/orcl/bdump/orcl_m000_3897.trc:
ORA-01116: error in opening database file 4
ORA-01110: data file 4: '/app/oracle/oradata/ORCL/datafile/o1_mf_users_555wrj4o_.dbf'
ORA-27041: unable to open file
Linux Error: 2: No such file or directory
Additional information: 3
3. 檢查dbwr的進(jìn)程PID
$ ps -ef|grep dbw0|grep -v grep
oracle 2879 1 0 21:38 ? 00:00:00 ora_dbw0_orcl
4. dbwr會(huì)打開(kāi)所有數(shù)據(jù)文件的句柄。在proc目錄中可以查到,目錄名是進(jìn)程PID,fd表示文件描述符。
$ cd /proc/2879/fd
$ ls -l
total 0
lr-x------ 1 oracle dba 64 Dec 19 21:50 0 -> /dev/null
lr-x------ 1 oracle dba 64 Dec 19 21:50 1 -> /dev/null
lr-x------ 1 oracle dba 64 Dec 19 21:50 10 -> /dev/zero
lr-x------ 1 oracle dba 64 Dec 19 21:50 11 -> /dev/zero
lr-x------ 1 oracle dba 64 Dec 19 21:50 12 -> /app/oracle/product/10.2.0/db_1/rdbms/mesg/oraus.msb
lrwx------ 1 oracle dba 64 Dec 19 21:50 13 -> /app/oracle/product/10.2.0/db_1/dbs/hc_orcl.dat
lrwx------ 1 oracle dba 64 Dec 19 21:50 14 -> /app/oracle/product/10.2.0/db_1/dbs/lkORCL
lrwx------ 1 oracle dba 64 Dec 19 21:50 15 -> /app/oracle/oradata/ORCL/controlfile/o1_mf_555wq3ng_.ctl
lrwx------ 1 oracle dba 64 Dec 19 21:50 16 -> /app/oracle/oradata/ORCL/datafile/o1_mf_system_555wqbnk_.dbf
lrwx------ 1 oracle dba 64 Dec 19 21:50 17 -> /app/oracle/oradata/ORCL/datafile/o1_mf_undotbs1_555wqxgl_.dbf
lrwx------ 1 oracle dba 64 Dec 19 21:50 18 -> /app/oracle/oradata/ORCL/datafile/o1_mf_sysaux_555wr5p6_.dbf
lrwx------ 1 oracle dba 64 Dec 19 21:50 19 -> /app/oracle/oradata/ORCL/datafile/o1_mf_users_555wrj4o_.dbf (deleted)
lr-x------ 1 oracle dba 64 Dec 19 21:50 2 -> /dev/null
lrwx------ 1 oracle dba 64 Dec 19 21:50 20 -> /app/oracle/oradata/ORCL/datafile/o1_mf_temp_555wrbnz_.tmp
lr-x------ 1 oracle dba 64 Dec 19 21:50 21 -> /app/oracle/product/10.2.0/db_1/rdbms/mesg/oraus.msb
lr-x------ 1 oracle dba 64 Dec 19 21:50 3 -> /dev/null
lr-x------ 1 oracle dba 64 Dec 19 21:50 4 -> /dev/null
l-wx------ 1 oracle dba 64 Dec 19 21:50 5 -> /app/oracle/admin/orcl/udump/orcl_ora_2871.trc
l-wx------ 1 oracle dba 64 Dec 19 21:50 6 -> /app/oracle/admin/orcl/bdump/alert_orcl.log
lrwx------ 1 oracle dba 64 Dec 19 21:50 7 -> /app/oracle/product/10.2.0/db_1/dbs/lkinstorcl (deleted)
l-wx------ 1 oracle dba 64 Dec 19 21:50 8 -> /app/oracle/admin/orcl/bdump/alert_orcl.log
lrwx------ 1 oracle dba 64 Dec 19 21:50 9 -> /app/oracle/product/10.2.0/db_1/dbs/hc_orcl.dat
注意其中"/app/oracle/oradata/ORCL/datafile/o1_mf_users_555wrj4o_.dbf (deleted)"字樣,表示該文件已經(jīng)被刪除,如果是Solaris操作系統(tǒng),ls命令不會(huì)有如此清晰的顯示,為了在Solaris系統(tǒng)中確認(rèn)哪個(gè)句柄對(duì)應(yīng)哪個(gè)文件,則需要使用lsof程序。
5. 直接cp該句柄文件名回原位置。
cp 19 /app/oracle/oradata/ORCL/datafile/o1_mf_users_555wrj4o_.dbf
6. 進(jìn)行數(shù)據(jù)文件recover
SQL> alter database datafile 4 offline;
Database altered.
SQL> recover datafile 4;
Media recovery complete.
SQL> alter database datafile 4 online;
Database altered.
完成數(shù)據(jù)文件恢復(fù)。
恢復(fù)的原理是,在Linux操作系統(tǒng)中,如果文件從操作系統(tǒng)級(jí)別被rm掉,之前打開(kāi)該文件的進(jìn)程仍然持有相應(yīng)的文件句柄,所指向的文件仍然可以讀寫(xiě),并且該文件的文件描述符可以從/proc目錄中獲得。但是要注意的是,此時(shí)如果關(guān)閉數(shù)據(jù)庫(kù),則此句柄會(huì)消失,那么除了掃描磁盤(pán)進(jìn)行文件恢復(fù)之外就沒(méi)有其它方法了,因此在數(shù)據(jù)庫(kù)出現(xiàn)問(wèn)題的時(shí)候,如果不確認(rèn)情況的復(fù)雜程度,千萬(wàn)不要隨便關(guān)閉數(shù)據(jù)庫(kù)。重啟數(shù)據(jù)庫(kù)往往是沒(méi)有意義的,甚至是致命的。
當(dāng)然,客戶(hù)的操作系統(tǒng)是Solaris,并且客戶(hù)刪除的文件還包括current online redo log,因此還有其它更復(fù)雜的操作,不在這里描述。
1. 在數(shù)據(jù)庫(kù)open的時(shí)候,直接刪除users表空間中的數(shù)據(jù)文件。
復(fù)制代碼
代碼如下:SQL> select name from v$datafile;
NAME
--------------------------------------------------------------------------------
/app/oracle/oradata/ORCL/datafile/o1_mf_system_555wqbnk_.dbf
/app/oracle/oradata/ORCL/datafile/o1_mf_undotbs1_555wqxgl_.dbf
/app/oracle/oradata/ORCL/datafile/o1_mf_sysaux_555wr5p6_.dbf
/app/oracle/oradata/ORCL/datafile/o1_mf_users_555wrj4o_.dbf
SQL> host rm /app/oracle/oradata/ORCL/datafile/o1_mf_users_555wrj4o_.dbf
2. 嘗試在users表空間中創(chuàng)建表,開(kāi)始報(bào)錯(cuò)。
復(fù)制代碼
代碼如下:SQL> create table t tablespace users as select * from dual;
create table t tablespace users as select * from dual
*
ERROR at line 1:
ORA-01116: error in opening database file 4
ORA-01110: data file 4:
'/app/oracle/oradata/ORCL/datafile/o1_mf_users_555wrj4o_.dbf'
ORA-27041: unable to open file
Linux Error: 2: No such file or directory
Additional information: 3
在告警日志中,同樣也可以看到類(lèi)似信息。
復(fù)制代碼
代碼如下:Mon Dec 19 21:48:17 CST 2011
Errors in file /app/oracle/admin/orcl/bdump/orcl_m000_3897.trc:
ORA-01116: error in opening database file 4
ORA-01110: data file 4: '/app/oracle/oradata/ORCL/datafile/o1_mf_users_555wrj4o_.dbf'
ORA-27041: unable to open file
Linux Error: 2: No such file or directory
Additional information: 3
3. 檢查dbwr的進(jìn)程PID
復(fù)制代碼
代碼如下:$ ps -ef|grep dbw0|grep -v grep
oracle 2879 1 0 21:38 ? 00:00:00 ora_dbw0_orcl
4. dbwr會(huì)打開(kāi)所有數(shù)據(jù)文件的句柄。在proc目錄中可以查到,目錄名是進(jìn)程PID,fd表示文件描述符。
復(fù)制代碼
代碼如下:$ cd /proc/2879/fd
$ ls -l
total 0
lr-x------ 1 oracle dba 64 Dec 19 21:50 0 -> /dev/null
lr-x------ 1 oracle dba 64 Dec 19 21:50 1 -> /dev/null
lr-x------ 1 oracle dba 64 Dec 19 21:50 10 -> /dev/zero
lr-x------ 1 oracle dba 64 Dec 19 21:50 11 -> /dev/zero
lr-x------ 1 oracle dba 64 Dec 19 21:50 12 -> /app/oracle/product/10.2.0/db_1/rdbms/mesg/oraus.msb
lrwx------ 1 oracle dba 64 Dec 19 21:50 13 -> /app/oracle/product/10.2.0/db_1/dbs/hc_orcl.dat
lrwx------ 1 oracle dba 64 Dec 19 21:50 14 -> /app/oracle/product/10.2.0/db_1/dbs/lkORCL
lrwx------ 1 oracle dba 64 Dec 19 21:50 15 -> /app/oracle/oradata/ORCL/controlfile/o1_mf_555wq3ng_.ctl
lrwx------ 1 oracle dba 64 Dec 19 21:50 16 -> /app/oracle/oradata/ORCL/datafile/o1_mf_system_555wqbnk_.dbf
lrwx------ 1 oracle dba 64 Dec 19 21:50 17 -> /app/oracle/oradata/ORCL/datafile/o1_mf_undotbs1_555wqxgl_.dbf
lrwx------ 1 oracle dba 64 Dec 19 21:50 18 -> /app/oracle/oradata/ORCL/datafile/o1_mf_sysaux_555wr5p6_.dbf
lrwx------ 1 oracle dba 64 Dec 19 21:50 19 -> /app/oracle/oradata/ORCL/datafile/o1_mf_users_555wrj4o_.dbf (deleted)
lr-x------ 1 oracle dba 64 Dec 19 21:50 2 -> /dev/null
lrwx------ 1 oracle dba 64 Dec 19 21:50 20 -> /app/oracle/oradata/ORCL/datafile/o1_mf_temp_555wrbnz_.tmp
lr-x------ 1 oracle dba 64 Dec 19 21:50 21 -> /app/oracle/product/10.2.0/db_1/rdbms/mesg/oraus.msb
lr-x------ 1 oracle dba 64 Dec 19 21:50 3 -> /dev/null
lr-x------ 1 oracle dba 64 Dec 19 21:50 4 -> /dev/null
l-wx------ 1 oracle dba 64 Dec 19 21:50 5 -> /app/oracle/admin/orcl/udump/orcl_ora_2871.trc
l-wx------ 1 oracle dba 64 Dec 19 21:50 6 -> /app/oracle/admin/orcl/bdump/alert_orcl.log
lrwx------ 1 oracle dba 64 Dec 19 21:50 7 -> /app/oracle/product/10.2.0/db_1/dbs/lkinstorcl (deleted)
l-wx------ 1 oracle dba 64 Dec 19 21:50 8 -> /app/oracle/admin/orcl/bdump/alert_orcl.log
lrwx------ 1 oracle dba 64 Dec 19 21:50 9 -> /app/oracle/product/10.2.0/db_1/dbs/hc_orcl.dat
注意其中"/app/oracle/oradata/ORCL/datafile/o1_mf_users_555wrj4o_.dbf (deleted)"字樣,表示該文件已經(jīng)被刪除,如果是Solaris操作系統(tǒng),ls命令不會(huì)有如此清晰的顯示,為了在Solaris系統(tǒng)中確認(rèn)哪個(gè)句柄對(duì)應(yīng)哪個(gè)文件,則需要使用lsof程序。
5. 直接cp該句柄文件名回原位置。
復(fù)制代碼
代碼如下:cp 19 /app/oracle/oradata/ORCL/datafile/o1_mf_users_555wrj4o_.dbf
6. 進(jìn)行數(shù)據(jù)文件recover
復(fù)制代碼
代碼如下:SQL> alter database datafile 4 offline;
Database altered.
SQL> recover datafile 4;
Media recovery complete.
SQL> alter database datafile 4 online;
Database altered.
完成數(shù)據(jù)文件恢復(fù)。
恢復(fù)的原理是,在Linux操作系統(tǒng)中,如果文件從操作系統(tǒng)級(jí)別被rm掉,之前打開(kāi)該文件的進(jìn)程仍然持有相應(yīng)的文件句柄,所指向的文件仍然可以讀寫(xiě),并且該文件的文件描述符可以從/proc目錄中獲得。但是要注意的是,此時(shí)如果關(guān)閉數(shù)據(jù)庫(kù),則此句柄會(huì)消失,那么除了掃描磁盤(pán)進(jìn)行文件恢復(fù)之外就沒(méi)有其它方法了,因此在數(shù)據(jù)庫(kù)出現(xiàn)問(wèn)題的時(shí)候,如果不確認(rèn)情況的復(fù)雜程度,千萬(wàn)不要隨便關(guān)閉數(shù)據(jù)庫(kù)。重啟數(shù)據(jù)庫(kù)往往是沒(méi)有意義的,甚至是致命的。
當(dāng)然,客戶(hù)的操作系統(tǒng)是Solaris,并且客戶(hù)刪除的文件還包括current online redo log,因此還有其它更復(fù)雜的操作,不在這里描述。
相關(guān)文章
Fedora Linux 42 穩(wěn)定版發(fā)布: 帶來(lái)大量新功能和軟件更新
Fedora 42昨日發(fā)布,這是 Red Hat 贊助開(kāi)發(fā)的杰出前沿 Linux 發(fā)行版的最新版,包含大量新功能和軟件更新,使其成為 2025 年上半年發(fā)布的一款出色的 Linux 操作系統(tǒng)之一,內(nèi)2025-04-16如何在Linux查看硬盤(pán)信息? 查看Linux硬盤(pán)大小類(lèi)型和硬件信息的5種方法
使用Linux系統(tǒng)的過(guò)程中,查看和了解硬盤(pán)信息是非常重要的工作,尤其是對(duì)于系統(tǒng)管理員而言,那么在Linux系統(tǒng)中如何查看硬盤(pán)信息?以下是具體內(nèi)容介紹2025-03-12如何在 Linux 中查看 CPU 詳細(xì)信息? 3招輕松查看CPU型號(hào)、核心數(shù)和溫度
在日常運(yùn)維工作中,獲取 CPU 信息是系統(tǒng)運(yùn)維管理員常見(jiàn)的工作內(nèi)容,無(wú)論是為了性能調(diào)優(yōu)、硬件升級(jí)還是僅僅滿(mǎn)足好奇心2025-03-11什么是 Arch Linux? 獨(dú)樹(shù)一幟的Arch Linux發(fā)行版分析
Arch Linux是為簡(jiǎn)化,優(yōu)化,現(xiàn)代化,實(shí)用主義,用戶(hù)中心和多功能性而創(chuàng)建Linux發(fā)行版,究竟是什么讓 Arch 與眾不同?下面我們就來(lái)簡(jiǎn)要解讀2025-02-19如何在Linux環(huán)境下制作 Win11裝機(jī)U盤(pán)?
一直用的linux辦公,想要將筆記本電腦從 Linux 系統(tǒng)切換回 Windows 11,我們可以制作一個(gè)win11裝機(jī)u盤(pán),詳細(xì)如下2025-02-17Rsnapshot怎么用? 基于Rsync的強(qiáng)大Linux備份工具使用指南
Rsnapshot 不僅可以備份本地文件,還能通過(guò) SSH 備份遠(yuǎn)程文件,接下來(lái)詳細(xì)介紹如何安裝、配置和使用 Rsnapshot,包括創(chuàng)建每小時(shí)、每天、每周和每月的本地備份,以及如何進(jìn)2025-02-06Linux Kernel 6.13發(fā)布:附更新內(nèi)容及新特性解讀
Linux 內(nèi)核 6.13 正式發(fā)布,新版本引入了惰性搶占支持,簡(jiǎn)化內(nèi)核搶占邏輯,通過(guò)減少與調(diào)度器相關(guān)的調(diào)用次數(shù),讓內(nèi)核在運(yùn)行時(shí)表現(xiàn)更優(yōu),從而提高效率2025-01-23五大特性引領(lǐng)創(chuàng)新! 深度操作系統(tǒng) deepin 25 Preview預(yù)覽版發(fā)布
今日,深度操作系統(tǒng)正式推出deepin 25 Preview版本,該版本集成了五大核心特性:磐石系統(tǒng)、全新DDE、Treeland窗口合成器、AI For OS以及Distrobox子系統(tǒng)2025-01-18Linux Mint Xia 22.1重磅發(fā)布: 重要更新一覽
Beta 版 Linux Mint“Xia” 22.1 發(fā)布,新版本基于 Ubuntu 24.04,內(nèi)核版本為 Linux 6.8,這次更新帶來(lái)了諸多優(yōu)化和改進(jìn),進(jìn)一步鞏固了 Mint 在 Linux 桌面操作系統(tǒng)領(lǐng)域的2025-01-16LinuxMint怎么安裝? Linux Mint22下載安裝圖文教程
Linux Mint22發(fā)布以后,有很多新功能,很多朋友想要下載并安裝,該怎么操作呢?下面我們就來(lái)看看詳細(xì)安裝指南2025-01-16