在linux使用命令行,有時會發生誤刪除文件的情況,發生這種情況的時候不要慌張, 還是有一定辦法恢復刪除文件的,首先將刪除文件的所在磁盤卸載掉,防止inode被覆蓋讀寫找不回來了, 然后使用工具進行恢復。這里推薦一個工具:extundelete
1. 工具安裝
可以通過yum install extundelete安裝,
也可以從網上下載安裝:
2. 找回文件
1.卸載刪除文件所在磁盤
如下在work目錄有一個文件test-del,里面有兩行內容, 刪除這個文件,查看到work目錄掛著在/dev/vdb1磁盤,
退出work目錄返回到上層目錄, 使用umount /dev/vdb1卸載目錄
[root@VM-16-14-centos work]# cat test-del
12345667
abcdefg
[root@VM-16-14-centos work]#
[root@VM-16-14-centos work]# rm test-del -f
[root@VM-16-14-centos work]# cd ../
[root@VM-16-14-centos data]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 989M 0 989M 0% /dev
tmpfs 1000M 24K 1000M 1% /dev/shm
tmpfs 1000M 592K 999M 1% /run
tmpfs 1000M 0 1000M 0% /sys/fs/cgroup
/dev/vda1 79G 3.8G 72G 5% /
tmpfs 200M 0 200M 0% /run/user/0
/dev/vdb1 30G 19G 9.4G 67% /data/work
[root@VM-16-14-centos data]# umount /dev/vdb1
[root@VM-16-14-centos data]#
2. 查找刪除文件的inode,進行恢復
通過extundelete --inode 2 /dev/vdb1 查找刪除文件的inode,可以看到test-del文件的inode 是4680,
使用extundelete --restore-inode 4680 /dev/vdb1進行恢復, 恢復的文件在當前的RECOVERED_FILES目錄下,名字為file.inode,
對于test-del就是file.4680, 查看內容和刪除前的一致,文件恢復成功。
如果要恢復所以刪除的文件可以使用
extundelete --restore-all /dev/vdb1 進行恢復;
如果刪除的文件在磁盤的目錄下, 需要使用extundelete --inode 目錄inode進行逐級目錄查找。
[root@VM-16-14-centos data]# extundelete --inode 2 /dev/vdb1 |grep test-del
test-del 4680 Deleted
[root@VM-16-14-centos data]# extundelete --restore-inode 4680 /dev/vdb1
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 240 groups loaded.
Loading journal descriptors ... 29196 descriptors loaded.
[root@VM-16-14-centos data]# cat RECOVERED_FILES/file.4680
12345667
abcdefg
[root@VM-16-14-centos data]#