5.2 实战:在Centos6/RHEL6上恢复ext4文件系统下误删除的文件

 [root@xuegod63 ~]# rm -Rf /    #执行不成功的

rm: 在"/" 进行递归操作十分危险
rm: 使用 --no-preserve-root 选项跳过安全模式
[root@xuegod63 ~]# rm -rf /*    #这个可以执行成功。 呵呵。。。

ext4文件系统上删除文件,可以恢复: extundelete ,ext3恢复使用:ext3grep
windows恢复误删除的文件:  final data v2.0 汉化版  和  easyrecovery  

扩展:

Linux文件系统由三部分组成:文件名,inode,block
windows也由这三部分组成。
a.txt          -->inode              --> block
文件名       存放文件元数据信息       真正存放数据
查看文件文件名:

[root@xuegod63 ~]# cp /etc/passwd a.txt 
[root@xuegod63 ~]# ls a.txt 
a.txt

查看inode号:
常识: 每个文件,有一个inode号。

[root@xuegod63 ~]# ls -i a.txt
440266 a.txt

查看inode中的文件属性;  通过stat命令查看inode中包含的内容

[root@xuegod63 ~]# stat a.txt   #查看inode信息:
[root@xuegod63 ~]# ls -l a.txt 
-rw-r--r-- 1 root root 1720 Oct 25 10:21 a.txt

block块:真正存储数据的地方
逻辑删除: 
为什么删除比复制快?
 误删除文件后,第一件事要做什么???  你不心删除把存了几十年的大片删除了。
避免误删除的文件内容被覆盖。 如何避免?
卸载需要恢复文件的分区或以只读的方式挂载

5.2.2   实战:在ext4文件系统上恢复被误删除的文件

下载extundelete
http://sourceforge.net/    开源软件发布中心
准备测试分区:

[root@xuegod63 /]# fdisk /dev/sda  #创建一个sda4分区

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/sda: 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: 0x000b8b35

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1301    10240000   83  Linux
/dev/sda3            1301        1428     1024000   82  Linux swap / Solaris

Command (m for help): n   #创建一个新分区
Command action
   e   extended
   p   primary partition (1-4)
p  #创建一个主分区
Selected partition 4
First cylinder (1428-2610, default 1428): 
Using default value 1428
Last cylinder, +cylinders or +size{K,M,G} (1428-2610, default 2610): +1G  #指定分区大小

Command (m for help): w  #保存
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@xuegod63 ~]#reboot
或
[root@xuegod63 ~]# partx -a /dev/sda  #获得新分区表

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

使用新的分区表:

[root@xuegod63 /]# mkdir /tmp/sda4     #创建挂载点
[root@xuegod63 ~]# mkfs.ext4 /dev/sda4    #格式化
[root@xuegod63 ~]# mount /dev/sda4 /tmp/sda4/   #挂载

5.2.3   复制一些测试文件,然后把这些文件再删除,然后演示恢复:

[root@xuegod63 ~]# cp /etc/passwd /tmp/sda4/
[root@xuegod63 ~]# cp /etc/hosts /tmp/sda4/
[root@xuegod63 ~]# echo aaa > a.txt
[root@xuegod63 ~]# mkdir -p /tmp/sda4/a/b/c
[root@xuegod63 ~]# cp a.txt /tmp/sda4/a/
[root@xuegod63 ~]# cp a.txt /tmp/sda4/a/b/
[root@xuegod63 ~]# touch /tmp/sda4/a/b/kong.txt

安装tree命令:

[root@xuegod63 ~]# rpm -ivh /mnt/Packages/tree-1.5.3-2.el6.x86_64.rpm
[root@xuegod63 ~]# tree /tmp/sda4/
/tmp/sda4/
├── a
│   ├── a.txt
│   └── b
│       ├── a.txt
│       ├── c  #空目录
│       └── kong.txt  #空文件
├── hosts
├── lost+found
└── passwd

删除文件:

