首先说明:

[root@CentOS6 ~]# rm -rf /      //这条命令不可以执行
[root@CentOS6 ~]# rm -rf /* //这条命令可以执行,别去试

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

  误删除文件后,第一件事是避免误删除的文件内容被覆盖,这时可以卸载需要恢复文件的分区或以只读的方式挂载。

(1).下载extundelete

https://sourceforge.net/          开源软件发布中心

https://github.com/                   github项目托管平台

(2).准备实验环境

VMare12  CentOS6.8  添加一块硬盘20G

不会看这里,Linux空硬盘从分区到挂载

这里创建sdb1分区,挂载到/newpar下。

还有将CentOS6.8的光盘镜像挂载到/mnt下。

(3).复制一些测试文件,然后删除,以备测试恢复。

这里为了更好的展示,安装一下tree。

[root@centos6 ~]# rpm -ivh /mnt/Packages/tree-1.5.3-3.el6.x86_64.rpm
warning: /mnt/Packages/tree-1.5.3-3.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing... ########################################### [100%]
1:tree ########################################### [100%]

下面开始复制文件

[root@centos6 ~]# cp /etc/passwd /newpar/
[root@centos6 ~]# cp /etc/hosts /newpar/
[root@centos6 ~]# echo abc > a.txt
[root@centos6 ~]# mkdir -p /newpar/a/b/c/
[root@centos6 ~]# cp a.txt /newpar/a/
[root@centos6 ~]# cp a.txt /newpar/a/b/
[root@centos6 ~]# touch /newpar/a/b/test.txt
[root@centos6 ~]# tree /newpar/
/newpar/
├── a
│   ├── a.txt
│   └── b
│   ├── a.txt
│   ├── c    //空的
│   └── test.txt    //空的
├── hosts
├── lost+found
└── passwd
4 directories, 5 files

下面开始删除

[root@centos6 ~]# rm -rf /newpar/{a,hosts,passwd}
[root@centos6 ~]# ls /newpar/
lost+found

删完了,记得误删除第一步,卸载分区。如果是根目录看(7).扩展2

[root@centos6 ~]# umount /newpar/    //不能在挂载点下卸载

使用df -a查看文件系统的挂载点

[root@centos6 ~]# df -a
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg_centos6-lv_root
17938864 3958368 13062584 24% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
tmpfs 953652 72 953580 1% /dev/shm
/dev/sda1 487652 40913 421139 9% /boot
/dev/sr0 3824484 3824484 0 100% /mnt
none 0 0 0 - /proc/sys/fs/binfmt_misc
[root@centos6 ~]# mkdir /cdrom
[root@centos6 ~]# mount /dev/sr0 /cdrom/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@centos6 ~]# df -a
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg_centos6-lv_root
17938864 3958372 13062580 24% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
tmpfs 953652 72 953580 1% /dev/shm
/dev/sda1 487652 40913 421139 9% /boot
/dev/sr0 3824484 3824484 0 100% /mnt
none 0 0 0 - /proc/sys/fs/binfmt_misc
/dev/sr0 3824484 3824484 0 100% /cdrom

(4).安装extundelete

将下载好的extundelete上传到服务器中。

[root@centos6 ~]# ls
anaconda-ks.cfg install.log 模板 文档 桌面
a.txt install.log.syslog 视频 下载
extundelete-0.2.4.tar.bz2 公共的 图片 音乐

解压

[root@centos6 ~]# tar -jxvf extundelete-0.2.4.tar.bz2    //-j过滤bz2格式

准备依赖包(我的CentOS6.8镜像好像不全,其实还可以用rpm -ivh安装gcc-c++和e2fsprogs-devel)

[root@centos6 extundelete-0.2.4]# yum -y install gcc-c++
[root@centos6 extundelete-0.2.4]# yum -y install e2fsprogs-devel

安装extundelete

