本文以 Oracle 資料庫為例,示範當資料檔案被 rm 誤刪、但資料庫尚未重啟時,如何透過 /proc 找回檔案;以及一旦重啟後就必須改用備份還原的對比。
[oracle@primary dbwdn]$ ll -rw-r----- 1 oracle dba 52436992 Nov 27 21:32 test01.dbf ... [oracle@primary dbwdn]$ rm test01.dbf
oracle 3493 1 0 21:10 ? 00:00:00 ora_dbw0_dbwdn
取得進程號 3493。
背景進程仍持有該檔案的檔案描述符,因此可從 /proc 中找到並標記為 (deleted) 的項目:
[oracle@primary dbwdn]$ ll /proc/3493/fd ... lrwx------ 1 oracle oinstall 64 Nov 27 21:52 22 -> /u01/app/oracle/oradata/dbwdn/test01.dbf (deleted) ...
前提是:誤刪之後資料庫沒有重啟,系統也沒有重啟。
[oracle@primary ~]$ cp /proc/3493/fd/22 /u01/app/oracle/oradata/dbwdn/test01.dbf
如此一來,被刪掉的資料檔案就找回來了。
[oracle@primary dbwdn]$ sqlplus "/as sysdba" SQL> alter database datafile 5 offline; SQL> recover datafile 5; SQL> alter database datafile 5 online; SQL> shutdown immediate; SQL> startup;
資料庫成功恢復。
若誤刪後又重啟了資料庫,舊進程消失,/proc 中的描述符也不復存在,此時啟動會直接報錯:
ORA-01157: cannot identify/lock data file 4 - see DBWR trace file ORA-01110: data file 4: '/u01/app/oracle/oradata/dbwdn/users01.dbf'
這代表 /proc 的偷天換日已失效,只能改用備份集還原。
[oracle@primary dbwdn]$ rman target /
RMAN> startup nomount;
RMAN> alter database mount;
RMAN> run {
allocate channel t1 device type disk;
restore datafile 4;
recover datafile 4;
release channel t1;
}
RMAN> alter database open;
RMAN 會從備份片段還原並做媒體恢復,最後 open 資料庫即可。
rm 誤刪檔案後千萬不要慌張:不要重啟資料庫、不要重啟系統,才有機會用 /proc 把檔案救回來。一旦重啟,就只能依賴備份還原了。(本文以 Linux 為例,AIX 等其他平台未經測試。)
此外,若是在 Windows 上清空回收站後想找回檔案,可透過修改登錄檔、在 Namespace 下新增主鍵並指向「回收站」,再重啟電腦;但前提是機器沒有做過磁碟重組、系統完好,才有機會還原。