zoukankan      html  css  js  c++  java
  • Linux系统中文件被删除后的恢复方法(ext4)

    本文档给出了恢复ext4文件系统被误删的文件的方法,需要使用的软件是extundelete,这款软件对ext4文件系统的恢复成功率比较高,值得拥有!

    恢复ext4文件系统被误删的文件恢复ext4文件系统被误删的文件

    [root@localhost ~]# rm -Rf /   #执行不成功的!
    rm: 在"/" 进行递归操作十分危险
    rm: 使用 --no-preserve-root 选项跳过安全模式
    [root@localhost ~]# rm -rf /*    #这个可以执行成功! 呵呵。。。
    
    ext4文件系统上删除文件,可以恢复: extundelete ,ext3恢复使用:ext3grep
    windows恢复误删除的文件:  final data v2.0 汉化版  和  easyrecovery  
    

    扩展:

    Linux文件系统由三部分组成:文件名,inode,block
    windows也由这三部分组成。
    a.txt          -->inode              --> block
    文件名       存放文件元数据信息       真正存放数据
    查看文件文件名:
    [root@localhost ~]# cp /etc/passwd a.txt
    [root@localhost ~]# ls a.txt
    a.txt
    
    查看inode号:
    常识: 每个文件,有一个inode号。
    [root@localhost ~]# ls -i a.txt
    440266 a.txt
    查看inode中的文件属性;  通过stat命令查看inode中包含的内容
    [root@localhost ~]# stat a.txt   #查看inode信息:
    [root@localhost ~]# ls -l a.txt
    -rw-r--r-- 1 root root 1720 Oct 25 10:21 a.txt
    
    block块:真正存储数据的地方
    逻辑删除:假删除
    为什么删除比复制快?
    

    恢复ext4文件系统被误删的文件恢复ext4文件系统被误删的文件

    误删除文件后,第一件事要做什么?  你不心删除把存了几十年的大片删除了!
    要避免误删除的文件内容被覆盖,如何避免?

    卸载需要恢复文件的分区或者以只读的方式挂载
    例如:

    mount -o remount,ro /mnt
    实战:恢复ext4文件系统被误删的文件

    下载extundelete
    http://sourceforge.net/   开源软件发布中心

    extundelete-0.2.4.tar.bz2
    链接:https://pan.baidu.com/s/1n0dtGnhffcH7XrLv0TqUsw
    提取码:a5m7

    准备测试分区:
    [root@localhost ~]# ls /dev/sd*
    /dev/sda  /dev/sda1  /dev/sda2  /dev/sdb
    [root@localhost ~]# fdisk /dev/sdb
    Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
    Building a new DOS disklabel with disk identifier 0x539f33b8.
    Changes will remain in memory only, until you decide to write them.
    After that, of course, the previous content won't be recoverable.
    
    Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
    
    WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
             switch off the mode (command 'c') and change display units to
             sectors (command 'u').
    
    Command (m for help): p #查看分区表信息
    
    Disk /dev/sdb: 21.5 GB, 21474836480 bytes
    255 heads, 63 sectors/track, 2610 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x539f33b8
    
       Device Boot      Start         End      Blocks   Id  System
    
    Command (m for help): n #创建一个新分区
    Command action
       e   extended
       p   primary partition (1-4)
    p #创建一个主分区
    Partition number (1-4): 1
    First cylinder (1-2610, default 1): 
    Using default value 1
    Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1G #指定分区大小
    
    Command (m for help): p #查看分区表信息
    
    Disk /dev/sdb: 21.5 GB, 21474836480 bytes
    255 heads, 63 sectors/track, 2610 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x539f33b8
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1         132     1060258+  83  Linux
    
    Command (m for help): w #保存
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    
    [root@localhost ~]# partx -a /dev/sdb1  #获得新分区表
    或者
    [root@localhost ~]# reboot
    

    扩展:
    如果在根下删除文件了,想恢复,怎么办?
    方法1: 立即断电,然后把磁盘以只读方式,挂载到另一个电脑中进行恢复。
    方法2:把extundelete在虚拟机上(虚拟机系统要和服务器版本一样),提前安装好后再复制到U盘中,把U盘插入服务器,恢复时,恢复的文件要保存到U盘中,(不要让恢复的数据写到/下,那样会覆盖之前删除的文件)。

    使用新的分区:
    [root@localhost ~]# mkdir /tmp/sdb1 #创建挂载点
    [root@localhost ~]# mkfs.ext4 /dev/sdb1 #把/dev/sdb1分区文件系统格式化成ext4
    [root@localhost ~]# mount /dev/sdb1 /tmp/sdb1 #把/dev/sdb1分区挂到/tmp/sdb1
    [root@localhost ~]# df -h 
    Filesystem                    Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root   18G  1.3G   16G   8% /
    tmpfs                         499M     0  499M   0% /dev/shm
    /dev/sda1                     485M   33M  427M   8% /boot
    /dev/sr0                      3.6G  3.6G     0 100% /media/cdrom
    /dev/sdb1                    1020M   34M  935M   4% /tmp/sdb1
    
    复制一些测试文件,然后把这些文件再删除,然后演示恢复:
    [root@localhost ~]# cp /etc/passwd /tmp/sdb1
    [root@localhost ~]# cp /etc/hosts /tmp/sdb1 
    [root@localhost ~]# echo aaa > a.txt
    [root@localhost ~]# mkdir -p /tmp/sdb1/a/b/c
    [root@localhost ~]# cp a.txt /tmp/sdb1/a
    [root@localhost ~]# cp a.txt /tmp/sdb1/a/b
    [root@localhost ~]# touch /tmp/sdb1/a/b/kong.txt
    [root@localhost ~]# yum install -y tree
    [root@localhost ~]# tree /tmp/sdb1
    /tmp/sdb1
    ├── a
    │   ├── a.txt
    │   └── b
    │       ├── a.txt
    │       ├── c #空目录
    │       └── kong.txt #空文件
    ├── hosts
    ├── lost+found
    └── passwd
    
    4 directories, 5 files
    
    删除文件:
    [root@localhost ~]# cd /tmp/sdb1
    [root@localhost sdb1]# ls
    a  hosts  lost+found  passwd
    [root@localhost sdb1]# rm -rf a hosts passwd 
    [root@localhost sdb1]# ls
    lost+found
    

    误删除文件后,第一件事要做什么???
    如何避免误删除的文件内容被覆盖???
    卸载需要恢复文件的分区或者以只读的方式挂载

    [root@localhost sdb1]# cd /root
    [root@localhost ~]# df -h
    Filesystem                    Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root   18G  1.3G   16G   8% /
    tmpfs                         499M     0  499M   0% /dev/shm
    /dev/sda1                     485M   33M  427M   8% /boot
    /dev/sr0                      3.6G  3.6G     0 100% /media/cdrom
    /dev/sdb1                    1020M   34M  935M   4% /tmp/sdb1
    [root@localhost ~]# echo "/dev/sdb1 /tmp/sdb1 ext4 defaults 0 0" >> /etc/fstab 
    [root@localhost ~]# mount -o remount,ro /tmp/sdb1 #以读写的形式重新挂载/tmp/sdb1所在分区
    [root@localhost ~]# df -h
    Filesystem                    Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root   18G  1.3G   16G   8% /
    tmpfs                         499M     0  499M   0% /dev/shm
    /dev/sda1                     485M   33M  427M   8% /boot
    /dev/sr0                      3.6G  3.6G     0 100% /media/cdrom
    /dev/sdb1                    1020M   34M  935M   4% /tmp/sdb1
    [root@localhost ~]# touch /tmp//sdb1/testfile
    touch: cannot touch `/tmp//sdb1/testfile': Read-only file system
    

    或者

    [root@localhost ~]# umount /tmp/sdb1 #卸载/tmp/sdb1所在分区
    [root@localhost ~]# df -h
    Filesystem                    Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root   18G  1.3G   16G   8% /
    tmpfs                         499M     0  499M   0% /dev/shm
    /dev/sda1                     485M   33M  427M   8% /boot
    /dev/sr0                      3.6G  3.6G     0 100% /media/cdrom
    
    安装extundelete工具 
    上传extundelete到Linux中:
    从Windows上传extundelete文件到Linux,安装SecureCRT或者XShell
    [root@localhost ~]# yum install -y lrzsz  
    # 安装后就有了rz命令和sz命令
    rz: 将Windows中的文件上传到Linux
    sz: 将Linux中的文件下载到Windows
    
    源码安装extundelete
    [root@localhost ~]# cd /usr/local/src
    [root@localhost src]# ls
    [root@localhost src]# rz
    rz waiting to receive.
     zmodem trl+C ȡ
      100%     105 KB  105 KB/s 00:00:01       0 Errorsbz2...
    
    [root@localhost src]# ls
    extundelete-0.2.4.tar.bz2
    [root@localhost src]# tar xjvf extundelete-0.2.4.tar.bz2 
    [root@localhost src]# cd extundelete-0.2.4
    [root@localhost extundelete-0.2.4]# yum install -y e2fsprogs-devel gcc*
    [root@localhost extundelete-0.2.4]# ./configure   #检查系统安装环境
    [root@localhost extundelete-0.2.4]# make  -j 4  #编译,把源代码编译成可执行的二进制文件。 -j 4   使用4进程同时编译,提升编译速度或者使用4核CPU同时编译。
    [root@localhost extundelete-0.2.4]# make install  #编译安装
    

    扩展:

    install 和cp 有什么区别? 
    install 复制时可以指定权限  cp不可以
    例:
    [root@localhost ~]# install -m 777 /bin/find /opt/a.sh
    [root@localhost ~]# ll /opt/
    
    开始恢复:

    方法一:通过inode结点恢复
    方法二:通过文件名恢复
    方法三:恢复某个目录,如目录a下的所有文件:
    方法四:恢复所有的文件

    [root@localhost extundelete-0.2.4]# mkdir /test #创建一个目录使用于存放恢复的数据
    [root@localhost extundelete-0.2.4]# cd /test
    [root@localhost test]# 
    
    
    通过inode结点查看被删除的文件名字:
    [root@localhost test]# extundelete /dev/sdb1 --inode 2  
    File name                                       | Inode number | Deleted status
    .                                                 2
    ..                                                2
    lost+found                                        11
    passwd                                            12             Deleted
    hosts                                             13             Deleted
    a                                                 7377           Deleted
    
    扩展:ext4文件系统的分区根目录的inode值为2,xfs分区根目录的inode值为64
    
    [root@localhost test]# ls -id /boot/   #xfs文件系统
    64 /boot/
    
    [root@localhost test]# ls -id /tmp/sdb1
    2 /tmp/sdb1
    
    方法一:通过inode结点恢复
    [root@localhost test]# ls
    [root@localhost test]# extundelete /dev/sdb1 --restore-inode 12
    NOTICE: Extended attributes are not restored.
    Loading filesystem metadata ... 9 groups loaded.
    Loading journal descriptors ... 61 descriptors loaded.
    [root@localhost test]# ls
    RECOVERED_FILES
    [root@localhost test]# ls RECOVERED_FILES/
    file.12
    [root@localhost test]# diff /etc/passwd  RECOVERED_FILES/file.12 #对比文件内容,没有任何输出,说明恢复后的文件内容没有变化
    
    方法二:通过文件名恢复
    [root@localhost test]# extundelete /dev/sdb1 --restore-file passwd
    [root@localhost test]# diff /etc/passwd RECOVERED_FILES/passwd #对比文件内容,没有任何输出,说明恢复后的文件内容没有变化
    
    方法三:恢复某个目录,如目录a下的所有文件:
    [root@localhost test]# extundelete /dev/sdb1 --restore-directory a
    [root@localhost test]#  tree RECOVERED_FILES/a/
    RECOVERED_FILES/a/
    ├── a.txt
    └── b
        └── a.txt
    
    1 directory, 2 files
    
    方法四:恢复所有的文件
    [root@localhost test]# rm -rf RECOVERED_FILES/*
    [root@localhost test]# extundelete /dev/sdb1 --restore-all
    [root@localhost test]# ls RECOVERED_FILES/
    a  hosts  passwd
    [root@localhost test]# tree  RECOVERED_FILES/
    RECOVERED_FILES/
    ├── a
    │   ├── a.txt
    │   └── b
    │       └── a.txt
    ├── hosts
    └── passwd
    
    2 directories, 4 files
    
    数据对比
    删除前:
    [root@localhost ~]# tree /tmp/sdb1
    /tmp/sdb1
    ├── a
    │   ├── a.txt
    │   └── b
    │       ├── a.txt
    │       ├── c #空目录
    │       └── kong.txt #空文件
    ├── hosts
    ├── lost+found
    └── passwd
    
    4 directories, 5 files
    
    恢复后:
    [root@localhost test]# tree  RECOVERED_FILES/
    RECOVERED_FILES/
    ├── a
    │   ├── a.txt
    │   └── b
    │       └── a.txt
    ├── hosts
    └── passwd
    
    2 directories, 4 files
    

    extundelete在恢复文件的时候能不能自动创建空文件和目录?
    答:不能

  • 相关阅读:
    hdu 4614 线段树 二分
    cf 1066d 思维 二分
    lca 最大生成树 逆向思维 2018 徐州赛区网络预赛j
    rmq学习
    hdu 5692 dfs序 线段树
    dfs序介绍
    poj 3321 dfs序 树状数组 前向星
    cf 1060d 思维贪心
    【PAT甲级】1126 Eulerian Path (25分)
    【PAT甲级】1125 Chain the Ropes (25分)
  • 原文地址:https://www.cnblogs.com/linuxprobe/p/12774954.html
Copyright © 2011-2022 走看看