[root@centos6 ~]# cd extundelete-0.2.4
[root@centos6 extundelete-0.2.4]# ./configure    //检查系统安装环境,为了生成Makefile文件
Configuring extundelete 0.2.4
Writing generated files to disk
[root@centos6 extundelete-0.2.4]# make -j 4    //编译,把源代码编译成可执行的二进制文件。-j 4使用4进程或4核同时编译,提高编译速度。根据实际配置修改
make -s all-recursive
Making all in src
extundelete.cc:571: 警告:未使用的参数‘flags’
[root@centos6 extundelete-0.2.4]# make install    //安装
Making install in src
/usr/bin/install -c extundelete '/usr/local/bin'    //在此目录下

(5).恢复文件

创建一个文件夹,将恢复的文件保存到文件夹内

[root@centos6 ~]# umount /dev/sdb1    //确保卸载分区
umount: /dev/sdb1: not mounted
[root@centos6 ~]# mkdir test    //创建test文件夹
[root@centos6 ~]# cd test/
[root@centos6 test]# ls

查看inode号

[root@centos6 test]# extundelete /dev/sdb1 --inode 2    //ext4文件系统分区根目录的inode值为2,xfs文件系统分区根目录的inode值为64。
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 160 groups loaded.
Group: 0
Contents of inode 2:
0000 | ed 41 00 00 00 10 00 00 60 7b 2e 5c 4e 7b 2e 5c | .A......`{.\N{.\
0010 | 4e 7b 2e 5c 00 00 00 00 00 00 03 00 08 00 00 00 | N{.\............
0020 | 00 00 00 00 06 00 00 00 21 24 00 00 00 00 00 00 | ........!$......
0030 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 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 | ................
0080 | 1c 00 00 00 a8 37 a3 c1 a8 37 a3 c1 7c d2 20 60 | .....7...7..|. `
0090 | 7a 72 2e 5c 00 00 00 00 00 00 00 00 00 00 00 00 | zr.\............
00a0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00b0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00c0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00d0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00e0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00f0 | 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: 4096
Access time: 1546550112
Creation time: 1546550094
Modification time: 1546550094
Deletion Time: 0
Low 16 bits of Group Id: 0
Links count: 3
Blocks count: 8
File flags: 0
File version (for NFS): 0
File ACL: 0
Directory ACL: 0
Fragment address: 0
Direct blocks: 9249, 0, 0, 0, 0, 0, 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
a 262145 Deleted

1)通过inode恢复

根据上面的inode值,使用--restore-inode选项恢复passwd

[root@centos6 test]# extundelete /dev/sdb1 --restore-inode 12
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 160 groups loaded.
Loading journal descriptors ... 67 descriptors loaded.
[root@centos6 test]# ls -l
总用量 4
drwxr-xr-x. 2 root root 4096 1月 4 07:07 RECOVERED_FILES    //可以看到一个新的文件夹
[root@centos6 test]# cd RECOVERED_FILES/
[root@centos6 RECOVERED_FILES]# ls
file.12    //这就是恢复出来的文件
[root@centos6 RECOVERED_FILES]# diff /etc/passwd file.12  //比较一下是否有不同。没有输出就是一样。

2)通过文件名恢复

也可以根据上面的文件名,使用--restore-file选项恢复passwd。这样还可以同时还原文件名

[root@centos6 test]# extundelete /dev/sdb1 --restore-file passwd
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 160 groups loaded.
Loading journal descriptors ... 67 descriptors loaded.
Successfully restored file passwd
[root@centos6 test]# ls RECOVERED_FILES/
file.12 passwd    //可以看到恢复出了一个名为passwd的文件
[root@centos6 test]# diff RECOVERED_FILES/file.12 RECOVERED_FILES/passwd    //两个文件内容是一样的

3)恢复某个目录

使用--restore-directory选项恢复文件夹a。注意:空目录和空文件无法恢复

