測(cè)試平臺(tái):rhel 4.6 and ids10.00uc3r1
相關(guān)命令:lsof用于顯示打開(kāi)的文件(list open file),此軟件在其他系統(tǒng)中可能需要另行安裝.
dd用于覆蓋及復(fù)制文件(convert and copy a file).
測(cè)試?yán)?
在正在運(yùn)行的數(shù)據(jù)庫(kù)服務(wù)器中(如下)
[informix@netsky datafile]$ onstat -d
IBM Informix Dynamic Server Version 10.00.UC3R1 -- On-Line -- Up 00:56:19 -- 28228 Kbytes
Dbspaces
address number flags fchunk nchunks pgsize flags owner name
4442e7e0 1 0x1 1 1 2048 N informix rootdbs
4442ec90 2 0x2001 2 1 2048 N T informix tempdbs
4442ede8 3 0x1 3 1 2048 N informix logdbs
44523630 4 0x1 4 1 2048 N informix phydbs
44523788 5 0x1 5 1 2048 N informix workdbs
445238e0 6 0x1 6 1 2048 N informix testdbs0
44523a38 7 0x1 7 1 2048 N informix testdbs1
44523b90 8 0x1 8 1 2048 N informix testdbs2
44523ce8 9 0x1 9 1 2048 N informix testdbs3
44523e40 10 0x1 10 1 2048 N informix testdbs4
10 active, 2047 maximum
Chunks
address chunk/dbs offset size free bpages flags pathname
4442e938 1 1 20 20000 12108 PO-- /opt/informix/datafile/rootdbs
445342f0 2 2 20 20000 19947 PO-- /opt/informix/datafile/tempdbs
44534478 3 3 20 20450 397 PO-- /opt/informix/datafile/logdbs
44534600 4 4 20 20450 19397 PO-- /opt/informix/datafile/phydbs
44534788 5 5 20 51150 39948 PO-- /opt/informix/datafile/workdbs
44534910 6 6 0 20000 19911 PO-- /opt/informix/datafile/testdbs0
44534a98 7 7 0 20000 19947 PO-- /opt/informix/datafile/testdbs1
44534c20 8 8 0 20000 19947 PO-- /opt/informix/datafile/testdbs2
44534da8 9 9 0 20000 19947 PO-- /opt/informix/datafile/testdbs3
4442eb08 10 10 0 20000 19947 PO-- /opt/informix/datafile/testdbs4
10 active, 2047 maximum
NOTE: The values in the "size" and "free" columns for DBspace chunks are
displayed in terms of "pgsize" of the DBspace to which they belong.
Expanded chunk capacity mode: disabled
往testdbs0中增加一些測(cè)試數(shù)據(jù)
informix@netsky ~]$ dbaccess dbmon -
Database selected.
>create table ttt (id char(10)) in testdbs0;
>insert into ttt values('000');
>insert into ttt values('111');
做onmode -c強(qiáng)制完全checkpoint,把數(shù)據(jù)刷回磁盤.
然后刪除數(shù)據(jù)塊testdbs0
[informix@netsky datafile]$ ll
drwxrwxr-x 2 informix informix 4096 Nov 27 06:06 bakup
-rw-rw---- 1 informix informix 41922560 Feb 10 00:06 logdbs
-rw-rw---- 1 informix informix 41922560 Feb 9 23:14 phydbs
-rw-rw---- 1 informix informix 41000960 Feb 10 00:06 rootdbs
-rw-rw---- 1 informix informix 41000960 Feb 9 23:17 tempdbs
-rw-rw---- 1 informix informix 40960000 Nov 27 06:01 testdbs1 --testdbs0被刪除.
-rw-rw---- 1 informix informix 104796160 Feb 9 23:13 workdbs
此時(shí)可以通過(guò)lsof查找數(shù)據(jù)庫(kù)打開(kāi)的文件testdbs0,可以看到該文件已經(jīng)被標(biāo)識(shí)刪除了.
[root@netsky datafile]# lsof | egrep '(COMMAND|deleted)'
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
oninit 23864 root 261u REG 253,0 40960000 620162 /opt/informix/datafile/testdbs0 (deleted)
oninit 23868 root 258u REG 253,0 40960000 620162 /opt/informix/datafile/testdbs0 (deleted)
此時(shí)再查看testdbs0上的數(shù)據(jù),并往testdbs0中增加一些數(shù)據(jù).
informix@netsky ~]$ dbaccess dbmon -
Database selected.
> select * from ttt;
id
000
111
2 row(s) retrieved.
> insert into ttt values('333');
1 row(s) inserted.
做onmode -c強(qiáng)制完全checkpoint,把數(shù)據(jù)刷回磁盤.
然后把已經(jīng)刪除的testdbs0恢復(fù)出來(lái)備份.
在這里,結(jié)合lsof的輸出信息PID進(jìn)入相應(yīng)的目錄,再通過(guò)FD號(hào)進(jìn)行dd恢復(fù).
[root@netsky fd]# cd /proc/23864/fd
[root@netsky fd]# ll
total 13
lr-x------ 1 root root 64 Feb 9 23:36 0 -> /opt/informix/msg/en_us/0333/olmsglog.iem
lrwx------ 1 root root 64 Feb 9 23:36 1 -> /dev/pts/2
lrwx------ 1 root root 64 Feb 9 23:36 2 -> /dev/pts/2
lrwx------ 1 root root 64 Feb 9 23:36 256 -> /opt/informix/datafile/rootdbs
lrwx------ 1 root root 64 Feb 9 23:36 257 -> /opt/informix/datafile/tempdbs
lrwx------ 1 root root 64 Feb 9 23:36 258 -> /opt/informix/datafile/logdbs
lrwx------ 1 root root 64 Feb 9 23:36 259 -> /opt/informix/datafile/phydbs
lrwx------ 1 root root 64 Feb 9 23:36 260 -> /opt/informix/datafile/workdbs
lrwx------ 1 root root 64 Feb 9 23:36 261 -> /opt/informix/datafile/testdbs0 (deleted)
lrwx------ 1 root root 64 Feb 9 23:36 262 -> /opt/informix/datafile/testdbs1
lrwx------ 1 root root 64 Feb 9 23:36 263 -> /opt/informix/datafile/testdbs2
lrwx------ 1 root root 64 Feb 9 23:36 264 -> /opt/informix/datafile/testdbs3
lrwx------ 1 root root 64 Feb 9 23:36 265 -> /opt/informix/datafile/testdbs4
這里在PID 23864的FD(File Descriptor)為261即為我們所要的.將其dd出來(lái)到/tmp/testdbs0
[root@netsky fd]# dd if=261 of=/tmp/testdbs0
80000+0 records in
80000+0 records out
完成以上操作后,停止數(shù)據(jù)庫(kù).
[informix@netsky datafile]$ onmode -ky
將恢復(fù)的數(shù)據(jù)文件拷回原目錄,并修改權(quán)限
[informix@netsky datafile]$ cp /tmp/testdbs0 /opt/informix/datafile/testdbs0
[informix@netsky datafile]$ chmod 660 testdbs0 ;chown informix:informix testdbs0
重新啟動(dòng)數(shù)據(jù)庫(kù)
[informix@netsky datafile]$ oninit -vy
驗(yàn)證數(shù)據(jù)文件恢復(fù).
[informix@netsky datafile]$ dbaccess dbmon -
Database selected.
> select * from ttt;
id
000
111
333
3 row(s) retrieved.
例子驗(yàn)證,在testdbs0被刪除后,文件被刪除后,數(shù)據(jù)上并未完全刪除的,只是其inode信息在系統(tǒng)上被刪除.但正在使用其的程序仍保留了這個(gè)inode信息,程序仍可以往里面寫入信息..在程序關(guān)閉前,可通過(guò)FD(File Descriptor)的方式進(jìn)行恢復(fù).
注意:要求的是文件必須仍被打開(kāi)..這樣的操作并不是十分保險(xiǎn)的,可能會(huì)造成數(shù)據(jù)損壞或者缺少.
-rw-rw---- 1 informix informix 40960000 Nov 27 06:01 testdbs2
-rw-rw---- 1 informix informix 40960000 Nov 27 06:01 testdbs3
-rw-rw---- 1 informix informix 40960000 Nov 27 06:01 testdbs4