Background


Today I did stupid things that I went into the ~/Downloads/ and pressed [Alt] + [A] then [Shift] + [Delete]. Wtf... I didn't want to delete this folder but another sub-folder...... So no zuo no die : )


Theory


  • Generally, Linux filesystem mainly contains inode and blocks. inode is the index of file or directory; blocks stores the actual data.

  • Usually, our rm or [Shift] + [Delete] in GUI just modify the inode but do not rewrite the blocks in which your data has ever be (If you use some special professional softwares to delete file, good luck : ) ).

  • So if our file is just there as it was before if we take measures in time. We can't use filename or inode to fetch it, but there are other probably ways to make it.

  • EMPTY. I want to add more filesystem knowledge here later. Remind me of it, OK ?

P.S. The filesystem is very interesting. You can regard it as local Domain Name System to some extent. And the filename is something like the domain name, while inode is something like IP address.


Recovery


Environment: Linux (I tried on Ubuntu 14.04 & CentOS 6.0)
Filesystem: Ext4 [1]
Of course, you'd better be root and everything will be easier.

0x0 Forbid other users and processes to Modify the filesystem

~If no speical prompts, you'd better follow this part to protect your data.
~Now we test on /dev/sda8 mounting at /tmp. sth in /tmp may be erased after mounting or unmounting, but sth in /tmp/lost+found may not.
~I use cat /proc/meminfo > /tmp/lost+found/tet to create a test file and delete it.
~fs means filesystem.
~sth means something.

For the test, I md5sum /tmp/lost+found/tet before deleting it

Before unmounting, use ls -id /tmp and take down the inode number

  1. use df or fdisk -l to determine the fs to be unmounted
  2. unmount it: umount /dev/sda8

  3. mount the fs with read-only state: mount -r -n /dev/sda8 And you can have a try to write sth and find failed.

P.S.
~When I man umount I found umount /dev/sda8 is not recommended and it's better to use umount /tmp (directory name). And after I mount -r -n /dev/sda8, I failed to use umount /dev/sda8 to unmount it.
~If the fs is busy and you can't unmount it. You can use fuser -v -m /dev/sda8 to find the processes using it and kill them. What's more, you can use fuser -k -v -m /dev/sda8 to kill them automatically.


Here I will give some methods I have tried(the title is the main tool to be used).

0x1 extundelete

  • yum install e2fsprogs e2fsprogs-libs e2fsprogs-devel
  • wget http://tenet.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2
    If you didn't install g++ like me, yum install gcc-c++
  • ./configure
  • make && make install
  • extundelete /dev/sda8 --inode 2 to check

  • extundelete /dev/sda8 --restore-directory lost+found
    here I use --restore-directory, you can also use --restore-file RELATIVE-PATH-OF-FILE
  • md5sum ./RECOVERD_FILES/lost+found/tet

Bingo~

P.S.
You can also use extundelete /dev/sda8 --restore-all simply.

0x02 debugfs & dd

...

0x03 testdisk

...


Finally


Some advice:

  • Backup is very very important
  • Disk should be parted into filesystems (/,/home,/boot,/var,/usr,swap,...)
  • alias rm="rm -i"

Quotations


I learnt knowledge from articles below. Thanks for the authors' sharing.


Appendix


[1] How to determine your filesystem?

  1. fdisk -l # to get the name of disk. E.g. /dev/sda5
    You can also use df and this tool can show the relationship between fs and your directories. However, this tool can only deal with fs being mounted.
  2. file -s /dev/sda5