[root@centos6 test]# extundelete /dev/sdb1 --restore-directory a
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 160 groups loaded.
Loading journal descriptors ... 67 descriptors loaded.
Searching for recoverable inodes in directory a ...
7 recoverable inodes found.
Looking through the directory structure for deleted files ...
3 recoverable inodes still lost.    //3个可回收的inode仍然丢失
[root@centos6 test]# tree RECOVERED_FILES/a/
RECOVERED_FILES/a/
├── a.txt
└── b
└── a.txt    //少了空文件夹c和空文件test.txt 1 directory, 2 files

4)恢复所有文件

使用--restore-all选项恢复所有文件。注意:空目录和空文件无法恢复

[root@centos6 test]# rm -rf *
[root@centos6 test]# ls
[root@centos6 test]# extundelete /dev/sdb1 --restore-all
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 160 groups loaded.
Loading journal descriptors ... 67 descriptors loaded.
Searching for recoverable inodes in directory / ...
7 recoverable inodes found.
Looking through the directory structure for deleted files ...
0 recoverable inodes still lost.
[root@centos6 test]# tree RECOVERED_FILES/
RECOVERED_FILES/
├── a
│   ├── a.txt
│   └── b
│   └── a.txt    //少了空目录c和空文件test.txt
├── hosts
└── passwd 2 directories, 4 files

(6).扩展1:

Linux文件系统由三部分组成:文件名,inode(存放文件元数据信息),block(真正存放数据)。Windows也由这三部分组成。

1)查看inode号

[root@CentOS6 ~]# ls -i anaconda-ks.cfg
137428 anaconda-ks.cfg

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

[root@CentOS6 ~]# stat anaconda-ks.cfg
File: "anaconda-ks.cfg"
Size: 1700 Blocks: 8 IO Block: 4096 普通文件
Device: fd00h/64768d Inode: 137428 Links: 1
Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-09-01 18:01:33.658648102 +0800
Modify: 2018-03-13 18:29:40.674999889 +0800
Change: 2018-03-13 18:29:48.813999887 +0800

2)Block块:真正存储数据的地方

为什么删除比复制块?因为一般的删除都是逻辑删除,只删除了文件名。当有新的文件占用inode和block,此时只有找专业的数据恢复公司,通过奇偶校验找回文件。

(7).扩展2:

如果想恢复根下删除的文件怎么办?

方法一:立即断电(因为正常关机会产生日志,防止日志对数据覆盖),然后把磁盘以只读方式挂在到另一台相同相同的电脑中进行恢复

方法二:把extundelete在另一台相同相同的电脑上安装好,然后复制到U盘中。把U盘插入服务器,恢复时将恢复的文件保存到U盘中(不要让恢复的数据写到根下,那样会覆盖之前删除的文件)

