Linux不像windows有那麼顯眼的回收站,不是簡單的還原就能夠了。
node
linux刪除文件還原能夠分爲兩種狀況,一種是刪除之後在進程存在刪除信息,一種是刪除之後進程都找不到,只有藉助於工具還原,這裏分別檢查介紹下。mysql
這種通常是有活動的進程存在持續標準輸入或輸出,到時文件被刪除後,進程PID仍是存在。這也就是有些服務器刪除一些文件可是磁盤不釋放的緣由。好比當前舉例說明:
經過一個shell終端對一個測試文件作cat追加操做:linux
[root@21yunwei_backup ~]# echo "hello py" > testdelete.py [root@21yunwei_backup ~]# cat >> testdelete.py hello delete
另一個終端查看這個文件能夠清楚看到內容:nginx
[root@21yunwei_backup ~]# cat testdelete.py hello py hello delete
此時,在當前服務器刪除文件rm -f ./testdelete.py
sql
命令查看這個目錄,文件已經不存在了,那麼如今咱們將其恢復出來。shell
1. lsof查看刪除的文件進程是否還存在。windows
這裏用到一個命令lsof,如沒有安裝請自行yum或者apt-get。相似這種狀況,咱們能夠先lsof查看刪除的文件 是否還在:bash
[root@21yunwei_backup ~]# lsof | grep deleted mysqld 1512 mysql 5u REG 252,3 0 6312397 /tmp/ibzW3Lot (deleted) cat 20464 root 1w REG 252,3 23 1310722 /root/testdelete.py (deleted)
幸運的是這種狀況進程還存在 ,那麼開始進行恢復 操做。服務器
2. 恢復。tcp
恢復命令:
cp /proc/pid/fd/1 /指定目錄/文件名
進入 進程目錄,通常是進入/proc/pid/fd/,針對當前狀況:
[root@21yunwei_backup ~]# cd /proc/20464/fd [root@21yunwei_backup fd]# ll total 0 lrwx------ 1 root root 64 Nov 15 18:12 0 > /dev/pts/1 l-wx------ 1 root root 64 Nov 15 18:12 1 > /root/testdelete.py (deleted) lrwx------ 1 root root 64 Nov 15 18:12 2 > /dev/pts/1
恢復操做:
cp 1 /tmp/testdelete.py
查看文件:
[root@21yunwei_backup fd]# cat /tmp/testdelete.py hello py hello delete
恢復完成。
建立準備刪除的目錄並echo一個 帶有內容的文件:
[root@21yunwei_backup 21yunwei]# tree . ├── deletetest │ └── mail │ └── test.py ├── lost+found └── passwd 3 directories, 2 files [root@21yunwei_backup 21yunwei]# cat /21yunwei/deletetest/mail/test.py hello Dj [root@21yunwei_backup 21yunwei]# tail -2 passwd haproxy:x:500:502::/home/haproxy:/bin/bash tcpdump:x:72:72::/:/sbin/nologin
執行刪除操做:
[root@21yunwei_backup 21yunwei]# rm -rf ./* [root@21yunwei_backup 21yunwei]# ll total 0
如今開始進行誤刪除文件的恢復。這種狀況通常是沒有守護進程或者後臺進程對其持續輸入,因此刪除就刪除了,lsof也看不到。就要藉助於工具。這裏咱們採用的工具是extundelete第三方工具。恢復步驟以下:
中止對當前分區作任何操做,防止inode被覆蓋。inode被覆蓋基本就告別恢復了。好比中止所在分區的服務,卸載目錄所在的設備,有必要的狀況下均可以斷網。
dd if=/path/filename of=/dev/vdc1
umount /dev/vdb1 或者 umount /21yunwei
若是提示設備busy,能夠用fuser命令強制卸載:
fuser -m -v -i -k /21yunwei
wget http://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2 tar jxvf extundelete-0.2.4.tar.bz2 cd extundelete-0.2.4 ./configure make make install
掃描誤刪除的文件:
[root@21yunwei_backup extundelete-0.2.4]# extundelete --inode 2 /dev/vdb1 NOTICE: Extended attributes are not restored. Loading filesystem metadata ... 8 groups loaded. Group: 0 Contents of inode 2: . .省略N行 File name | Inode number | Deleted status . 2 .. 2 lost+found 11 Deleted deletetest 12 Deleted passwd 14 Deleted
經過掃描發現了咱們刪除的文件夾,如今執行恢復操做。
[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-file passwd NOTICE: Extended attributes are not restored. Loading filesystem metadata ... 8 groups loaded. Loading journal descriptors ... 46 descriptors loaded. Successfully restored file passwd
恢復文件是放到了當前目錄RECOVERED_FILES。
查看恢復的文件:
[root@21yunwei_backup /]# tail -5 RECOVERED_FILES/passwd mysql:x:497:500::/home/mysql:/bin/false nginx:x:496:501::/home/nginx:/sbin/nologin zabbix:x:495:497:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin haproxy:x:500:502::/home/haproxy:/bin/bash tcpdump:x:72:72::/:/sbin/nologin
[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-directory deletetest NOTICE: Extended attributes are not restored. Loading filesystem metadata ... 8 groups loaded. Loading journal descriptors ... 46 descriptors loaded. Searching for recoverable inodes in directory deletetest ... 5 recoverable inodes found. Looking through the directory structure for deleted files ... [root@21yunwei_backup /]# cat RECOVERED_FILES/deletetest/mail/test.py hello Dj
[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-all NOTICE: Extended attributes are not restored. Loading filesystem metadata ... 8 groups loaded. Loading journal descriptors ... 46 descriptors loaded. Searching for recoverable inodes in directory / ... 5 recoverable inodes found. Looking through the directory structure for deleted files ... 0 recoverable inodes still lost. [root@21yunwei_backup /]# cd RECOVERED_FILES/ [root@21yunwei_backup RECOVERED_FILES]# tree . ├── deletetest │ └── mail │ └── test.py └── passwd 2 directories, 2 files
[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-inode 14 NOTICE: Extended attributes are not restored. Loading filesystem metadata ... 8 groups loaded. Loading journal descriptors ... 46 descriptors loaded. [root@21yunwei_backup /]# tail -5 /RECOVERED_FILES/file.14 mysql:x:497:500::/home/mysql:/bin/false nginx:x:496:501::/home/nginx:/sbin/nologin zabbix:x:495:497:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin haproxy:x:500:502::/home/haproxy:/bin/bash tcpdump:x:72:72::/:/sbin/nologin
注意恢復inode的時候,恢復 出來的文件名和以前不同,須要單獨進行更名。內容是沒問題的。
更多的extundelete用法請參考extundelete –help選項參數說明,當前恢復全部的操做完成。