Linux下恢復誤刪文件:思路+實踐


周五籃球群里有人問誤刪文件了怎么恢復,得知是ext4文件系統之后我推薦了ext4magic這個工具,然后又有人提到了xfs的話怎么辦,正好前幾天看到Dave Chinner在郵件列表里提到了這個問題,他推薦的工具是xfs_irecover。這里就稍微總結一下Linux下誤刪文件如何恢復。

 

1. 當發現誤刪了文件之后,立即把文件系統卸載掉,或者remount成ro狀態,就是不要再寫了,讓數據不要被其他數據覆蓋。因為大部分文件系統在刪除文件的時候只是把這個文件標記成刪除,把文件所使用的數據塊標記成可用,但是上邊的數據還沒有被清除,數據還是在的。那么這個時候不再寫硬盤也就保證了數據塊不會被其他數據覆蓋掉,也就還有希望找回來。

 

2. 這一步是可選的。最好把要恢復的分區做一個鏡像,dd if=/dev/sda5 of=/path/to/image/file bs=4k,這樣在恢復的時候在鏡像上盡興,就算恢復出錯數據被毀掉了,那也是鏡像。

 

3. 根據不同的文件系統,選用不同的工具來找回刪除的文件。ext3推薦用ext3grep,ext4用ext4magic,其實ext4magic是基於ext3grep的,而且ext4magic也能處理ext2/3文件系統;xfs用xfs_irecover,xfs_irecover的manpage在這里。

 

4. 至於能夠恢復多少數據出來,那就看人品了

 

這里用ext4做個例子

 

# create ext4 fs and copy some files there

fallocate -l 16m ext4.img

losetup -f --show ext4.img

mkfs -t ext4 /dev/loop0

mount /dev/loop0 /mnt/ext4

cp  /mnt/ext4/

sync

 

# delete some files

rm /mnt/ext4/*

 

# umount the ext4 fs, this is important!

umount /dev/loop0

# make a copy of the fs

dd if=/dev/loop0 of=ext4-copy.img bs=4k

# run ext4magic on the image

ext4magic -m -d outputdir ext4-copy.img

# some sample output from the command

eguan@localhost:~/workspace/src/kernel$ sudo /home/eguan/bin/ext4magic -m ext4.img -d testdir

Warning: Activate magic-scan or disaster-recovery function, may be some command line options ignored

"testdir"  accept for recoverdir

Filesystem in use: ext4.img

 

Using  internal Journal at Inode 8

Activ Time after  : Sun Jun  7 22:43:54 2015

Activ Time before : Sun Jun  7 23:02:18 2015

Inode 2 is allocated

Unknown code ext2 45 #0 for block bitmap for ext4.img

Warning: error-NR 2133571363 can not found file: /

MAGIC-1 : start lost directory search

MAGIC-2 : start lost file search

--------        testdir/MAGIC-2/image/jpeg/I_0000000012.jpg

--------        testdir/MAGIC-2/image/jpeg/I_0000000013.jpg

--------        testdir/MAGIC-2/image/jpeg/I_0000000014.jpg

MAGIC-2 : start lost in journal search

MAGIC-3 : start ext4-magic-scan search

ext4magic : EXIT_SUCCESS

更多使用方法看ext4magic的manpage吧,就在源碼包里。

閱讀原文


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM