暂无图片
暂无图片
2
暂无图片
暂无图片
暂无图片

linux上恢复误删的方法

watson 2024-09-19
165

在Linux系统中,当你使用rm命令删除一个文件时,该文件的目录项(文件名和inode之间的链接)会被移除,但文件的数据块(inode及其指向的数据)在文件系统中仍然保留,直到该空间被新的数据覆盖。因此,理论上讲,如果文件刚刚被删除并且没有其他文件写入覆盖该空间,你有可能通过inode来恢复文件。


不过,需要注意的是,这种方法并不是直接通过inode命令来恢复文件,因为标准的Linux工具和命令(如ls、find等)并不直接支持通过inode号来访问文件(除非它们仍然通过某个目录项间接访问)。但是,你可以使用一些工具和技术来尝试恢复被删除的文件

一、误删除文件进程还在的情况。

这种一般是有活动的进程存在持续标准输入或输出,到时文件被删除后,进程PID还是存在。这也就是有些服务器删除一些文件但是磁盘不释放的原因。比如当前举例说明:通过一个shell终端对一个测试文件做cat追加操作:


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

另外一个终端查看这个文件可以清楚看到内容:

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

此时,在当前服务器删除文件rm -f ./testdelete.py

令查看这个目录,文件已经不存在了,那么现在我们将其恢复出来。

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

这里用到一个命令lsof,如没有安装请自行yum或者apt-get。类似这种情况,我们可以先lsof查看删除的文件 是否还在:

[root@21yunwei_backup ~]# lsof | grep deleted
mysqld 1512 mysql 5u REG 252,306312397/tmp/ibzW3Lot (deleted)
cat 20464 root 1w REG 252,3231310722/root/testdelete.py (deleted)

幸运的是这种情况进程还存在 ,那么开始进行恢复 操作。

2、恢复。

恢复命令:

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

进入 进程目录,一般是进入/proc/pid/fd/,针对当前情况:

[root@21yunwei_backup ~]# cd   /proc/20464/fd
[root@21yunwei_backup fd]# ll
total 0
lrwx------ 1 root root 64Nov1518:120> /dev/pts/1
l-wx------ 1 root root 64Nov1518:121> /root/testdelete.py (deleted)
lrwx------ 1 root root 64Nov1518:122> /dev/pts/1

恢复操作:

cp 1/tmp/testdelete.py

查看文件:

[root@21yunwei_backup fd]# cat  /tmp/testdelete.py
hello

恢复完成。


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

现在开始进行误删除文件的恢复。这种情况一般是没有守护进程或者后台进程对其持续输入,所以删除就删除了,lsof也看不到。就要借助于工具。这里我们采用的工具是extundelete第三方工具。恢复步骤如下:

1.停止对当前分区做任何操作,防止inode被覆盖。inode被覆盖基本就告别恢复了。比如停止所在分区的服务,卸载目录所在的设备,有必要的情况下都可以断网。2.通过dd命令对当前分区进行备份,防止第三方软件恢复失败导致数据丢失。适合数据非常重要的情况,这里测试,就没有备份,如备份可以考虑如下方式:

dd if=/path/filename of=/dev/vdc1

1.通过umount命令,对当前设备分区卸载。或者fuser 命令。

umount /dev/vdb1 或者 umount /21yunwei

如果提示设备busy,可以用fuser命令强制卸载:

fuser -m -v -i -k /21yunwei

extundelete工具安装

  • extundelete下载地址:http://extundelete.sourceforge.net/

1

wget https://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2

解压该文件tar jxvf extundelete-0.2.4.tar.bz2

若报这种错误

1

2

3

4

5

[root@docking ~]# tar jxvf extundelete-0.2.4.tar.bz2

tar (child): bzip2:无法 exec: 没有那个文件或目录

tar (child): Error is not recoverable: exiting now

tar: Child returned status 2

tar: Error is not recoverable: exiting now

则使用yum -y install bzip2进行解决

1

2

3

4

5

6

7

8

9

10

[root@docking ~]# tar jxvf extundelete-0.2.4.tar.bz2

extundelete-0.2.4/

extundelete-0.2.4/acinclude.m4

extundelete-0.2.4/missing

extundelete-0.2.4/autogen.sh

extundelete-0.2.4/aclocal.m4

extundelete-0.2.4/configure

extundelete-0.2.4/LICENSE

extundelete-0.2.4/README

...................................................

1

2

cd  extundelete-0.2.4

./configure

若这步骤报错

1

2

3

4

5

[root@docking extundelete-0.2.4]# ./configure

Configuring extundelete 0.2.4

configure: error: in `/root/extundelete-0.2.4':

configure: error: C++ compiler cannot create executables

See `config.log' for more details

则使用yum -y install gcc-c++解决.

若执行上一步仍然报错,

1

2

3

[root@docking extundelete-0.2.4]# ./configure

Configuring extundelete 0.2.4

configure: error: Can't find ext2fs library

则使用yum -y install e2fsprogs e2fsprogs-devel来解决。
#Ubuntu的解决办法为sudo apt-get install e2fslibs-dev e2fslibs-dev

不出意外的话到这里应该configure能够顺利完成.

1

2

3

4

[root@docking extundelete-0.2.4]# ./configure

Configuring extundelete 0.2.4

Writing generated files to disk

[root@docking extundelete-0.2.4]#

最后make然后 make install

1

2

3

4

5

6

7

8

9

10

[root@docking extundelete-0.2.4]# make

make -s all-recursive

Making all in src

extundelete.cc: 在函数‘ext2_ino_t find_inode(ext2_filsys, ext2_filsys, ext2_inode*, std::string, int)'中:

extundelete.cc:1272:29: 警告:在 {} 内将‘search_flags'从‘int'转换为较窄的类型‘ext2_ino_t {aka unsigned int}' [-Wnarrowing]

    buf, match_name2, priv, 0};

                             ^

[root@docking extundelete-0.2.4]# make install

Making install in src

  /usr/bin/install -c extundelete '/usr/local/bin'

extundelete安装完成.

扫描误删除的文件:

使用df -lh查看挂载:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

taroballs@taroballs-PC:~$ d-lh

文件系统        容量  已用  可用 已用% 挂载点

udev            1.9G     0  1.9G    0% /dev

tmpfs           387M  1.8M  385M    1% /run

/dev/sda2        92G   61G   26G   71% /

tmpfs           1.9G   49M  1.9G    3% /dev/shm

tmpfs           5.0M  4.0K  5.0M    1% /run/lock

tmpfs           1.9G     0  1.9G    0% /sys/fs/cgroup

/dev/sda3       104G   56G   44G   57% /home

tmpfs           387M   40K  387M    1% /run/user/1000

/dev/sda4        70G   20G   47G   30% /media/taroballs/d8423f8c-d687-4c03-a7c8-06a7fb57f96d

/dev/sdb1       6.8G  4.1G  2.8G   60% /media/taroballs/taroballs

/dev/sr0        4.0G  4.0G     0  100% /media/taroballs/2018-01-16-12-36-00-00

taroballs@taroballs-PC:~$ cd /media/taroballs/taroballs/

taroballs@taroballs-PC:/media/taroballs/taroballs$

可以看到,我们的目录/media/taroballs/taroballs
挂载到/dev/sdb1 这个文件系统中.

umount我们的挂载盘比如:

1

2

taroballs@taroballs-PC:~$ df -lh | grep /dev/sdb1

/dev/sdb1       6.8G  4.1G  2.8G   60% /media/taroballs/taroballs

umount这个目录

1

2

3

4

taroballs@taroballs-PC:~$ umount /media/taroballs/taroballs

taroballs@taroballs-PC:~$ df -lh | grep /dev/sdb1

taroballs@taroballs-PC:~$

#记得删除一定要后umount哦,不然二次写入谁也帮不了你呢。

通过inode节点恢复

1

2

3

taroballs@taroballs-PC:~$ mkdir recovertest

taroballs@taroballs-PC:~$ cd recovertest/

taroballs@taroballs-PC:~/recovertest$

执行恢复extundelete /dev/sdb1 --inode 2

