zoukankan      html  css  js  c++  java
  • Centos6/RHEL6下恢复ext4文件系统下误删除的文件

    一.关于ext4文件系统

    ​ EXT4是第四代扩展文件系统(英语:Fourth extended filesystem,缩写为 ext4)是Linux系统下的日志文件系统,是ext3文件系统的

    后继版本。Ext4是由Ext3的维护者Theodore Tso领导的开发团队实现的。相比于ext3文件系统,ext4文件系统支持更大的文件系统和更

    大的文件,更多的子目录数量,更多的块和i-节点数量等等。

    ​ 对于ext4文件系统上误删除文件,可以使用extundelete恢复,对于ext3文件系统,则使用ext3grep恢复误删除的文件。在windows上

    恢复误删除的文件可以使用final data v2.0 汉化版和easyrecovery。

    二.linux文件系统的组成(inode,block)

    ​ Linux文件系统由三部分组成:文件名,inode,block。inode存放文件元数据信息,block是真正存放数据的地方。windows也由这三部分组成。

    ​ 每个文件都有一个inode号,可以使用ls -i和stat查看。

    [root@node5 ~]# ls -i test.txt 
    34566868 test.txt
    [root@node5 ~]# stat test.txt 
      File: ‘test.txt’
      Size: 12        	Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d	Inode: 34566868    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2020-10-10 17:30:12.532654117 +0800
    Modify: 2020-10-10 17:30:12.532654117 +0800
    Change: 2020-10-10 17:30:12.533654118 +0800
     Birth: -
    

    三.问题:为什么删除比复制快?

    ​ 因为这只是逻辑删除。删除的时候并不是将整个数据都真正删除了,它只是将文件列表设置为可以写入状态,在有新数据要写入时,直

    接覆盖原来的数据。可以使用如下图表示:

    image-20201012181134673

    image-20201012181152486

    四.问题:当我们误删除文件后,第一件事要做什么?

    ​ 由于在linux中,删除只是逻辑删除,此时我们要避免误删除的文件内容被新写入的文件覆盖。可以卸载需要恢复文件的分区或者以只

    读的方式挂载。

    ​ 如果在根下删除文件,想恢复数据,应该怎么办?

    ​ 方法1: 立即断电,然后把磁盘以只读方式,挂载到另一个电脑中进行恢复。

    ​ 方法2:把extundelete在虚拟机上(虚拟机系统要和服务器版本一样),提前安装好后再复制到U盘中,把U盘插入服务器,恢复

    时,恢复的文件要保存到U盘中,(不要让恢复的数据写到/下,那样会覆盖之前删除的文件)。

    五.准备测试环境

    1.首先去https://sourceforge.net/projects/extundelete/这个网站下载extundelete-0.2.4.tar.bz2软件。值得一提的是http://sourceforge.net/ 是开源软件发布中心,好多软件可以去这里下载。

    2.可以添加一块硬盘,或者在现有硬盘的基础上进行分区。磁盘分区大致步骤是fdisk进行分区,创建挂载点,格式化成ext4文件系统,最

    后挂载。具体磁盘分区可以查看磁盘分区工具章节https://www.cnblogs.com/renshengdezheli/p/13941563.html。

    3.准备测试文件,删除测试文件,卸载分区

    #因为/dev/sdb5是ext4文件系统,所以在/sdb5目录下创建测试文件
    [root@node5 ~]# df -hT
    Filesystem              Type      Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root xfs        17G   11G  6.2G  64% /
    devtmpfs                devtmpfs  3.9G     0  3.9G   0% /dev
    tmpfs                   tmpfs     3.9G  8.0K  3.9G   1% /dev/shm
    tmpfs                   tmpfs     3.9G  8.8M  3.9G   1% /run
    tmpfs                   tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
    /dev/sda1               xfs      1014M  125M  890M  13% /boot
    /dev/sdb1               xfs        47M  2.7M   45M   6% /sdb1
    /dev/sdb5               ext4      190M  1.6M  175M   1% /sdb5
    /dev/sdb6               xfs       295M   16M  280M   6% /sdb6
    tmpfs                   tmpfs     785M     0  785M   0% /run/user/0
    [root@node5 ~]# cd /sdb5
    [root@node5 sdb5]# pwd
    /sdb5
    [root@node5 sdb5]# ls
    lost+found
    
    #复制一些文件当做测试文件
    [root@node5 sdb5]# cp /etc/passwd ./
    [root@node5 sdb5]# cp /etc/hosts ./
    [root@node5 sdb5]# cp -r /boot/grub2 ./
    [root@node5 sdb5]# mkdir -p a/b/c/d
    [root@node5 sdb5]# touch a/b/test.txt
    [root@node5 sdb5]# tree ./ -L 2
    ./
    ├── a
    │   └── b
    ├── grub2
    │   ├── device.map
    │   ├── fonts
    │   ├── grub.cfg
    │   ├── grubenv
    │   ├── i386-pc
    │   └── locale
    ├── hosts
    ├── lost+found
    └── passwd
    
    7 directories, 5 files
    [root@node5 sdb5]# ls
    a  grub2  hosts  lost+found  passwd
    
    #删除测试文件
    [root@node5 sdb5]# rm -rf a grub2 hosts passwd
    [root@node5 sdb5]# ls
    lost+found
    
    #卸载分区,避免写入新数据把原先数据覆盖
    [root@node5 sdb5]# cd
    [root@node5 ~]# umount /sdb5
    

    六.安装extundelet

    1.由于extundelete-0.2.4.tar.bz2是源码包,所以安装extundelete使用源码安装的方式。

    2.源码编译安装extundelete

    [root@node5 ~]# ls extundelete*
    extundelete-0.2.4.tar.bz2
    
    #解压缩安装包
    [root@node5 ~]# tar jxvf extundelete-0.2.4.tar.bz2 
    [root@node5 ~]# ls
    201810240430234009.pcm  apache-tomcat-8.0.51.tar.gz  extundelete-0.2.4                                       idea快捷键.txt              linux-4.19.77.tar   qemu-4.1.0                     test2.txt
    a                       busybox-1.24.2               extundelete-0.2.4.tar.bz2                               iu.jpg                      linux脚本.sh        qemu-4.1.0.tar                 test.txt
    aaa.txt                 busybox-1.24.2.tar.bz2       glibc-aarch64-linux-gnu-2.24-2.sdl7.2.noarch.rpm        jdk-8u172-linux-x64.tar.gz  nginx-1.8.0.tar.gz  rootfs                         测试乱码文件.txt
    anaconda-ks.cfg         ceshi.txt                    glibc-aarch64-linux-gnu-devel-2.24-2.sdl7.2.noarch.rpm  linux-4.19.77               qemu-2.11.0.tar.xz  telnet-0.17-64.el7.x86_64.rpm
    
    [root@node5 ~]# cd extundelete-0.2.4
    [root@node5 extundelete-0.2.4]# pwd
    /root/extundelete-0.2.4
    [root@node5 extundelete-0.2.4]# ls
    acinclude.m4  aclocal.m4  autogen.sh  config.h.in  configure  configure.ac  depcomp  install-sh  LICENSE  Makefile.am  Makefile.in  missing  README  src
    
    #安装依赖包
    [root@node5 extundelete-0.2.4]# yum -y install e2fsprogs-devel
     
    #检查系统安装环境
    [root@node5 extundelete-0.2.4]# ./configure 
    Configuring extundelete 0.2.4
    Writing generated files to disk
    
    #$?返回上一个命令的返回值,返回0就是正确
    [root@node5 extundelete-0.2.4]# echo $?
    0
    
    #编译,把源代码编译成可执行的二进制文件。-j 4表示使用4个进程同时进行编译,提升编译速度或使用4核CPU同时编译
    [root@node5 extundelete-0.2.4]# make -j 4
    make -s all-recursive
    Making all in src
    extundelete.cc: In function ‘ext2_ino_t find_inode(ext2_filsys, ext2_filsys, ext2_inode*, std::string, int)’:
    extundelete.cc:1272:29: warning: narrowing conversion of ‘search_flags’ from ‘int’ to ‘ext2_ino_t {aka unsigned int}’ inside { } [-Wnarrowing]
        buf, match_name2, priv, 0};
                                 ^
    [root@node5 extundelete-0.2.4]# echo $?
    0
    
    #编译安装
    [root@node5 extundelete-0.2.4]# make install
    Making install in src
      /usr/bin/install -c extundelete '/usr/local/bin'
      
    [root@node5 extundelete-0.2.4]# echo $?
    0
    
    #可以看到已经存在extundelete这个可执行文件
    [root@node5 extundelete-0.2.4]# which extundelete
    /usr/local/bin/extundelete
    
    #扩展install和cp有什么区别? 
    #install复制时可以指定权限,但是cp不可以
    #例如
    [root@node5 ~]# install -m 777 /bin/find /opt/a.sh
    [root@node5 ~]# ll /opt/
    

    七.恢复误删除文件

    1.恢复误删除文件有四种方法:

    • ​ 通过inode号恢复数据
    • ​ 通过文件名恢复数据
    • ​ 恢复指定目录
    • ​ 恢复所有的文件

    2.创建一个用于存放恢复数据的文件夹,通过inode结点查看被删除的文件名字。

    [root@node5 ~]# pwd
    /root
    [root@node5 ~]# mkdir reback
    [root@node5 ~]# cd reback/
    [root@node5 reback]# pwd
    /root/reback
    
    #通过inode结点查看被删除的文件名字
    #扩展:ext4文件系统的分区根目录的inode值为2,xfs分区根目录的inode值为64
    [root@node5 reback]# extundelete /dev/sdb5 --inode 2
    NOTICE: Extended attributes are not restored.
    Loading filesystem metadata ... 25 groups loaded.
    Group: 0
    Contents of inode 2:
    0000 | ed 41 00 00 00 04 00 00 78 fe 83 5f 76 fe 83 5f | .A......x.._v.._
    0010 | 76 fe 83 5f 00 00 00 00 00 00 03 00 02 00 00 00 | v.._............
    0020 | 00 00 08 00 08 00 00 00 0a f3 01 00 04 00 00 00 | ................
    0030 | 00 00 00 00 00 00 00 00 01 00 00 00 24 11 00 00 | ............$...
    0040 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
    0050 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
    0060 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
    0070 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
    
    Inode is Allocated
    File mode: 16877
    Low 16 bits of Owner Uid: 0
    Size in bytes: 1024
    Access time: 1602485880
      eation time: 1602485878
    ▽odification time: 1602485878
    Deletion Time: 0
    Low 16 bits of Group Id: 0
    Links count: 3
    Blocks count: 2
    File flags: 524288
    File version (for NFS): 0
    File ACL: 0
    Directory ACL: 0
    Fragment address: 0
    Direct blocks: 127754, 4, 0, 0, 1, 4388, 0, 0, 0, 0, 0, 0
    Indirect block: 0
    Double indirect block: 0
    Triple indirect block: 0
    
    File name                                       | Inode number | Deleted status
    .                                                 2
    ..                                                2
    lost+found                                        11
    passwd                                            12             Deleted
    hosts                                             13             Deleted
    grub2                                             14             Deleted
    a                                                 324            Deleted
    

    3.通过inode号恢复指定数据

    [root@node5 reback]# extundelete /dev/sdb5 --restore-inode 12
    

    4.通过文件名恢复指定数据

    [root@node5 reback]# extundelete /dev/sdb5 --restore-file hosts
    

    5.恢复指定目录的所有文件

    [root@node5 reback]# extundelete /dev/sdb5 --restore-directory a
    

    6.恢复所有文件

    [root@node5 reback]# extundelete /dev/sdb5 --restore-all
    

    7.注意:extundelete在恢复文件的时候不能自动创建空文件和空目录。例如

    image-20201013004650994

  • 相关阅读:
    数据结构.队列
    数据结构.栈
    数据结构.线性表(2)——链式表
    新标日初级:12(小李比森年轻)
    数据结构.线性表(1)——顺序表
    新标日初级:11(小野喜欢歌曲)
    新标日初级:10(京都的红叶很有名)
    crawlSpider
    爬虫如何将数据保存到mongodb数据库中
    爬虫如何将数据保存到mysql数据库
  • 原文地址:https://www.cnblogs.com/renshengdezheli/p/13952936.html
Copyright © 2011-2022 走看看