linux恢复 rm -rf * 误删数据

linux恢复 rm -rf * 误删数据

    • 一、磁盘分区挂载为只读
      • 1、查看被删除文件位于哪个分区
      • 2、将对应目录重新挂载为只读
    • 二、安装数据恢复工具 extundelete
      • 1、下载extundelete-0.2.4
      • 2、安装
    • 三、恢复
      • 1、模拟删除文件,设置只读
      • 2、查看被删除的文件
      • 3、恢复
      • 4、重新挂载分区为读写

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 /data

找出相关进程,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.

你可能感兴趣的:(linux)