1

2

3

4

5

6

7

8

9

10

11

12

13

14

taroballs@taroballs-PC:/media/taroballs/taroballs$ sudo extundelete /dev/sdb1 --inode 2

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

deletetest                                        12             Deleted

tmppasswd                                            14             Deleted

通过扫描发现了我们删除的文件夹,现在执行恢复操作。
(1)恢复单一文件tmppasswd

1

2

3

4

5

taroballs@taroballs-PC:~/recovertest$  extundelete /dev/sdb1 --restore-file passwd  

NOTICE: Extended attributes are not restored.

Loading filesystem metadata ... 8 groups loaded.

Loading journal descriptors ... 46 descriptors loaded.

Successfully restored file tmppasswd

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

1

2

taroballs@taroballs-PC:~/recovertest$ cat tmppasswd

tcpdump:x:172:72::/:/sbin/nologin

(2)恢复目录deletetest

1

2

3

4

5

6

7

extundelete /dev/sdb1 --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 ...

(3)恢复所有

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

taroballs@taroballs-PC:~/recovertest$ extundelete /dev/sdb1 --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.

taroballs@taroballs-PC:~/recovertest$ tree

backuptest/

├── deletetest

│   └── innerfolder

│       └── deletefile.txt

└── tmppasswd

 

2 directories, 2 files

(4)恢复指定inode

1

2

3

4

5

6

7

taroballs@taroballs-PC:~/recovertest$ extundelete /dev/sdb1 --restore-inode 14

NOTICE: Extended attributes are not restored.

Loading filesystem metadata ... 8 groups loaded.

Loading journal descriptors ... 46 descriptors loaded.

taroballs@taroballs-PC:~/recovertest$ cat file.14

tcpdump:x:172:72::/:/sbin/nologin

#注意恢复inode的时候,恢复 出来的文件名和之前不一样,需要单独进行改名。

最后附上extundelete的用法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

$ extundelete --help

Usage: extundelete [options] [--] device-file

Options:

  --version, -[vV]       Print version and exit successfully.

  --help,                Print this help and exit successfully.

  --superblock           Print contents of superblock in addition to the rest.

                         If no action is specified then this option is implied.

  --journal              Show content of journal.

  --after dtime          Only process entries deleted on or after 'dtime'.

  --before dtime         Only process entries deleted before 'dtime'.

Actions:

  --inode ino            Show info on inode 'ino'.

  --block blk            Show info on block 'blk'.

  --restore-inode ino[,ino,...]

                         Restore the file(s) with known inode number 'ino'.

                         The restored files are created in ./RECOVERED_FILES

                         with their inode number as extension (ie, file.12345).

  --restore-file 'path'  Will restore file 'path'. 'path' is relative to root

                         of the partition and does not start with a '/'

                         The restored file is created in the current

                         directory as 'RECOVERED_FILES/path'.

  --restore-files 'path' Will restore files which are listed in the file 'path'.

                         Each filename should be in the same format as an option

                         to --restore-file, and there should be one per line.

  --restore-directory 'path'

                         Will restore directory 'path'. 'path' is relative to the

                         root directory of the file system.  The restored

                         directory is created in the output directory as 'path'.

  --restore-all          Attempts to restore everything.

  -j journal             Reads an external journal from the named file.

  -b blocknumber         Uses the backup superblock at blocknumber when opening

                         the file system.

  -B blocksize           Uses blocksize as the block size when opening the file

                         system.  The number should be the number of bytes.

  --log 0                Make the program silent.

  --log filename         Logs all messages to filename.

--log D1=0,D2=filename   Custom control of log messages with comma-separated

   Examples below:       list of options.  Dn must be one of info, warn, or

   --log info,error      error.  Omission of the '=name' results in messages

   --log warn=0          with the specified level to be logged to the console.

   --log error=filename  If the parameter is '=0', logging for the specified

                         level will be turned off.  If the parameter is

                         '=filename', messages with that level will be written

                         to filename.

   -o directory          Save the recovered files to the named directory.

                         The restored files are created in a directory

                         named 'RECOVERED_FILES/' by default.

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论