File recovery (简体中文) - ArchWiki

# File recovery (简体中文)

翻译状态： 本文是英文页面 File_recovery 的翻译，最后翻译时间：2014-08-2，点击这里可以查看翻译后英文页面的改动。

这篇文章列举了一系列的Arch Linux下恢复数据的选择.

## 特别提示

### 三思而后行

这篇文章主要的意图是教学. 如果你意外的删除或损坏了你的有价值且不可替代的数据并且毫无数据恢复的经验的话, 请立刻关闭你的电脑(只要长按电源键或直接拔插头即可; 不要使用系统的关机功能)然后寻求专业的帮助. 如果你在没有充分理解下述行为的情况下进行了任何一个操作都极有可能使你的情况更糟.

### 故障的磁盘

在数据恢复的领域里, 操作一个磁盘的镜像会比直接操作物理磁盘会更好些. 总体上来说, 一个坏掉的硬盘的情况会随着时间恶化. 我们的目标应当是首先尽快的抢救出尽可能多的数据并且抛弃故障的磁盘. ddrescue 与 dd_rescue 工具与 `dd` 不同, 他们会反复尝试从错误中恢复并且从头到尾从尾到头的读磁盘以抢救出数据. 他们不停的记录数据, 所以恢复过程中的暂停-恢复并不会损失进度.

参阅 Disk cloning.

由类如 ddrescue 这样的工具创建的镜像文件可以像物理设备一样被挂载并且安全的工作. 请务必做好原始镜像的备份以便在恢复工作黄掉的时候重新做人.

一种正确的久经考验的改进故障磁盘的读取工作的方法就是保持设备清凉. 在冰箱里放一会儿是个好主意, 不过在不要让冰冷的设备变热的太快, 因为这样会产生冷凝水. 如果能在进行恢复工作的时候把硬盘放在冰箱里并用线材连接电脑就再好不过了.

不要指望对故障磁盘的档案检查会管用, 这只会使情况变得更糟糕. 请务必把它挂载为只读.

### Backup flash media/small partitions

As an alternative to working with a 'live' partition (mounted or not), it is often preferable to work with an image, provided that the filesystem in question is not too large and that you have sufficient free HDD space to accommodate the image file. For example, flash memory devices like thumb drives, digital cameras, portable music players, cellular phones, etc. are likely to be small enough to image in many cases.

Be sure to read the man pages for the utilities listed below to verify that they are capable of working with image files.

To make an image, one can use `dd` as follows:

    # dd if=/dev/target_partition of=/home/user/partition.image

### Working with digital cameras

In order for some of the utilities listed in the next section to work with flash media, the device in question needs to be mounted as a block device (i.e., listed under /dev). Digital cameras operating in PTP (Picture Transfer Protocol) mode will not work in this regard. PTP cameras are transparently handled by libgphoto and/or libptp. In this case, "transparently" means that PTP devices do not get block devices. The alternative to PTP mode, USB Mass Storage (UMS) mode, is not supported by all cameras. Some cameras have a menu item that allows switching between the two modes; refer to your camera's user manual. If your camera does not support UMS mode and therefore cannot be accessed as a block device, your only alternative is to use a flash media reader and physically remove the storage media from your camera.

## Foremost

Foremost is a console program to recover files based on their headers, footers, and internal data structures. This process is commonly referred to as data carving. Foremost can work on disk image files (such as those generated by dd, Safeback, Encase, etc.) or directly on a drive. The headers and footers can be specified by a configuration file or command line switches can be used to specify built-in file types. These built-in types look at the data structures of a given file format, allowing for more reliable and faster recovery.

See Foremost article.

## Extundelete

Extundelete is a terminal-based utility designed to recover deleted files from ext3 and ext4 partitions. It can recover all the recently deleted files from a partition and/or a specific file(s) given by relative path or inode information. Note that it works only when the partition is unmounted. The recovered files are saved in the current directory under the folder named `RECOVERED_FILES/`.

### Installation

extundelete is available in the official repositories.

### Usage

Derived from the post on Linux Poison.

To recover data from a specific partition, the device name for the partition, which will be in the format `/dev/sdXN` (X is a letter and N is a number.), must be known. The example used here is `/dev/sda4`, but your system might use something different (For example, MMC card readers use `/dev/mmcblkNpN` as their naming scheme.) depending on your filesystem and device configuration. If you are unsure, run `df`, which prints currently mounted partitions.

Once which partition data is to be recovered from has been determined, simply run:

    # extundelete /dev/sda4 --restore-file directory/file

Any subdirectories must be specified, and the command runs from the highest level of the partition, so, to recover a file in `/home/SomeUserName/`, assuming `/home` is on its own partition, run:

    # extundelete /dev/sda4 restore-file SomeUserName/SomeFile

To speed up multi-file recovery, extundelete has a `--restore-files` option as well.

To recover an entire directory, run:

    # extundelete /dev/sda4 --restore-directory SomeUserName/SomeDirectory

For advanced users, to manually recover blocks or inodes with extundelete, debugfs can be used to find the inode to be recovered; then, run:

    # extundelete --restore-inode inode

