Warning: Illegal string offset 'top' in /www/wwwroot/hellyhua.com/wp-content/themes/Snape/single.php on line 45

先介绍一些文件的基本概念, 文件实际上是一个指向inode的链接, inode链接包含了文件的所有属性, 比如权限和所有者, 数据块地址(文件存储在磁盘的这些数据块中). 当你删除(rm)一个文件, 实际删除了指向inode的链接, 并没有删除inode的内容. 进程可能还在使用. 只有当inode的所有链接完全移去, 然后这些数据块将可以写入新的数据.

proc文件系统可以协助我们恢复数据. 每一个系统上的进程在/proc都有一个目录和自己的名字: 里面包含了一个fd(文件描述符)子目录(进程需要打开文件的所有链接). 如果从文件系统中删除一个文件, 此处还有一个inode的引用:

/proc/进程号/fd/文件描述符

接下来, 你需要知道打开文件的进程号(pid)和文件描述符(fd). 这些都可以通过lsof工具方便获得, lsof的意思是”list open files, 列出(进程)打开的文件”. 然后你将可以从/proc拷贝出需要恢复的数据.

下面介绍在Centos 5 系统上使用lsof恢复误删的文件:

环境
主机: DELL POWEREAGE 2950
系统: CentOS release 5 (Final)
内核: 2.6.18-8.el5
lsof 版本: 4.78

恢复过程 :
首先, 我们需要创建一个文本文件, 删除然后恢复:

[root@localhost ~]# man rsync | col -b > rsync.txt
[root@localhost ~]# more rsync.txt
rsync(1)                                                              rsync(1)

NAME
       rsync – faster, flexible replacement for rcp

SYNOPSIS
       rsync [OPTION]… SRC [SRC]… DEST

CRTL + Z 停止查看

stat rsync.txt
File: `rsync.txt’
Size: 139381          Blocks: 288        IO Block: 4096   regular file
Device: 802h/2050d      Inode: 98407       Links: 1
Access: (0644/-rw-r–r–) Uid: (    0/    root)   Gid: (    0/    root)
Access: 2008-06-04 12:21:26.000000000 +0800
Modify: 2008-06-04 12:21:09.000000000 +0800
Change: 2008-06-04 12:21:09.000000000 +0800

[root@localhost ~]# lsof | grep rsync.txt
more      28119    root    3r      REG        8,2   139381      98407 /usr/home/qipeng1/rsync.txt

程序名more 进程号28819 运行者root 文件描述符3 普通文件

[root@localhost ~]# cp /proc/28119/fd/3 file
[root@localhost ~]# man rsync | col -b > file1
[root@localhost ~]# cmp file file1
[root@localhost ~]#

完全一样 说明被删除的文件 只要文件描述符没有关掉 应该是可以恢复的


Warning: Illegal string offset 'footer' in /www/wwwroot/hellyhua.com/wp-content/themes/Snape/single.php on line 49