Linux不像windows有那么顯眼的回收站,不是簡單的還原就可以了,linux洗掉檔案還原可以分為兩種情況,一種是洗掉以后在行程存在洗掉資訊,一種是洗掉以后行程都找不到,只有借助于工具還原,這里分別檢查介紹下,

一、誤洗掉檔案行程還在的情況,
這種一般是有活動的行程存在持續標準輸入或輸出,到時檔案被洗掉后,行程PID還是存在,這也就是有些服務器洗掉一些檔案但是磁盤不釋放的原因,比如當前舉例說明:通過一個shell終端對一個測驗檔案做cat追加操作:
[root@21yunwei_backup ~]# echo "hello py"> testdelete.py[root@21yunwei_backup ~]# cat >> testdelete.py hello delete
另外一個終端查看這個檔案可以清楚看到內容:
[root@21yunwei_backup ~]# cat testdelete.py hello pyhello delete
此時,在當前服務器洗掉檔案rm -f ./testdelete.py
命令查看這個目錄,檔案已經不存在了,那么現在我們將其恢復出來,
1. lsof查看洗掉的檔案行程是否還存在,
這里用到一個命令lsof,如沒有安裝請自行yum或者apt-get,類似這種情況,我們可以先lsof查看洗掉的檔案 是否還在:
[root@21yunwei_backup ~]# lsof | grep deletedmysqld 1512 mysql 5u REG 252,306312397/tmp/ibzW3Lot (deleted)cat 20464 root 1w REG 252,3231310722/root/testdelete.py (deleted)
幸運的是這種情況行程還存在 ,那么開始進行恢復 操作,
2. 恢復,
恢復命令:
cp /proc/pid/fd/1/指定目錄/檔案名
進入行程目錄,一般是進入/proc/pid/fd/,針對當前情況:
[root@21yunwei_backup ~]# cd /proc/20464/fd[root@21yunwei_backup fd]# lltotal 0lrwx------ 1 root root 64Nov1518:120> /dev/pts/1l-wx------ 1 root root 64Nov1518:121> /root/testdelete.py (deleted)lrwx------ 1 root root 64Nov1518:122> /dev/pts/1
恢復操作:
cp 1/tmp/testdelete.py
查看檔案:
[root@21yunwei_backup fd]# cat /tmp/testdelete.pyhello pyhello delete
恢復完成,
二、誤洗掉的檔案行程已經不存在,借助于工具還原,
創建準備洗掉的目錄并echo一個 帶有內容的檔案:
[root@21yunwei_backup 21yunwei]# tree.├── deletetest│ └── mail│ └── test.py├── lost+found└── passwd3 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/bashtcpdump:x:72:72::/:/sbin/nologin
執行洗掉操作:
[root@21yunwei_backup 21yunwei]# rm -rf ./*[root@21yunwei_backup 21yunwei]# lltotal 0
現在開始進行誤洗掉檔案的恢復,這種情況一般是沒有守護行程或者后臺行程對其持續輸入,所以洗掉就洗掉了,lsof也看不到,就要借助于工具,這里我們采用的工具是extundelete第三方工具,恢復步驟如下:
1.停止對當前磁區做任何操作,防止inode被覆寫,inode被覆寫基本就告別恢復了,比如停止所在磁區的服務,卸載目錄所在的設備,有必要的情況下都可以斷網,2.通過dd命令對當前磁區進行備份,防止第三方軟體恢復失敗導致資料丟失,適合資料非常重要的情況,這里測驗,就沒有備份,如備份可以考慮如下方式:
dd if=/path/filename of=/dev/vdc1
1.通過umount命令,對當前設備磁區卸載,或者fuser 命令,
umount /dev/vdb1 或者 umount /21yunwei
如果提示設備busy,可以用fuser命令強制卸載:
fuser -m -v -i -k /21yunwei
1.下載第三方工具extundelete安裝,搜索誤洗掉的檔案進行還原,
wget http://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2tar jxvf extundelete-0.2.4.tar.bz2cd extundelete-0.2.4./configure makemake install
掃描誤洗掉的檔案:
[root@21yunwei_backup extundelete-0.2.4]# extundelete --inode 2/dev/vdb1NOTICE: Extended attributes are not restored.Loading filesystem metadata ... 8 groups loaded.Group: 0Contents of inode 2:..省略N行File name | Inode number | Deleted status. 2.. 2lost+found 11Deleteddeletetest 12Deletedpasswd 14Deleted
通過掃描發現了我們洗掉的檔案夾,現在執行恢復操作,
1.恢復單一檔案passwd
[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/falsenginx:x:496:501::/home/nginx:/sbin/nologinzabbix:x:495:497:ZabbixMonitoringSystem:/var/lib/zabbix:/sbin/nologinhaproxy:x:500:502::/home/haproxy:/bin/bashtcpdump:x:72:72::/:/sbin/nologin
2.恢復目錄deletetest
[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.Searchingfor 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
3.恢復所有
[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-allNOTICE: Extended attributes are not restored.Loading filesystem metadata ... 8 groups loaded.Loading journal descriptors ... 46 descriptors loaded.Searchingfor 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└── passwd2 directories, 2 files
4.恢復指定inode
[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-inode 14NOTICE: 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.14mysql:x:497:500::/home/mysql:/bin/falsenginx:x:496:501::/home/nginx:/sbin/nologinzabbix:x:495:497:ZabbixMonitoringSystem:/var/lib/zabbix:/sbin/nologinhaproxy:x:500:502::/home/haproxy:/bin/bashtcpdump:x:72:72::/:/sbin/nologin
注意恢復inode的時候,恢復 出來的檔案名和之前不一樣,需要單獨進行改名,內容是沒問題的,
更多的extundelete用法請參考extundelete –help選項引數說明,當前恢復所有的操作完成,
以上就是良許教程網為各位朋友分享的Linux上恢復誤洗掉的檔案或目錄,
本文由博客一文多發平臺 OpenWrite 發布!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/253790.html
標籤:其他
