linux恢复 rm -rf * 误删数据


linux误删除文件后应尽快将磁盘挂载为只读,否则后续操作可能会覆盖这些数据,越早发现,并挂载为只读,恢复的成功机率就越大。因此,本文只是提供一种方式,不能保证一定能恢复误删的数据。

一、磁盘分区挂载为只读

1、查看被删除文件位于哪个分区

[root@localhost ~]# mount
/dev/sda3 on / type ext4 (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/sdb1 on /opt type ext4 (rw)

2、将对应目录重新挂载为只读

[root@localhost  ~]#  mount -r -n -o remount /opt
mount: /home is busy
如果显示 xxx is busy
[root@localhost  ~ ]# fuser -v -m /opt

找出相关进程,kill掉

[root@localhost  ~ ] #  mount -r -n -o remount /opt

成功后,此时在/opt目录 touch文件时,会报错:

[root@localhost  opt ] # touch txt
touch: cannot touch `txt’: Read-only file system

二、安装数据恢复工具 extundelete

1、下载extundelete-0.2.4

下载源:
http://superb-dca2.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2

2、安装

需要安装编译环境及extundelete依赖的包
yum -y install gcc+ gcc-c++
yum install e2fsprogs e2fsprogs-libs e2fsprogs-devel -y

[root@localhost extundelete-0.2.4]# ./configure 
Configuring extundelete 0.2.4
Writing generated files to disk
[root@localhost extundelete-0.2.4]# make
make -s all-recursive
Making all in src
[root@localhost extundelete-0.2.4]# make install
Making install in src
  /usr/bin/install -c extundelete '/usr/local/bin'
[root@localhost extundelete-0.2.4]# cd src/     --验证
[root@localhost src]# ./extundelete -v
extundelete version 0.2.4
libext2fs version 1.41.12
Processor is little endian.
[root@localhost src]# 

到此安装完成!

三、恢复

1、模拟删除文件,设置只读

[root@localhost src]# cd /opt/
[root@localhost opt]# ls
lost+found  test
[root@localhost opt]# cd test/
[root@localhost test]# ls
1.txt  extundelete-0.2.4.tar.bz2
[root@localhost test]# more 1.txt 
ceshi
test
qazwsx
edcrfv
tgbyhn
ujmik,
[root@localhost test]# cd ..
[root@localhost opt]# rm -rf test/
[root@localhost opt]# cd ..
[root@localhost /]# mount -r -n -o remount /opt

2、查看被删除的文件

[root@localhost /]# extundelete /dev/sdb1 --inode 2
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 400 groups loaded.
Group: 0
Contents of inode 2:
Inode is Allocated
File mode: 16877
Low 16 bits of Owner Uid: 0
Size in bytes: 4096
Access time: 1553670589
Creation time: 1553672852
Modification time: 1553672852
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
test                                              1835009        Deleted

一个分区挂载到一个目录下时,”根”目录的inode值为一般是2。

状态为deleted的是被删除的文件,可以看到模拟删除的文件test

3、恢复

确定删除的大概时间,可以较快的恢复误删的数据

[root@localhost /]# date -d "Wed Mar 26 15:40:00 CST 2019" +%s
1553586000

执行恢复命令(在另外的磁盘分区执行恢复)

[root@localhost /]# /extundelete-0.2.4/src/extundelete /dev/sdb1 --after 1553586000 --restore-all
Only show and process deleted entries if they are deleted on or after 1553586000 and before 9223372036854775807.
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 400 groups loaded.
Loading journal descriptors ... 78 descriptors loaded.
Searching for recoverable inodes in directory / ... 
5 recoverable inodes found.
Looking through the directory structure for deleted files ... 
1 recoverable inodes still lost.

恢复完成,验证

[root@localhost /]# cd RECOVERED_FILES/
[root@localhost RECOVERED_FILES]# ls
test
[root@localhost RECOVERED_FILES]# cd test/
[root@localhost test]# ls
1.txt  extundelete-0.2.4.tar.bz2
[root@localhost test]# more 1.txt 
ceshi
test
qazwsx
edcrfv
tgbyhn
ujmik,
[root@localhost test]# 

可以看到删除的文件已经恢复。

4、重新挂载分区为读写

[root@localhost test]#mount -rw  -o remount /opt

可以对rm启用别名,防止无交互删除

vi /etc/bashrc

#do not delete / or prompt if deleting more than 3 files at a time #
alias rm=‘rm -I --preserve-root’

source /etc/bashrc

ext3grep、debugfs也可以恢复误删文件,还需再研究

附:extundelete相关参数

[root@localhost test]# /extundelete-0.2.4/src/extundelete 
No action specified; implying --superblock.
/extundelete-0.2.4/src/extundelete: Missing device name.
Usage: /extundelete-0.2.4/src/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 ./RECOVERED_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 '/'
                         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-directory 'path'
                         Will restore directory 'path'. 'path' is relative to the
                         root directory of the file system.  The restored
                         directory is created in the output directory as 'path'.
  --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.
  --log 0                Make the program silent.
  --log filename         Logs all messages to filename.
--log D1=0,D2=filename   Custom control of log messages with comma-separated
   Examples below:       list of options.  Dn must be one of info, warn, or
   --log info,error      error.  Omission of the '=name' results in messages
   --log warn=0          with the specified level to be logged to the console.
   --log error=filename  If the parameter is '=0', logging for the specified
                         level will be turned off.  If the parameter is
                         '=filename', messages with that level will be written
                         to filename.
   -o directory          Save the recovered files to the named directory.
                         The restored files are created in a directory
                         named 'RECOVERED_FILES/' by default.

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 如果在Linux中执行了rm -rf命令误删文件,可以尝试使用一些工具来找回文件。以下是一些常用的工具: 1. extundelete:这是一个免费的工具,可以用于恢复ext2、ext3和ext4文件系统中的文件。使用该工具需要在文件系统上运行,因此需要使用Live CD或USB。 2. TestDisk:这是一个免费的工具,可以用于恢复各种文件系统中的文件,包括ext2、ext3、ext4、FAT、NTFS等。使用该工具需要在文件系统上运行,因此需要使用Live CD或USB。 3. Photorec:这是TestDisk的一个组件,可以用于恢复各种文件格式的文件,包括照片、视频、文档等。使用该工具需要在文件系统上运行,因此需要使用Live CD或USB。 4. Scalpel:这是一个免费的工具,可以用于恢复各种文件格式的文件,包括照片、视频、文档等。使用该工具需要在文件系统上运行,因此需要使用Live CD或USB。 需要注意的是,如果误删文件已经被覆盖,那么就无法恢复了。因此,尽可能在误删后立即停止使用该文件系统,并尝试使用上述工具进行恢复。 ### 回答2: 在Linux系统中执行rm -rf命令会删除指定目录下所有文件和子目录,这些文件和目录都会被彻底删除,无法恢复。因此,如果执行rm -rf误删了重要文件或目录,需要采取措施进行数据恢复。 以下是一些可用于恢复误删文件和目录的方法: 1. 使用trash-cli命令。该命令需要提前安装,可以将误删文件和目录以及它们的元数据存储在垃圾桶中,从而可以通过命令行轻松地恢复它们。 2. 使用extundelete命令。该命令是一个开源工具,可以恢复在ext3、ext4文件系统下误删文件,也可以恢复已经清空回收站的文件,但是要求在误删后尽快运行。 3. 使用TestDisk命令。该命令是一款数据恢复工具,可以恢复已经被删除、格式化、或分区表出错的分区和文件系统。 4. 使用Photorec命令。该命令也是一款数据恢复工具,与TestDisk命令功能类似,但是更加强大,可以恢复多种文件类型。 需要注意的是,以上方法均不能保证100%地恢复所有的文件和目录,还要依据具体情况进行判断和操作,同时在误删后尽快采取恢复措施,以免被系统重写覆盖,导致无法恢复。因此,在执行rm -rf命令前,应该在确认文件和目录的时候,避免误删,避免造成不必要的后果。 ### 回答3: 误删是指在操作电脑、服务器等过程中,不小心将重要的文件文件夹进行删除操作,而这个删除操作可能会导致数据的丢失,导致后续的工作无法进行。Linux系统中执行rm -rf命令时,若不加文件路径,会直接删除整个目录及其子目录内容,如果不小心误删了,我们可以通过以下几种方法进行找回: 1. 使用数据恢复工具 可以使用Linux下的各种恢复工具,比如extundelete、testdisk等恢复工具来查找误删文件,并尝试将其恢复。 2. 使用备份 在误删之前及时做好数据备份,可以从备份中恢复误删文件。由于备份的及时性和完整性,能够最大限度保证数据的安全。 3. 使用数据恢复公司的服务 如果数据很重要且不能自己处理,可以考虑寻求数据恢复公司的服务。比如Kroll Ontrack、Stellar数据恢复公司等都可以提供误删数据恢复服务。 总之,要避免误删,建议在执行删除操作时,尽量确认删除的文件文件夹是正确的,可以在删除前备份数据,以免错删重要数据。同时,平时要多加注意,不要乱删除系统文件,以免造成不必要的麻烦和损失。 以上方法可能并不是100%保证能将误删数据全部恢复,但身为一个合格的程序员,随时备份使用是规范中的一部分。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值