[root@xuegod63 ~]# cd /tmp/sda4/
[root@xuegod63 sda4]# ls
a  hosts  lost+found  passwd
[root@xuegod63 sda4]# rm -rf a hosts passwd 

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

[root@localhost ~]#cd /root
[root@localhost ~]# umount /tmp/sda4

5.2.4  安装extundelet  

上传extundelete到linux中:
从windows上传extundelete文件到linux,安装xmanager  v5   或者CRT

[root@xuegod63 ~]# rpm -ivh /mnt/Packages/lrzsz-0.12.20-27.1.el6.x86_64.rpm  

安装后,就有了rz命令和sz命令
rz : 上传windows中的文件到linux
sz :下载,将linux中的文件传到windows

解压并安装extundelet

[root@xuegod63 extundelete-0.2.4]# tar jxvf extundelete-0.2.4.tar.bz2 
[root@xuegod63 ~]# cd extundelete-0.2.4
[root@xuegod63]# yum install e2fsprogs-devel
[root@xuegod63 extundelete-0.2.4]# ./configure   #检查系统安装环境
[root@xuegod63 extundelete-0.2.4]# make  -j 4  #编译,把源代码编译成可执行的二进制文件。

 -j 4   使用4进程同时编译,提升编译速度 或 使用4核CPU同时编译。

[root@xuegod63 extundelete-0.2.4]# make install  #安装

install 和cp 有什么区别? 
install 复制时可以指定权限  cp不可以
例:

[root@xuegod63 ~]# install -m 777 /bin/find /opt/a.sh
[root@xuegod63 ~]# ll /opt/

5.2.5  开始恢复

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

[root@xuegod63 ~]# umount /tmp/sda4/ 
[root@xuegod63 ~]# mkdir test  #创建一个目录使用于存放恢复的数据
[root@xuegod63 ~]# cd test/

方法1:
通过inode结点查看被删除的文件名字:

[root@xuegod63 test]# extundelete /dev/sda4 --inode 2
.                                                2
lost+found                                        11
passwd                                            12             Deleted
hosts                                             13             Deleted
a                                                 7313           Deleted

扩展:ext4文件系统的分区根目录的inode值为2,xfs分区根目录的inode值为64

[root@xuegod63 test]# ls -id /boot/   #xfs文件系统
64 /boot/
[root@xuegod63 test]# mount /dev/sda4 /tmp/sda4/
[root@xuegod63 test]# ls -id /tmp/sda4/
2 /tmp/sda4/
[root@xuegod63 test]# umount /tmp/sda4/

方法1:通过inode结点恢复

[root@xuegod63 test]# extundelete /dev/sda4 --restore-inode 12
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 9 groups loaded.
Loading journal descriptors ... 63 descriptors loaded.
[root@xuegod63 test]# ls 
RECOVERED_FILES
[root@xuegod63 test]# diff /etc/passwd RECOVERED_FILES/file.12  # 没有任何输出,说明一样


方法二,通过文件名恢复

[root@xuegod63 test]# extundelete /dev/sda4 --restore-file passwd
[root@xuegod63 test]# diff /etc/passwd RECOVERED_FILES/passwd  # 没有任何输出,说明一样

方法三:恢复某个目录,如目录a下的所有文件:

[root@xuegod63 test]# extundelete /dev/sda4 --restore-directory a
[root@xuegod63 test]# tree RECOVERED_FILES/a/
RECOVERED_FILES/a/
├── a.txt
└── b
└── a.txt

下面是原来的目录结构:

[root@xuegod63 ~]# tree /root/sda4-back/a/
/root/sda4-back/a/
├── a.txt
└── b
    ├── a.txt
    ├── c
└── kong.txt

方法四:恢复所有的文件
[root@xuegod63 test]# extundelete /dev/sda4 --restore-all
删除前后的数据:
 
extundelete在恢复文件的时候能不能自动创建空文件和目录?
答:不能。

猜你喜欢

转载自blog.csdn.net/weixin_40744265/article/details/89470847
5.2
今日推荐