Ubuntu ext4文件系统下恢复误删除文件

本文详细记录了在Ubuntu系统下,如何使用extundelete工具来恢复ext3/4文件系统中被rm -rf误删除的文件。首先介绍了所需依赖和extundelete的安装步骤,然后讲解了如何卸载目标分区、检查剩余空间,最后展示了如何恢复单个文件、文件夹或所有删除文件的命令示例。

linux ext3 ext4文件系统下 恢复 rm -rf 误删除文件  

今天一时手快 rm -rf .* ,删除了一个重要邮件目录,幸好通过extundelete恢复了。
记下操作流程:
1.准备工作
主要通过 
extundelete 0.2.0完成恢复
需要依赖两个包

sudo aptitude install e2fsprogs e2fslibs-dev

下载页面http://extundelete.sourceforge.net/

快速下载: download extundelete

tar -xjf extundelete-0.2.0.tar.bz2

cd extundelete-0.2.0

编译三部曲

./configure
make
sudo make install

2.如果分区已经挂载,先umount,并且在将要恢复的地方查看空间大小

df -h ./

文件系统               1K-块        已用     可用 已用% 挂载点
/dev/sda3             18129292   2651744  14541764  16% /
/dev/sda1               194442     12197    172206   7% /boot

...

要 umount  /home!!!

如果你要恢复的是根目录或者系统目录就只有拿硬盘挂到另外一台电脑上操作了。

3.恢复制定分区下所有删除文件,默认恢复到执行命令的目录下,文件夹名字为RECOVERED_FILES,使用详情请参考:extundelete --help 运行的结果

恢复/dev/sda3里一个被误删除的文件

 

extundelete /dev/sda3 --restore-file /an/important/file

 

恢复/dev/sda3里一个被误删除的文件夹

extundelete /dev/sda3 --restore-directory /an/important

eg: grant/workspaces/...

 

恢复/dev/sda3里所有被删除的东西

extundelete /dev/sda3 --restore-all

sudo extundelete /dev/sda8 –restore-all

---------------------------------------------

$ extundelete --help

Usage: extundelete [options] [--] device-file

Options:

  --version, -[vV]       Print version and exit successfully.

  --help,                Print this help and exit successfully.

  --superblock           Print contents of superblock in addition to the rest.

                         If no action is specified then this option is implied.

  --journal              Show content of journal.

  --after dtime          Only process entries deleted on or after 'dtime'.

  --before dtime         Only process entries deleted before 'dtime'.

Actions:

  --inode ino            Show info on inode 'ino'.

  --block blk            Show info on block 'blk'.

  --restore-inode ino[,ino,...]

                         Restore the file(s) with known inode number 'ino'.

                         The restored files are created in ./RESTORED_FILES

                         with their inode number as extension (ie, file.12345).

  --restore-file 'path'  Will restore file 'path'. 'path' is relative to root

                         of the partition and does not start with a '/' (it

                         must be one of the paths returned by --dump-names).

                         The restored file is created in the current

                         directory as 'RECOVERED_FILES/path'.

  --restore-files 'path' Will restore files which are listed in the file 'path'.

                         Each filename should be in the same format as an option

                         to --restore-file, and there should be one per line.

  --restore-all          Attempts to restore everything.

  -j journal             Reads an external journal from the named file.

  -b blocknumber         Uses the backup superblock at blocknumber when opening

                         the file system.

  -B blocksize           Uses blocksize as the block size when opening the file

                         system.  The number should be the number of bytes.

[root@yznvm1 yzn]# extundelete /dev/sda3 --restore-file /home/yzn/helloworld/Makefile.am        
WARNING: Extended attributes are not restored.
WARNING: EXT3_FEATURE_INCOMPAT_RECOVER is set.
The partition should be unmounted to undelete any files without further data loss.
If the partition is not currently mounted, this message indicates 
it was improperly unmounted, and you should run fsck before continuing.
If you decide to continue, extundelete may overwrite some of the deleted
files
and make recovering those files impossible.  You should unmount the
file system and check it with fsck before using extundelete.
Would you like to continue? (y/n) 
y
Loading filesystem metadata ... 143 groups loaded.
Loading journal descriptors ... 29535 descriptors loaded.
Writing output to directory RECOVERED_FILES/
Restored inode 4223596 to file RECOVERED_FILES/home/yzn/helloworld/Makefile.am

 

ok,这样就恢复成功了。

当然文件不能保证一定恢复成功,我的另外一个文件恢复出来是乱码,还有一个文件报错找不到inode.



<think>好的,用户的问题是如何恢复ext4分区上使用rm命令误删除文件,需要方法和工具。首先,我得回忆一下之前提供的引用内容。引用1、2、3、4都提到了extundelete这个工具,还有如何挂载分区为只读,以及处理进程占用的问题。用户可能已经知道了一些基本步骤,但需要详细的指导。 首先,确认用户的需求:他们误删了文件,用的是ext4文件系统,想用工具恢复。根据引用1,extundelete支持按文件、目录、inode或全部恢复,所以这是主要的工具推荐。另外,引用2提到需要卸载分区,但如果是系统分区,可能需要用Live CD或U盘启动,这点也要注意。引用3提到如果无法卸载,使用fuser来终止进程,这也需要包括在步骤中。 接下来,我需要整理一个清晰的步骤指南。第一步是停止写入数据,防止覆盖,这点很重要。然后卸载分区,如果无法卸载,用fuser查找并终止进程。接着安装extundelete,可能需要添加epel源,尤其是CentOS/RHEL系统。然后使用挂载为只读或使用Live环境,用户可能需要用U盘进入系统,如引用2所述。 然后,具体的恢复命令,比如恢复单个文件、目录、inode或全部。需要给出示例命令,如引用1中的例子。还要提醒用户恢复文件存放在哪里,通常是当前目录下的RECOVERED_FILES。 可能还需要注意事项,比如成功率依赖分区是否被覆盖,及时操作的重要性,以及系统分区的处理需要Live环境。这些信息在引用中都有提到,需要综合起来。 用户可能的问题还包括其他工具,比如testdisk,但根据引用,extundelete是主要工具。此外,是否需要root权限,步骤中的命令是否需要sudo,这些都要说明清楚。 最后,生成相关问题,比如成功率、替代工具、系统分区恢复的特殊步骤,以及如何防止误删。这些问题可以帮助用户进一步了解相关主题,并预防未来的问题。 检查是否所有引用内容都被合理利用,比如umount、fuser、extundelete的各个参数,确保步骤正确无误。同时用中文清晰表达,符合用户的要求。</think>### 如何恢复ext4分区中被`rm`命令删除的文件 #### 一、核心恢复原理 ext4文件系统删除文件时仅清除inode索引信息,实际数据仍保留在磁盘上,直到被新数据覆盖。恢复需满足两个条件: 1. 分区未执行`TRIM`操作或覆盖写入 2. 及时停止对分区的写操作 #### 二、详细操作步骤 1. **立即停止写入操作** - 若删除发生在非系统分区(如`/data`): ```bash sudo umount /dev/sdXN # X为磁盘标识,N为分区号 ``` - 若删除发生在系统分区(如`/`): - 使用Live USB启动系统[^2] - 或强制挂载为只读模式: ```bash sudo mount -n -o remount,ro /dev/sdXN ``` 2. **检查进程占用(针对无法卸载的分区)** ```bash sudo fuser -m -v /dev/sdXN # 查看占用进程 sudo fuser -k -m -v /dev/sdXN # 终止占用进程 ``` 3. **安装恢复工具** ```bash # CentOS/RHEL sudo yum install epel-release sudo yum install extundelete # Ubuntu/Debian sudo apt-get install extundelete ``` 4. **执行文件恢复** | 恢复类型 | 命令示例 | 恢复结果位置 | |----------------|---------------------------------------------|---------------------| | 单个文件 | `extundelete /dev/sdXN --restore-file 路径` | `RECOVERED_FILES/` | | 整个目录 | `extundelete /dev/sdXN --restore-directory 路径` | 同上 | | 按inode号 | `extundelete /dev/sdXN --restore-inode 12` | 同上 | | 全盘扫描恢复 | `extundelete /dev/sdXN --restore-all` | 同上 | #### 三、关键注意事项 1. **成功率影响因素**: - 文件删除后经过的时间越长,恢复成功率越低 - 系统日志级别设置(建议保持默认`journal`模式) - 大文件恢复可能不完整 2. **系统分区特殊处理**: - 必须通过Live环境操作(如Ubuntu安装U盘)[^2] - 推荐挂载目标分区时添加`noload`参数: ```bash sudo mount -o ro,noload /dev/sdXN /mnt ``` 3. **替代工具方案**: ```bash # 使用testdisk进行深度扫描 sudo testdisk /dev/sdXN # 选择Advanced -> Undelete ``` #### 四、预防措施建议 1. 设置`rm`别名保护: ```bash alias rm='trash-put' # 需要先安装trash-cli ``` 2. 定期备份关键数据 3. 对重要分区启用文件版本控制: ```bash sudo btrfs subvolume create /data/.snapshots ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值