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

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

相关文章
|
5月前
|
Ubuntu Linux Anolis
Linux系统禁用swap
本文介绍了在新版本Linux系统(如Ubuntu 20.04+、CentOS Stream、openEuler等)中禁用swap的两种方法。传统通过注释/etc/fstab中swap行的方式已失效,现需使用systemd管理swap.target服务或在/etc/fstab中添加noauto参数实现禁用。方法1通过屏蔽swap.target适用于新版系统,方法2通过修改fstab挂载选项更通用,兼容所有系统。
509 3
Linux系统禁用swap
|
5月前
|
Linux
Linux系统修改网卡名为eth0、eth1
在Linux系统中,可通过修改GRUB配置和创建Udev规则或使用systemd链接文件,将网卡名改为`eth0`、`eth1`等传统命名方式,适用于多种发行版并支持多网卡配置。
1016 3
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
602 3
Linux系统初始化脚本
|
6月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
441 18
|
5月前
|
安全 Linux Shell
Linux系统提权方式全面总结:从基础到高级攻防技术
本文全面总结Linux系统提权技术,涵盖权限体系、配置错误、漏洞利用、密码攻击等方法,帮助安全研究人员掌握攻防技术,提升系统防护能力。
568 1
|
5月前
|
监控 安全 Linux
Linux系统提权之计划任务(Cron Jobs)提权
在Linux系统中,计划任务(Cron Jobs)常用于定时执行脚本或命令。若配置不当,攻击者可利用其提权至root权限。常见漏洞包括可写的Cron脚本、目录、通配符注入及PATH变量劫持。攻击者通过修改脚本、创建恶意任务或注入命令实现提权。系统管理员应遵循最小权限原则、使用绝对路径、避免通配符、设置安全PATH并定期审计,以防范此类攻击。
1185 1
|
6月前
|
缓存 监控 Linux
Linux系统清理缓存(buff/cache)的有效方法。
总结而言,在大多数情形下你不必担心Linux中buffer与cache占用过多内存在影响到其他程序运行;因为当程序请求更多内存在没有足够可用资源时,Linux会自行调整其占有量。只有当你明确知道当前环境与需求并希望立即回收这部分资源给即将运行重负载任务之前才考虑上述方法去主动干预。
1971 10
|
存储 Linux 数据安全/隐私保护
8.13 Linux删除系统用户(userdel命令)
userdel 命令功能很简单,就是删除用户的相关数据。此命令只有 root 用户才能使用。
404 0
8.13 Linux删除系统用户(userdel命令)
|
Linux
3.11 Linux删除空目录(rmdir命令)
和 mkdir 命令(创建空目录)恰好相反,rmdir(remove empty directories 的缩写)命令用于删除空目录,此命令的基本格式为:
467 0
3.11 Linux删除空目录(rmdir命令)
|
Linux
linux系统进程删除命令
linux学习记录
368 0