Linux File Recovery Study的更多相关文章

  1. chattr lsattr linux file system attributes - linux 文件系统扩展属性

    我们使用 linux 文件系统扩展属性,能够对linux文件系统进行进一步保护:从而给文件 赋予一些额外的限制:在有些情况下,能够对我们的系统提供保护: chattr命令用来改变文件属性.这项指令可改 ...

  2. Windows File Recovery - 微软官方文件恢复工具

    假如你不小心误删除了文件或因各种意外情况丢失数据后,你可以通过 微软这款工具 这个工具来尝试恢复它们.WinFR 工具支持读取本机硬盘.移动硬盘.U 盘,或者连接相机.手机.使用读卡器来恢复 SD.T ...

  3. Linux— file命令 用于辨识文件类型

    Linux file命令用于辨识文件类型. 通过file指令,我们得以辨识该文件的类型. 语法 file [-bcLvz][-f <名称文件>][-m <魔法数字文件>...] ...

  4. 磁盘、分区及Linux文件系统 [Disk, Partition, Linux File System]

    1.磁盘基础知识 1.1 物理结构 硬盘的物理结构一般由磁头与碟片.电动机.主控芯片与排线等部件组成:当主电动机带动碟片旋转时,副电动机带动一组(磁头)到相对应的碟片上并确定读取正面还是反面的碟面,磁 ...

  5. Linux File System Change Monitoring Technology、Notifier Technology

    catalog . 为什么要监控文件系统 : hotplug . udev . fanotify(fscking all notification system) . inotify . code e ...

  6. Linux File System

    目录 . Linux文件系统简介 . 通用文件模型 . VFS相关数据结构 . 处理VFS对象 . 标准函数 1. Linux文件系统简介 Linux系统由数以万计的文件组成,其数据存储在硬盘或者其他 ...

  7. Linux File、File Directory IO Operation Summary(undone)

    目录 . 引言 . Linux下文件操作API . Linux下文件目录操作API . Linux下的其他设备操作API 1. 引言 Linux支持多种文件系统,如ext.ext2.minix.iso ...

  8. linux file命令小记

    在linux中,所有东西都是文件:而且他并没有后缀名这一概念.Linux的扩展名没有太大的意义, file.tar.gz file.tgz file.tar.bz2 file.rar file.gz ...

  9. linux File Handling commands 'ls'.

    ref:Linux / Unix Command: ls NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DES ...

随机推荐

  1. clientTarget与用户代理别名

    将特定用户代理的别名添加到用户代理别名的内部集合中. 来自 <https://msdn.microsoft.com/zh-cn/library/6379d90d(v=vs.110).aspx&g ...

  2. Swift - 轮播图

    学写swift, 试着弄了一个轮播图, 仿照 HHBannerView的OC代码 Demo地址: https://github.com/liguoliangiOS/ZJGenWoYou.git 一.第 ...

  3. json-lib的使用《二》

    上篇文章主要集中在了使用json-lib来实现JSON字符串和java中的对象的互转上,忽视了json-lib本身的功能,json-lib中有两个类比较重要:JSONObject和JSONArray, ...

  4. SQL复制表结构或表数据

    需求: 软件开发过程中,一般会部署两个数据库:一个测试数据库提供给开发和测试过程使用:一个运维数据库提供上线使用.当需求变化需增加表时,会遇到数据库表结构或表数据同步的问题,这时就要复制表结构或表数据 ...

  5. JQUERY 实现加入收藏夹功能

    关于"加入收藏"的代码,很多人都不会重视,一般情况是随便在网上搜一个代码放在页面里就草草了事了.可是都没有做到主流浏览器的兼容.下面分享一段使用 jQuery 实现加入收藏夹的功能 ...

  6. Windows8.1系统下让VS2012编译运行IIS Express 64位 调试器

    有时候在window8以上系统中对C#系统进行调试 提示不能使用32位IIS Express,如果每次都采用IIS 在vs代码进行调试的时候很麻烦 下面我们就介绍一下怎么用编译调试64位代码: 网上方 ...

  7. 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(二)鹰眼模块

    讲解GIS功能模块实现之前,先大概说一下flexviewer的核心配置文件config.xml,系统额GIS功能widget菜单布局.系统的样式.地图资源等等都是在这里配置的,这里对flexviewe ...

  8. IOS开发基础知识--碎片12

    1:Delegate运用 .h #import <UIKit/UIKit.h> @protocol FilterHeaderViewDelegate <NSObject> @r ...

  9. Project、Target、Workspace and Scheme

    前言 今天有人问我Target和Project是什么关系?额...学习iOS开发都知道Target和Project的关系.这里我就简单的做了一个总结,而且加入的Scheme和Workspace.如果不 ...

  10. IOS开发之学习《AV Foundation 开发秘籍》

    敲了这么久的代码,查阅了很多资料,都是网络电子版的,而且时间久了眼睛也累了,还不如看一下纸质的书籍,让眼睛休息休息. 本篇开始学习<AV Foundation 开发秘籍>,并记录对自己本人 ...