Loading

40、Linux文件误删除恢复操作


rm -rf / #此方法删除不了/目录;

rm -rf /* #此方法可以删除/目录下的所有内容,禁止使用;

40.1、前言:

作为一个多用户、多任务的操作系统,Linux下的文件一旦被删除,是难以恢复的。尽管删除命令只是在文件节点中作删除标记,

并不真正清除文件内容,但是其他用户和一些有写盘动作的进程会很快覆盖这些数据。不过,对于家庭单机使用的Linux,或者误删

文件后及时补救,还是可以恢复的。

40.2、文件误删除情况一:

当前系统有多用户,其中一个用户对文件进行修改,另一个用户对文件进行删除操作,此时该文件

的进程id还是存在的通过以下命令进行恢复;

1、窗口一:

[root@nfs01 ~]# cd /tmp

[root@nfs01 tmp]# echo 'test' >test.txt

[root@nfs01 tmp]# cat test.txt

test

[root@nfs01 tmp]# cat >>test.txt

1

2

3


2、窗口二:

[root@nfs01 ~]# rm -vf /tmp/test.txt

已删除"/tmp/test.txt"

[root@nfs01 ~]# ls /tmp

[root@nfs01 ~]#

[root@nfs01 ~]# lsof | grep -i "delete"

cat 4715 root 1w REG 8,2 7 131075 /tmp/test.txt (deleted)

正在使用的命令 pid进程号 root用户 打开的文件句柄 /tmp/test.txt文件是delete状态

[root@nfs01 ~]# ls /proc/4715/fd

0 1 2

[root@nfs01 ~]# cp -v /proc/4715/fd/1 /tmp/test.txt

"/proc/4715/fd/1" -> "/tmp/test.txt"

[root@nfs01 ~]# ls /tmp

test.txt

[root@nfs01 ~]# cat /tmp/test.txt

test

1

2

3

3、对于一个正在使用的vim编辑的文件的用户,文件被其他用户删除后,编辑的用户保存文件后,文件依旧存在;

40.3、文件误删除情况二:

1、当前系统只有一个用户在登录,对文件进行了误删除操作(如果有多个用户进入单用户模式);

2、当前的测试环境:

(1)使用fdfisk /dev/sdb命令创建一个/dev/sdb1主分区并挂载到 /test/目录下;

[root@nfs01 ~]# df -hT

Filesystem Type Size Used Avail Use% Mounted on

/dev/sda2 ext4 57G 5.4G 49G 10% /

tmpfs tmpfs 297M 0 297M 0% /dev/shm

/dev/sda1 ext4 190M 36M 145M 20% /boot

/dev/sdb1 ext4 2.0G 3.0M 1.9G 1% /test

(2)生成测试文件和目录:

[root@nfs01 ~]# cd /test/

[root@nfs01 test]# echo 'lc_2018/9/14' >lc_test.txt

[root@nfs01 test]# mkdir test

[root@nfs01 test]# echo 'mkdir test' >test/dir.txt

[root@nfs01 test]# tree

.

├── lc_test.txt

├── lost+found

└── test

└── dir.txt

2 directories, 2 files

(3)删除/test/目录中的内容:

[root@nfs01 test]# rm -rvf test/ lc_test.txt

已删除"test/dir.txt"

已删除目录:"test"

已删除"lc_test.txt"

[root@nfs01 test]# ls

lost+found

3、注意事项及解决思路:

(1)停止对当前分区进行任何操作,防止inode值被覆盖;

(2)通过dd命令对当前分区进行备份,防止通过第三方软件恢复失败,造成数据丢失;

[root@nfs01 test]# dd if=/dev/sdb1 of=/tmp/sdb1_test.bak

记录了4194303+0 的读入

记录了4194303+0 的写出

2147483136字节(2.1 GB)已复制,71.7973 秒,29.9 MB/秒

(3)对当前的分区进行卸载;

[root@nfs01 ~]# umount /dev/sdb1

[root@nfs01 ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda2 57G 7.4G 47G 14% /

tmpfs 297M 0 297M 0% /dev/shm

/dev/sda1 190M 36M 145M 20% /boot

(4)下载并安装第三方开源工具;

下载地址:http://zy-res.oss-cn-hangzhou.aliyuncs.com/server/extundelete-0.2.4.tar.bz2

[root@nfs01 tools]# rz -y

rz waiting to receive.

Starting zmodem transfer. Press Ctrl+C to cancel.

Transferring extundelete-0.2.4.tar.bz2...

100% 105 KB 105 KB/sec 00:00:01 0 Errors

[root@nfs01 tools]# tar -xjf extundelete-0.2.4.tar.bz2

[root@nfs01 tools]# cd extundelete-0.2.4

[root@nfs01 extundelete-0.2.4]# ./configure --prefix=/application/extundelete0.2.4

Configuring extundelete 0.2.4

configure: error: Can't find ext2fs library

[root@nfs01 extundelete-0.2.4]# yum install e2fsprogs-devel e2fsprogs -y

[root@nfs01 extundelete-0.2.4]# rpm -qa e2fsprogs-devel e2fsprogs

e2fsprogs-1.41.12-24.el6.x86_64

e2fsprogs-devel-1.41.12-24.el6.x86_64

[root@nfs01 extundelete-0.2.4]# ./configure --prefix=/application/extundelete0.2.4 #参数配置;

Configuring extundelete 0.2.4

Writing generated files to disk

[root@nfs01 extundelete-0.2.4]# make #编译;

make -s all-recursive

Making all in src

extundelete.cc:571: 警告:未使用的参数‘flags’ #忽略此错误

[root@nfs01 extundelete-0.2.4]# echo $?

0

[root@nfs01 extundelete-0.2.4]# make install #安装;

Making install in src

/usr/bin/install -c extundelete '/application/extundelete0.2.4/bin'

(5)数据恢复

[root@nfs01 extundelete-0.2.4]# cd /application/extundelete0.2.4/bin/

1)查询/dev/sdb1分区可恢复的数据信息,标记为deleted状态的是已经删除的文件或目录;

[root@nfs01 bin]# ./extundelete /dev/sdb1 --inode 2

........................................................................

File name | Inode number | Deleted status

. 2

.. 2

lost+found 11

lc_test.txt 12 Deleted

test 8193 Deleted

文件名 inode值 文件的状态

2)恢复单个文件:

[root@nfs01 bin]# ./extundelete /dev/sdb1 --restore-file lc_test.txt

NOTICE: Extended attributes are not restored.

Loading filesystem metadata ... 16 groups loaded.

Loading journal descriptors ... 47 descriptors loaded.

Successfully restored file lc_test.txt

#恢复的文件在当前目录下的“RECOVERED_FILES”目录中;

[root@nfs01 bin]# ls RECOVERED_FILES/

lc_test.txt

3)恢复单个目录:

[root@nfs01 bin]# ./extundelete /dev/sdb1 --restore-directory test

NOTICE: Extended attributes are not restored.

Loading filesystem metadata ... 16 groups loaded.

Loading journal descriptors ... 47 descriptors loaded.

Searching for recoverable inodes in directory test ...

3 recoverable inodes found.

Looking through the directory structure for deleted files ...

2 recoverable inodes still lost.

[root@nfs01 bin]# ls RECOVERED_FILES/

lc_test.txt test

[root@nfs01 bin]# ls RECOVERED_FILES/test/

dir.txt

4)恢复所有的文件和目录:

[root@nfs01 bin]# ./extundelete /dev/sdb1 --restore-all

NOTICE: Extended attributes are not restored.

Loading filesystem metadata ... 16 groups loaded.

Loading journal descriptors ... 47 descriptors loaded.

Searching for recoverable inodes in directory / ...

3 recoverable inodes found.

Looking through the directory structure for deleted files ...

0 recoverable inodes still lost.

如果重复恢复的话,不会覆盖之前恢复目录中的文件,会生成filename_v1....的形式进行

(6)挂载磁盘并拷贝数据:

[root@nfs01 bin]# mount /dev/sdb1 /test

[root@nfs01 bin]# mount

/dev/sdb1 on /test type ext4 (rw)

[root@nfs01 bin]# cp -av RECOVERED_FILES/* /test/

"RECOVERED_FILES/lc_test.txt" -> "/test/lc_test.txt"

"RECOVERED_FILES/test" -> "/test/test"

"RECOVERED_FILES/test/dir.txt" -> "/test/test/dir.txt"

[root@nfs01 bin]# ls /test

lc_test.txt lost+found test

40.4、内容补充:

1、lost+found:

几乎每个被格式化过的Linux分区都会有,意外后(非正常关机等)找回的文件一般在这里面。这个目录

是储存发生意外后丢失的文件的,只有root用户才能打开。如果删除了该文件夹,生成的命令是mklost+found;

2、fsck分区修复:

修复分区之前需要卸载分区;

(1)如果Linux分区有损坏情况,启动有问题,不能正常进入文本或图形界面。那么一般会出现提示,需要输入Root密码登录后采用

fsck -t ext4 /usr/local可以修复;

(3)fsck -t ext4 /dev/sdb 修复系统分区无法挂载的情况;

报错:mount: wrong fs type, bad option, bad superblock on /dev/mapper/oraclevg-oraclelv,

missing codepage or helper program, or other error

In some cases useful info is found in syslog - try

dmesg | tail or so

(3)fsck命令详解:

-t : 给定档案系统的型式,若在 /etc/fstab 中已有定义或 kernel 本身已支援的则不需加上此参数

-s : 依序一个一个地执行 fsck 的指令来检查

-A : 对/etc/fstab 中所有列出来的 partition 做检查

-C : 显示完整的检查进度

-d : 列印 e2fsck 的 debug 结果

-p : 同时有 -A 条件时,同时有多个 fsck 的检查一起执行

-R : 同时有 -A 条件时,省略 / 不检查

-V : 详细显示模式


posted @ 2020-02-15 21:06  云起时。  阅读(820)  评论(0编辑  收藏  举报