原文链接:http://www.21yunwei.com/archi...
原文作者:21运维

Linux不像windows有那么显眼的回收站,不是简略的还原就能够了。linux删除文件还原能够分为两种状况,一种是删除当前在过程存在删除信息,一种是删除当前过程都找不到,只有借助于工具还原。这里别离查看介绍下

一,误删除文件过程还在的状况。

这种个别是有流动的过程存在继续规范输出或输入,到时文件被删除后,过程PID还是存在。这也就是有些服务器删除一些文件然而磁盘不开释的起因。比方以后举例说明:通过一个shell终端对一个测试文件做cat追加操作:

[root@21yunwei_backup ~]# echo  "hello  py" > testdelete.py[root@21yunwei_backup ~]# cat  >> testdelete.py hello delete

另外一个终端查看这个文件能够分明看到内容:

[root@21yunwei_backup ~]# cat testdelete.py hello  pyhello delete

此时,在以后服务器删除文件rm -f ./testdelete.py 命令查看这个目录,文件曾经不存在了,那么当初咱们将其复原进去。

1、lsof查看删除的文件过程是否还存在。

这里用到一个命令lsof,如没有装置请自行yum或者apt-get。相似这种状况,咱们能够先lsof查看删除的文件 是否还在:

[root@21yunwei_backup ~]# lsof | grep deletedmysqld     1512   mysql    5u      REG              252,3          0    6312397 /tmp/ibzW3Lot (deleted)cat       20464    root    1w      REG              252,3         23    1310722 /root/testdelete.py (deleted)

侥幸的是这种状况过程还存在 ,那么开始进行复原 操作。

2,复原。

复原命令:

cp /proc/pid/fd/1  /指定目录/文件名

进入 过程目录,个别是进入/proc/pid/fd/,针对当前情况:

[root@21yunwei_backup ~]# cd   /proc/20464/fd[root@21yunwei_backup fd]# lltotal 0lrwx------ 1 root root 64 Nov 15 18:12 0 > /dev/pts/1l-wx------ 1 root root 64 Nov 15 18:12 1 > /root/testdelete.py (deleted)lrwx------ 1 root root 64 Nov 15 18:12 2 > /dev/pts/1

复原操作:

cp 1 /tmp/testdelete.py

查看文件:

[root@21yunwei_backup fd]# cat  /tmp/testdelete.pyhello  pyhello delete

复原实现。

二,误删除的文件过程曾经不存在,借助于工具还原。

创立筹备删除的目录并echo一个 带有内容的文件:

[root@21yunwei_backup 21yunwei]# tree  .  ├── deletetest  │   └── mail  │       └── test.py  ├── lost+found  └── passwd     3 directories, 2 files  [root@21yunwei_backup 21yunwei]# cat /21yunwei/deletetest/mail/test.py   hello Dj  [root@21yunwei_backup 21yunwei]# tail  -2  passwd   haproxy:x:500:502::/home/haproxy:/bin/bash  tcpdump:x:72:72::/:/sbin/nologin

执行删除操作:

[root@21yunwei_backup 21yunwei]# rm  -rf    ./*[root@21yunwei_backup 21yunwei]# lltotal 0

当初开始进行误删除文件的复原。这种状况个别是没有守护进行或者后盾过程对其继续输出,所以删除就删除 了,lsof也看不到。就要借助于工具。这里咱们采纳的工具是extundelete第三方工具。复原步骤如下:

  • 1,进行对以后分区做任何操作,避免inode被笼罩。inode被笼罩根本就辞别自行车了。比方进行所在分区的服务,卸载目录所在的设施,有必要的状况下都能够断网。
  • 2,通过dd命令对 以后分区进行备份,避免第三方软件复原失败导致数据失落。适宜数据十分重要的状况,这里测试,就没有备份,如备份能够思考如下形式:dd if=/path/filename of=/dev/vdc1
  • 3,通过umount命令,对以后设施分区卸载。或者fuser 命令。umount /dev/vdb1 或者 umount /21yunwei 如果提醒设施busy,能够用fuser命令强制卸载:fuser -m -v -i -k /21yunwei
  • 4,下载第三方工具extundelete装置,搜寻误删除的文件进行还原。
wget  http://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2tar jxvf extundelete-0.2.4.tar.bz2cd  extundelete-0.2.4./configure makemake  install

扫描误删除的文件:

[root@21yunwei_backup extundelete-0.2.4]# extundelete  --inode 2 /dev/vdb1  NOTICE: Extended attributes are not restored.  Loading filesystem metadata ... 8 groups loaded.  Group: 0  Contents of inode 2:     .  .省略N行     File name                | Inode number | Deleted status  .                            2  ..                           2  lost+found                   11             Deleted  deletetest                   12             Deleted  passwd                       14             Deleted

通过扫描发现了咱们删除的文件夹,当初执行复原操作。(1)复原繁多文件passwd

[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-file passwd  NOTICE: Extended attributes are not restored.Loading filesystem metadata ... 8 groups loaded.Loading journal descriptors ... 46 descriptors loaded.Successfully restored file passwd

复原文件是放到了当前目录RECOVERED_FILES。查看复原的文件:

[root@21yunwei_backup /]# tail  -5  RECOVERED_FILES/passwd mysql:x:497:500::/home/mysql:/bin/falsenginx:x:496:501::/home/nginx:/sbin/nologinzabbix:x:495:497:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologinhaproxy:x:500:502::/home/haproxy:/bin/bashtcpdump:x:72:72::/:/sbin/nologin

(2)复原目录deletetest

[root@21yunwei\_backup /]# extundelete /dev/vdb1 --restore-directory  deletetest   NOTICE: Extended attributes are not restored.  Loading filesystem metadata ... 8 groups loaded.  Loading journal descriptors ... 46 descriptors loaded.  Searching for recoverable inodes in directory deletetest ...   5 recoverable inodes found.  Looking through the directory structure for deleted files ...   [root@21yunwei_backup /]# cat  RECOVERED_FILES/deletetest/mail/test.py   hello Dj

(3)复原所有

[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-all  NOTICE: Extended attributes are not restored.  Loading filesystem metadata ... 8 groups loaded.  Loading journal descriptors ... 46 descriptors loaded.  Searching for recoverable inodes in directory / ...   5 recoverable inodes found.  Looking through the directory structure for deleted files ...   0 recoverable inodes still lost.   [root@21yunwei_backup /]# cd RECOVERED_FILES/  [root@21yunwei_backup RECOVERED\_FILES]# tree  .  ├── deletetest  │   └── mail  │       └── test.py  └── passwd     2 directories, 2 files

(4),复原指定inode。

[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-inode 14  NOTICE: Extended attributes are not restored.  Loading filesystem metadata ... 8 groups loaded.  Loading journal descriptors ... 46 descriptors loaded.  [root@21yunwei_backup /]# tail  -5   /RECOVERED_FILES/file.14   mysql:x:497:500::/home/mysql:/bin/false  nginx:x:496:501::/home/nginx:/sbin/nologin  zabbix:x:495:497:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin  haproxy:x:500:502::/home/haproxy:/bin/bash  tcpdump:x:72:72::/:/sbin/nologin

留神复原inode的时候,复原 进去的文件名和之前不一样,须要独自进行改名。内容是没问题的。

更多的extundelete用法请参考extundelete –help选项参数阐明,以后复原所有的操作实现。