Ubuntu如何简单粗暴的恢复被删除的文件

由于自己手残,删除了自己写了好久的一个项目main.c文件。当时我那个难受啊,但是后来自己百度,查看博客,终于找到了一个好的办法恢复因自己手残而删除的重要文件。

  • ubuntu恢复已删除的文件软件

extundelete

  • 安装方法

sudo apt-get install extundelete
  • 使用方法

我们可以使用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 ./RESTORED_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 '/' (it
                         must be one of the paths returned by --dump-names).
                         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.
  --output-dir 'path'    Restore files in the output dir 'path'.
                         By default the restored files are created under current directory 'RECOVERED_FILES'.
  --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.

我们知道当我们不小心删除了有用的文件,我们一般是比较容易知道删除的时间的,因此,使用时间这个option可以很快并且精确的恢复出我们想要的文件。那这个dtime怎么生成。请参考如下命令:

date       //查看一当前时间
date -d "2020-5-4 17:55:33" +%s  //生成dtime的索引

《Ubuntu如何简单粗暴的恢复被删除的文件》

  • 恢复命令

    首先需要生成dtime 的索引

    date       //查看一当前时间
    date -d "2020-5-4 17:55:33" +%s  //生成dtime的索引

    《Ubuntu如何简单粗暴的恢复被删除的文件》

    sudo extundelete /dev/sda1 --after 1588586319--restore-all

    注:/dev/sda1中sda的选择可能会有不同,自己可以一个一个试,我的sda1是可以的。

恢复完之后,系统会在你所在的当前目录下生成一个RECOVERED_FILES 的文件,那个文件里面会存放你所删除的文件及其他们所在的路径。

《Ubuntu如何简单粗暴的恢复被删除的文件》

 然后我们只需要将我们所需要的文件或文件夹拷贝出来就ok了。

 

  • extundelete原理

由于 在linux系统中,超级块描述了分区的信息,一个分区被分为两个部分,索引节点表和数据块区,这个在格式化的时候就定下来了。文件(目录也是文件的一种,只不过它的内容是描述目录下的文件的)由索引节点描述,索引节点描述了文件的修改时间,文件的名称,文件的数据块地址等等。并且,linux对于文件删除操作是个懒动作,删除文件时系统只是将文件对应的索引节点及其拥有的数据块置为free(将nlink=0),而并没有做其他清空的,只有当这个索引节点或者数据块被真正用到的时候才会修改里面的数据。这就为我们文件修复提供了可趁之机。由于系统中的索引节点是固定大小的,因此可以很轻松的遍历扫描系统中所有的索引节点,找出free的索引节点并检查其数据块是否已经被用,如果没有则可修复并修复。同时,由于索引节点里的时间等信息也是保留的,因此就可以根据时间来恢复特定的被删除的文件。

文件误删除后的注意事项
     从上面的分析可知,误删文件后,尽量不要做大的数据操作,以避免被删除的文件的数据块被重新使用,导致数据完全丢失。

    原文作者:XiaoCheng'Blog
    原文地址: https://blog.csdn.net/qq_44045338/article/details/105936439
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