Linux不像windows有那么显眼的回收站,不是简单的还原就可以了。
linux删除文件还原可以分为两种情况,一种是删除以后在进程存在删除信息,一种是删除以后进程都找不到,只有借助于工具还原。这里分别检查介绍下

一,误删除文件进程还在的情况。
这种一般是有活动的进程存在持续标准输入或输出,到时文件被删除后,进程PID还是存在。这也就是有些服务器删除一些文件但是磁盘不释放的原因。比如当前举例说明:
通过一个shell终端对一个测试文件做cat追加操作:

  1. [root@21yunwei_backup ~]# echo "hello py" > testdelete.py
  2. [root@21yunwei_backup ~]# cat >> testdelete.py
  3. hello delete

另外一个终端查看这个文件可以清楚看到内容:

  1. [root@21yunwei_backup ~]# cat testdelete.py
  2. hello py
  3. hello delete

此时,在当前服务器删除文件rm -f ./testdelete.py
命令查看这个目录,文件已经不存在了,那么现在我们将其恢复出来。
1,lsof查看删除的文件进程是否还存在。这里用到一个命令lsof,如没有安装请自行yum或者apt-get。类似这种情况,我们可以先lsof查看删除的文件 是否还在:

  1. [root@21yunwei_backup ~]# lsof | grep deleted
  2. mysqld 1512 mysql 5u REG 252,3 0 6312397 /tmp/ibzW3Lot (deleted)
  3. cat 20464 root 1w REG 252,3 23 1310722 /root/testdelete.py (deleted)

幸运的是这种情况进程还存在 ,那么开始进行恢复 操作。
2,恢复。
恢复命令:

  1. cp /proc/pid/fd/1 /指定目录/文件名

进入 进程目录,一般是进入/proc/pid/fd/,针对当前情况:

  1. [root@21yunwei_backup ~]# cd /proc/20464/fd
  2. [root@21yunwei_backup fd]# ll
  3. total 0
  4. lrwx------ 1 root root 64 Nov 15 18:12 0 > /dev/pts/1
  5. l-wx------ 1 root root 64 Nov 15 18:12 1 > /root/testdelete.py (deleted)
  6. lrwx------ 1 root root 64 Nov 15 18:12 2 > /dev/pts/1

恢复操作:

  1. cp 1 /tmp/testdelete.py

查看文件:

  1. [root@21yunwei_backup fd]# cat /tmp/testdelete.py
  2. hello py
  3. hello delete

恢复完成。

二,误删除的文件进程已经不存在,借助于工具还原。
创建准备删除的目录并echo一个 带有内容的文件:

  1. [root@21yunwei_backup 21yunwei]# tree
  2. .
  3. ├── deletetest
  4. │   └── mail
  5. │   └── test.py
  6. ├── lost+found
  7. └── passwd
  8. 3 directories, 2 files
  9. [root@21yunwei_backup 21yunwei]# cat /21yunwei/deletetest/mail/test.py
  10. hello Dj
  11. [root@21yunwei_backup 21yunwei]# tail -2 passwd
  12. haproxy:x:500:502::/home/haproxy:/bin/bash
  13. tcpdump:x:72:72::/:/sbin/nologin

执行删除操作:

  1. [root@21yunwei_backup 21yunwei]# rm -rf ./*
  2. [root@21yunwei_backup 21yunwei]# ll
  3. total 0

现在开始进行误删除文件的恢复。这种情况一般是没有守护进行或者后台进程对其持续输入,所以删除就删除 了,lsof也看不到。就要借助于工具。这里我们采用的工具是extundelete第三方工具。恢复步骤如下:

1,停止对当前分区做任何操作,防止inode被覆盖。inode被覆盖基本就告别自行车了。比如停止所在分区的服务,卸载目录所在的设备,有必要的情况下都可以断网。
2,通过dd命令对 当前分区进行备份,防止第三方软件恢复失败导致数据丢失。适合数据非常重要的情况,这里测试,就没有备份,如备份可以考虑如下方式:
dd if=/path/filename of=/dev/vdc1
3,通过umount命令,对当前设备分区卸载。或者fuser 命令。
umount /dev/vdb1 或者 umount /21yunwei
如果提示设备busy,可以用fuser命令强制卸载:fuser -m -v -i -k /21yunwei
4,下载第三方工具extundelete安装,搜索误删除的文件进行还原。

  1. wget http://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2
  2. tar jxvf extundelete-0.2.4.tar.bz2
  3. cd extundelete-0.2.4
  4. ./configure
  5. make
  6. make install

扫描误删除的文件:

  1. [root@21yunwei_backup extundelete-0.2.4]# extundelete --inode 2 /dev/vdb1
  2. NOTICE: Extended attributes are not restored.
  3. Loading filesystem metadata ... 8 groups loaded.
  4. Group: 0
  5. Contents of inode 2:
  6. .
  7. .省略N行
  8. File name | Inode number | Deleted status
  9. . 2
  10. .. 2
  11. lost+found 11 Deleted
  12. deletetest 12 Deleted
  13. passwd 14 Deleted

通过扫描发现了我们删除的文件夹,现在执行恢复操作。
(1)恢复单一文件passwd

  1. [root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-file passwd
  2. NOTICE: Extended attributes are not restored.
  3. Loading filesystem metadata ... 8 groups loaded.
  4. Loading journal descriptors ... 46 descriptors loaded.
  5. Successfully restored file passwd

恢复文件是放到了当前目录RECOVERED_FILES。
查看恢复的文件:

  1. [root@21yunwei_backup /]# tail -5 RECOVERED_FILES/passwd
  2. mysql:x:497:500::/home/mysql:/bin/false
  3. nginx:x:496:501::/home/nginx:/sbin/nologin
  4. zabbix:x:495:497:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
  5. haproxy:x:500:502::/home/haproxy:/bin/bash
  6. tcpdump:x:72:72::/:/sbin/nologin

(2)恢复目录deletetest

  1. [root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-directory deletetest
  2. NOTICE: Extended attributes are not restored.
  3. Loading filesystem metadata ... 8 groups loaded.
  4. Loading journal descriptors ... 46 descriptors loaded.
  5. Searching for recoverable inodes in directory deletetest ...
  6. 5 recoverable inodes found.
  7. Looking through the directory structure for deleted files ...
  8. [root@21yunwei_backup /]# cat RECOVERED_FILES/deletetest/mail/test.py
  9. hello Dj

(3)恢复所有

  1. [root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-all
  2. NOTICE: Extended attributes are not restored.
  3. Loading filesystem metadata ... 8 groups loaded.
  4. Loading journal descriptors ... 46 descriptors loaded.
  5. Searching for recoverable inodes in directory / ...
  6. 5 recoverable inodes found.
  7. Looking through the directory structure for deleted files ...
  8. 0 recoverable inodes still lost.
  9. [root@21yunwei_backup /]# cd RECOVERED_FILES/
  10. [root@21yunwei_backup RECOVERED_FILES]# tree
  11. .
  12. ├── deletetest
  13. │   └── mail
  14. │   └── test.py
  15. └── passwd
  16. 2 directories, 2 files

(4),恢复指定inode。

  1. [root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-inode 14
  2. NOTICE: Extended attributes are not restored.
  3. Loading filesystem metadata ... 8 groups loaded.
  4. Loading journal descriptors ... 46 descriptors loaded.
  5. [root@21yunwei_backup /]# tail -5 /RECOVERED_FILES/file.14
  6. mysql:x:497:500::/home/mysql:/bin/false
  7. nginx:x:496:501::/home/nginx:/sbin/nologin
  8. zabbix:x:495:497:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
  9. haproxy:x:500:502::/home/haproxy:/bin/bash
  10. tcpdump:x:72:72::/:/sbin/nologin

注意恢复inode的时候,恢复 出来的文件名和之前不一样,需要单独进行改名。内容是没问题的。

更多的extundelete用法请参考extundelete –help选项参数说明,当前恢复所有的操作完成。

作者:21运维

链接:http://www.21yunwei.com/archives/6030

Linuxt通过命令lsof或者extundelete工具恢复误删除的文件或者目录的更多相关文章

  1. centOS6.4 extundelete工具恢复rm -rf 删除的目录

    PS:补充下,我在fedora 19上运行的时候遇到的一个问题: [root@localhost extundelete-]# ./configure Configuring extundelete ...

  2. centOS6.4 extundelete工具恢复rm -rf 删除的目录[转]

    原文:http://www.cnblogs.com/patf/p/3368765.html PS:补充下,我在fedora 19上运行的时候遇到的一个问题: 1 [root@localhost ext ...

  3. Linux恢复误删除的文件或者目录(转)

    linux不像windows有个回收站,使用rm -rf *基本上文件是找不回来的. 那么问题来了: 对于linux下误删的文件,我们是否真的无法通过软件进行恢复呢? 答案当然是否定的,对于误删的文件 ...

  4. 如何在Linux上恢复误删除的文件或目录

    Linux不像windows有那么显眼的回收站,不是简单的还原就可以了.linux删除文件还原可以分为两种情况,一种是删除以后在进程存在删除信息,一种是删除以后进程都找不到,只有借助于工具还原,这里分 ...

  5. Linux恢复误删除的文件或者目录

    文章转载自:https://www.jianshu.com/p/662293f12a47 linux不像windows有个回收站,使用rm -rf *基本上文件是找不回来的. 那么问题来了: 对于li ...

  6. 高性能Linux服务器 第6章 ext3文件系统反删除利器ext3grep extundelete工具恢复rm -rf 误删除的文件

    高性能Linux服务器 第6章  ext3文件系统反删除利器ext3grep  extundelete工具恢复rm -rf 误删除的文件 只能用于ext3文件系统!!!!!!!高俊峰(高性能Linux ...

  7. linux下恢复误删除的文件方法(ext2及ext3)

     linux下恢复误删除的文件方法(ext2及ext3) 2009-12-19 15:23:47 分类: LINUX 如果是ext2文件系统的,直接用debugfs是可以恢复出来的,但对于ext3,d ...

  8. Linux下面误删除文件使用extundelete工具恢复介绍

    操作系统版本:CentOS release 6.4 (Final)      软件版本:extundelete-0.2.4.tar.bz2 PS:该软件恢复文件系统仅支持ext2/ext3/ext4 ...

  9. Linux下恢复误删除的文件

    原文地址:http://www.libenfu.com/vim-分区下误删的文件,恢复文件全记录-转 当时我的工作目录是/source/needrecovered. $ pwd /source/nee ...

  10. Linux命令(20)linux服务器之间复制文件和目录

    linux的scp命令: scp就是secure copy的简写,用于在linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器. 有时我们需要获得远程服务器上 ...

随机推荐

  1. Vue导出模板、使用前端js办法导出表格数据、导入表格前端读取表格数据、导入表格发送后端读取数据

    以下是几种用的较多的函数方法,可以参考使用. // 導出1 myExport() { // post請求文件寫法1 const url = 'http://XXXX/XXXX/XXXX/XXXX' c ...

  2. Kubernetes(k8s)配置文件管理:ConfigMap

    目录 一.系统环境 二.前言 三.ConfigMap概览 四.创建ConfigMap 五.ConfigMap的使用 5.1 以环境变量的方式使用ConfigMap 5.2 以卷的方式使用ConfigM ...

  3. 11月30日内容总结——前端简介、http协议概念、html协议概念及基础知识和部分标签的讲解

    目录 一.前端与后端的概念 什么是前端开发? 什么是后端? 学习前端的目的 前端三剑客 二.前端前戏 三.HTTP协议 1.四大特性 2.报文格式 3.响应状态码 四.HTML概览 1.HTML简介 ...

  4. v-html渲染页面的时候 css样式无效

    感谢: https://www.cnblogs.com/niuxiaoxian/p/9443873.html 当我们用v-html渲染页面的时候会发现样式并没有添加上,如下 复制代码 <temp ...

  5. 嵌入式Linux—Framebuffer应用编程

    Framebuffer 应用编程 Frame的意思是帧,buffer的意思是缓冲区.Framebuffer就是一块内存(硬件设备),里面保存着一帧图像. ioctl()函数解析 ioctl()函数非常 ...

  6. ASP.NET Core - .NET 6 的入口文件

    自从.NET 6 开始,微软对应用的入口文件进行了调整,移除了 Main 方法和 Startup 文件,使用顶级语句的写法,将应用初始化的相关配置和操作全部集中在 Program.cs 文件中,如下: ...

  7. 一个诡异的 Pulsar InterruptedException 异常

    背景 今天收到业务团队反馈线上有个应用往 Pulsar 中发送消息失败了,经过日志查看得知是发送消息时候抛出了 java.lang.InterruptedException 异常. 和业务沟通后得知是 ...

  8. linux nginx mysql php LNMP一键安装包

    官网: LNMP一键安装包 - CentOS/RadHat/Debian/Ubuntu下自动编译安装Nginx,PHP,MySQL,PHPMyAdmin 安装方法: centos 7.2安装 lnmp ...

  9. liunx Ubuntu20.04怎么安装mysql

    转载csdn:优秀的处女座 https://blog.csdn.net/qq_42481794/article/details/113923803

  10. XShell 连接 VMware Linux

    如果你的系统是 Ubuntu,并且是精简安装的系统,极大可能使用ifconfig时会提示你安装相关依赖,所以你安装的时候要记得顺带安装 ifconfig: sudo apt install ifcon ...