inode stands for any valid inode. Additional inodes to recover can be listed in an unspaced, comma-separated fashion.

Finally, to recover all deleted files from an entire partition, run:

    # extundelete /dev/sda4 --restore-all

## Testdisk and PhotoRec

TestDisk and Photorec are both open-source data recovery utilities licensed under the terms of the GNU Public License (GPL).

TestDisk is primarily designed to help recover lost partitions and/or make non-booting disks bootable again when these symptoms are caused by faulty software, certain types of viruses, or human error, such as the accidental deletion of partition tables.

PhotoRec is file recovery software designed to recover lost files including photographs (Hint: PhotographRecovery), videos, documents, archives from hard disks and CD-ROMs. PhotoRec ignores the filesystem and goes after the underlying data, so it will still work even with a re-formatted or severely damaged filesystems and/or partition tables.

### Installation

testdisk from the official repositories provides both TestDisk and PhotoRec.

### Files recovered by photorec

The photorec utility stores recovered files with a random names(for most of the files) under a numbered directories, e.g. `./recup_dir.1/f872690288.jpg`, `./recup_dir.1/f864563104_wmclockmon-0.1.0.tar.gz`.

### See also

- Wiki (TestDisk): http://www.cgsecurity.org/wiki/TestDisk
- Wiki (Photorec): http://www.cgsecurity.org/wiki/PhotoRec
- Homepage: http://www.cgsecurity.org/
- Sort images by resolution
- Restore name of a tar.gz archive
- Post recovery tasks

## e2fsck

e2fsck is the ext2/ext3 filesystem checker included in the base install of Arch. e2fsck relies on a valid superblock. A superblock is a description of the entire filesystem's parameters. Because this data is so important, several copies of the superblock are distributed throughout the partition. With the `-b` option, e2fsck can take an alternate superblock argument; this is useful if the main, first superblock is damaged.

To determine where the superblocks are, run `dumpe2fs -h` on the target, unmounted partition. Superblocks are spaced differently depending on the filesystem's blocksize, which is set when the filesystem is created.

An alternate method to determine the locations of superblocks is to use the -n option with mke2fs. Be sure to use the `-n` flag, which, according to the `mke2fs` manpage, "Causes mke2fs to not actually create a filesystem, but display what it would do if it were to create a filesystem. This can be used to determine the location of the backup superblocks for a particular filesystem, so long as the mke2fs parameters that were passed when the filesystem was originally created are used again. (With the -n option added, of course!)".

### Installation

Both `e2fsck` and `dumpe2fs` are included in the base Arch install as part of e2fsprogs.

### See also

- e2fsck man page: http://phpunixman.sourceforge.net/index.php/man/e2fsck/8
- dumpe2fs man page: http://phpunixman.sourceforge.net/index.php?parameter=dumpe2fs&mode=man

## Working with raw disk images

If you have backed up a drive using ddrescue or dd and you need to mount this image as a physical drive, see this section.

### Mount the entire disk

To mount a complete disk image to the next free loop device, use the `losetup` command:

    # losetup -f -P /path/to/image

Tip:

- The `-f` flag mounts the image to the next available loop device.
- The `-P` flag creates additional devices for every partition.

### Mounting partitions

In order to be able to mount a partiton of a whole disk image, follow the steps above.

Once the whole disk image is mounted, a normal `mount` command can be used on the loop device:

    # mount /dev/loop0p1 /mnt/example

This command mounts the first partition of the image in loop0 to the folder to the mountpoint `/mnt/example`. Remember that the mountpoint directory must exist!

#### Getting disk geometry

Once the entire disk image has been mounted as a loopback device, its drive layout can be inspected.

### 使用 QEMU 来修复 NTFS

对于包含有不少于一个那些需要在Windows下 `chkdsk`一下的NTFS分区的镜像(为什么是Windows?因为Linux下没有优秀的NTFS检查器), QEMU可以把原始镜像在虚拟中当做物理硬盘来操作:

    # qemu -hda /path/to/primary.img -hdb /path/to/DamagedDisk.img

然后, 假设Windows被安装在 `primary.img`, 它就可以用来检查 `/path/to/DamagedDisk.img`上的分区.

## 文本文件恢复

只用少量命令便在一块硬盘中找到被删除的纯文本完全是可能的. 你需要一段来自你所需要恢复的文件的比较独一无二的字符串.

首先, 使用 `strings` 命令来搞到一个分区中的所有文本：

    # strings /dev/sdXN > BigStringsFile

然后, 使用 `grep` 来筛选 BigStringsFile中的内容:

    $ grep -i -200 "Unique string in text file" BigStringsFile > GrepOutputFile

Note: `-200` 选项告诉grep打印每个匹配字符串的前后200行. 如果你在寻找的文本很长, 也许你会需要调整下.

如果运气好的话, 你删掉的文本就在 GrepOutputFile里.

## 参阅

- Data Recovery on the Ubuntu wiki

Categories:

- File systems (简体中文)
- System recovery (简体中文)