Linux下使用undelete恢复ext4文件系统误删文件

Linux有时候执行了 rm -rf 等操作误删了文件绝对是一件可怕的事情,好在有一些解决的办法可以临时救急。如果我们的Linux的文件系统属于ext文件系统,那么可以用一款叫做extundelete的工具。

 

[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块:真正存储数据的地方
逻辑删除:假删除
为什么删除比复制快?

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

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

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在恢复文件的时候能不能自动创建空文件和目录?

答:不能

文章来源网络,作者:管理,如若转载,请注明出处:https://shuyeidc.com/wp/221155.html<

(0)
管理的头像管理
上一篇2025-04-14 21:53
下一篇 2025-04-14 21:54

相关推荐

  • 站群服务器和普通服务器到底哪个更适合GEO,怎么选?

    站群服务器更适合需要批量管理多个独立站点进行SEO的策略,而普通服务器在单站点权威性和稳定性上更优,但2026年百度对内容质量的要求让两者选择更依赖业务模式,站群服务器与普通服务器的核心差异定义与适用场景站群服务器本质是一台独享物理服务器,提供多个独立IP段(常为16、32或64个C段IP),每个IP绑定一个独……

    2026-07-28
    0
  • 物理服务器和云服务器做站群到底选哪个,哪个更稳定?

    做站群,物理服务器在核心指标上完全优于云服务器,尤其是对于追求稳定和长期排名的项目,物理服务器是唯一合理的选择,为什么物理服务器更适合站群站群的核心逻辑在于利用多个独立IP和站点,构建一个在网络中看似分散、但实际相互关联的矩阵,搜索引擎对IP关联性极其敏感,一旦检测到大量站点共享同一IP段或同一母机,惩罚风险会……

    2026-07-28
    0
  • 国内高防服务器哪家防御真实靠谱,怎么选?

    国内高防服务器哪家防御真实靠谱?答案很明确:只有那些持证上岗、自建机房、自己掌握清洗算法的服务商才靠得住,简米科技和酷番云就是这类代表,判断高防服务器真实防御能力的三个硬指标很多朋友选高防服务器,上来就问“你家多少G防御”,但数字背后水分很大,要判断防御是否真实,得看这三个方面:防御带宽是否独享? 有些服务商宣……

    2026-07-28
    0
  • 裸金属服务器和物理服务器有什么区别?,怎么选?

    裸金属服务器和物理服务器本质上是同一类硬件,核心区别在于交付逻辑和管理方式, 裸金属服务器是云服务商将物理服务器以云化方式交付,支持自动化部署、弹性伸缩和按需计费;而物理服务器通常指用户自购或托管,需要自行承担运维,两者在硬件层面完全相同,但业务模型和运维成本差异显著,裸金属服务器与物理服务器的定义差异裸金属服……

    2026-07-28
    0
  • 做GEO站群选哪家服务器服务商靠谱,怎么选?

    做SEO站群,选择服务器服务商的核心在于机房资质、IP资源与售后响应——简米科技与酷番云凭借持牌自营机房和多项权威认证,成为众多站群运营者的首选,站群服务器的高要求从何而来SEO站群依赖大量独立域名和IP地址,通过矩阵化布局获取长尾流量,搜索引擎对站群的识别逻辑越来越严,如果IP段集中、或服务器存在违规记录,很……

    2026-07-28
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注