在CentOS6或RHEL6恢复上ext4文件系统误删除的文件的更多相关文章

  1. 恢复Linux下被误删除的文件(笔记)

    恢复Linux下被误删除的文件 [root@xuegod63 ~]# mount /dev/cdrom /mnt/ 分一个区:sda4  查找:extundelete 分一个区:sda4  [root ...

  2. 如何恢复Eclipse中被误删除的文件

    在使用Eclipse时,可能会不小心误删除一些文件,没关系,Eclipse有个非常强大的功能,能让这些误删除的文件恢复回来,下面就来介绍一下. 工具/原料   Eclipse Kepler 方法/步骤 ...

  3. Linux高级运维 第五章 Vim编辑器和恢复ext4下误删除的文件-Xmanager工具

    5.1  vim主要模式介绍,vim命令模式. 确保系统已经安装了VIM工具 [root@panda ~]# rpm -qf `which vim` [root@panda ~]# rpm -qf ` ...

  4. NTFS(Windows)、ext4(RHEL6)和xfs(RHEL7)文件系统的误删除恢复和备份

    前言 对于误删除文件的设备,要马上停止任何写的操作,防止删除的文件被覆盖,导致数据丢失! 恢复NTFS文件系统下误删的文件 以Windows为例,市面上能恢复的工具不少,例如EasyRecovery. ...

  5. 恢复ext4文件系统superblock

    恢复ext4文件系统superblock 1. Create ext4 文件系统. [root@localhost ~]# mkfs.ext4 /dev/vdb1 [root@localhost ~] ...

  6. 高性能Linux服务器 第6章 ext3文件系统反删除利器ext3grep extundelete工具恢复rm -rf 误删除的文件

    高性能Linux服务器 第6章  ext3文件系统反删除利器ext3grep  extundelete工具恢复rm -rf 误删除的文件 只能用于ext3文件系统!!!!!!!高俊峰(高性能Linux ...

  7. 第5章 Linux上管理文件系统

    5.1 机械硬盘 机械硬盘由多块盘片组成,它们都绕着主轴旋转.每块盘片上下方都有读写磁头悬浮在盘片上下方,它们与盘片的距离极小.在每次读写数据时盘片旋转,读写磁头被磁臂控制着不断的移动来读取其中的数据 ...

  8. 一例Ext4文件系统fsck后损坏的修复过程

    1.故障发生背景 Ext4文件系统没有umount下来,之后做了fsck操作检查一致性,结果导致Ext4文件mount不上(有时也会表现为导致目录变成了文件). 报错提示信息:mount: wrong ...

  9. Ext4文件系统修复

    Ext4文件系统修复 目录 一. super block........................................................................ ...

随机推荐

  1. C11性能之道:标准库优化

    1.emplace_back减少内存拷贝和移动 emplace_back能通过参数构造对象,不需要拷贝或者移动内存,相比pusk_back能更好的避免内存的拷贝和移动,使容器插入元素性能得到进一步提升 ...

  2. linux内存相关好文(转)

    话说团队的兄弟有一天问我,为啥咱唯一的一个服务器,内存都用完了,我还想在上面测性能呢.我一听,第一反应:不可能!我说你胡扯呢吧,咱那可是16G的一个物理机,上面就跑了git服务器,怎么可能把内存吃完了 ...

  3. GitLab 迁移与升级

    参考: [ 博客园 BigBao ] 环境说明: OS: CentOS 7.x gitlab-ce 初始版本: 8.8.5 gitlab-ce 升级到版本: 11.2.3 升级方式: rpm 安装升级 ...

  4. MyBatis 系列五 之 延迟加载、一级缓存、二级缓存设置

    MyBatis的延迟加载.一级缓存.二级缓存设置 首先我们必须分清延迟加载的适用对象 延迟加载 MyBatis中的延迟加载,也称为懒加载,是指在进行关联查询时,按照设置延迟加载规则推迟对关联对象的se ...

  5. Python中的异常处理 -- (转)

    python中的异常   异常是指程序中的例外,违例情况.异常机制是指程序出现错误后,程序的处理方法.当出现错误后,程序的执行流程发生改变,程序的控制权转移到异常处理. Exception类是常用的异 ...

  6. 用python玩微信(聊天机器人,好友信息统计)

    1.用 Python 实现微信好友性别及位置信息统计 这里使用的python3+wxpy库+Anaconda(Spyder)开发.如果你想对wxpy有更深的了解请查看:wxpy: 用 Python 玩 ...

  7. linux内核网络接收数据流程图【转】

    转自:http://blog.chinaunix.net/uid-23069658-id-3141409.html 4.3 数据接收流程图   各层主要函数以及位置功能说明:          1)s ...

  8. C基础 常用设计模式粗解

    引言 面向对象, 设计模式是现代软件开发基石. C的面向过程已经很简洁, 但不代表C就没有面向对象.(libuv框架中C面向对象用的很多) 因为思想是互通的.全当熟悉一下那些常用的设计模式.先假定有一 ...

  9. 网站服务器压力Web性能测试(4):服务器压力Web性能测试小结

    1.Apache Bench,Webbench,http_load对网站压力Web性能进行测试时,为了得到更加客观和准确的数值,应该从远程访问.局域网访问和本地等多个方面进行全方位的测试.一般用127 ...

  10. yii2 一对多关系的对分页造成的影响

    下面代码中关联descies时,匹配较多,造成分页数不对,需要加条件限制: $model = User::find() ->joinWith('app') ->joinWith(['des ...