How to restore accidentally deleted Linux Files

Source: Internet
Author: User
Reprinted at: Workshop. Although the DELETE command only marks the deletion in the file node and does not actually clear the file content, other users and some processes with disk write operations will soon overwrite the data. However, you can recover a Linux instance that is used by a single machine in your home, or rectify the file by mistake.

1. A brief introduction to the ext2 File System Structure

In the ext2 File System Used in Linux, files are stored in blocks. By default, the size of each block is 1 K. Different blocks are distinguished by block numbers. Each file has a node that contains information such as the file owner, read/write permission, and file type. For a file smaller than 12 blocks, the block number of the file data block is directly stored in the node. If the file contains more than 12 blocks, the node stores the block number of an indirect block after the 12 blocks. In the block corresponding to this indirect block number, block number that stores 256 file data blocks (each block number in ext2fs occupies 4 bytes, so that the block number that can be stored in a block is 1024/4 = 256 ). If a larger file exists, the second-level indirect block and third-level indirect block appear in the node.

2. Restore the accidentally deleted file

Most Linux distributions provide a debugfs tool for editing ext2 file systems. However, there is still some work to do before using this tool.

First, Remount the partition where the accidentally deleted file is located in read-only mode. Run the following command: (assume that the file is in the/usr partition)

Mount-r-n-o remount/usr-r indicates read-only mounting;-N indicates no write to/etc/mtab. If the file is recovered from/etc, add this parameter. If the system says XXX partion busy, you can run the fuser command to check which processes use the files in this partition:

Fuser-v-M/usr

If there are no important processes, run the following command to stop them:

Fuser-K-v-M/usr

Then you can remount these file systems.

If all the files are installed in a large partition, you can use Linux single to enter the single-user mode at the boot prompt to minimize the chance of system processes writing data to the hard disk, or simply mount the hard disk on another machine. In addition, do not write the recovered data to/to avoid damaging the useful data. If the host has DoS/Windows, you can write it to these partitions:

Mount-r-N/dev/hda1/mnt/had
Then you can run debugfs: (Suppose Linux is in/dev/hda5)
# Debugfs/dev/hda5
The prompt debugfs appears:
The lsdel command can be used to list the information of many deleted files:
Debugfs: lsdel
Debugfs: 2692 deleted inodes found.
Inode owner mode size blocks time deleted
164821 0 100600 8192 1/1 sun May 13 19:22:46 2001
.............................................................................................
36137 0 100644 4 1/1 Tue Apr 24 10:11:15 2001
196829 0 100644 149500 38/38 mon May 27 13:52:04 2001
Debugfs:

There are many objects listed (2692 objects are found here). The first field is the file node number, the second field is the file owner, the third field is the read/write permission, and the next is the file size, which occupies the number of parts, deletion time. Then we can determine what we need based on the file size and deletion date. For example, we want to restore a file with a node of 196829:

You can first check the file data status:

Debugfs: stat <196829>
Inode: 196829 type: regular mode: 0644 flags: 0x0 version: 1
User: 0 group: 0 size: 149500
File ACL: 0 directory ACL: 0
Links: 0 blockcount: 38
Fragment: Address: 0 Number: 0 size: 0
Ctime: 0x31a9a574 -- mon May 27 13:52:04 2001
Atime: 0x31a21dd1 -- Tue May 21 20:47:29 2001
Mtime: 0x313bf4d7 -- Tue Mar 5 08:01:27 2001
Dtime: 0x31a9a574 -- mon May 27 13:52:04 2001
Blocks:
594810 594811 594814 594815 594816 ........................................
Total: 38
Then you can use the dump command to restore the file:
Debugfs: Dump <196829>/mnt/hda/01.sav
In this way, the file is restored. Exit debugfs:
Debugfs: Quit
Another method is to manually edit inode:
Debugfs: Mi <196829>
Mode [0100644]
User ID [0]
Group ID [0]
Size [149500]
Creation Time [0x31a9a574]
Modification time [0x31a9a574]
Access time [0x31a21dd1]
Deletion time [0x31a9a574] 0
Link count [0] 1
Block count [38]
File flags [0x0]
Reserved1 [0]
File ACL [0]
Directory ACL [0]
Fragment Address [0]
Fragment number [0]
Fragment Size [0]
Direct block #0 [594810]
..................................
Triple indirect block [0]

After the MI command is used, a line of information is displayed each time for editing. For other lines, press enter to confirm. Change the deletion time to 0 (not deleted), and change link count to 1. After modification, exit debugfs:

Debugfs: Quit

Then use fsck to check/dev/hda5

Fsck/dev/hda5

The program will find the lost data block and put it in lost + found. The files in this directory are what we need.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.