首頁 運維 linux運維 linux刪除的檔案如何恢復?

linux刪除的檔案如何恢復?

Apr 20, 2020 pm 04:12 PM
linux

linux刪除的檔案如何恢復?以下這篇文章為大家介紹一下恢復Linux刪除檔案的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有幫助。

linux刪除的檔案如何恢復?

linux不像windows有個回收站,使用rm -rf *基本上檔案是找不回來的。

那麼問題來了:

對於linux下誤刪的文件,我們是否真的無法透過軟體進行恢復呢?

答案當然是否定的,對於誤刪的文件,我們還是能透過軟體恢復過來的。誤刪檔案還原可以分成兩種情況:

  • 一種是刪除以後在行程存在刪除資訊

  • 一種是刪除以後進程都找不到,只有借助工具還原。

接下來以範例分別解說下兩種不同的誤刪還原方式:

錯誤刪除檔案程式還在的情況:

這種一般是有活動的進程存在持續標準輸入或輸出,到時檔案被刪除後,進程PID依舊存在。這也是為什麼有些伺服器刪除一些檔案但是磁碟不釋放的原因。

打開一個終端機對一個測試檔案做cat追加操作:

[root@docking ~]# echo "This is DeleteFile test." > deletefile.txt
[root@docking ~]# ls
deletefile.txt
[root@docking ~]# cat >> deletefile.txt 
Add SomeLine into deletefile for fun.
登入後複製

打開另外一個終端機查看這個檔案可以清楚看到內容:

[root@docking ~]# ls
deletefile.txt
[root@docking ~]# cat deletefile.txt 
This is DeleteFile test.
Add SomeLine into deletefile for fun.
登入後複製

此時,刪除文件rm -f deletefile.txt

[root@docking ~]# rm -f deletefile.txt 
[root@docking ~]# ls
#命令查看这个目录,文件已经不存在了,那么现在我们将其恢复出来。
登入後複製
  • lsof查看刪除的檔案程式是否還存在。

  • 如沒有安裝請自行yum install lsofapt-get install lsof

1.類似這種情況,我們可以先lsof查看刪除的檔案是否還在

[root@docking ~]# lsof | grep deletefile
cat       21796          root    1w      REG              253,1        63     138860 /root/deletefile.txt (deleted)
登入後複製

2、恢復cp /proc/pid/fd/1 /指定目錄/檔案名稱

#進入進程目錄,一般是進入/proc/pid/fd/,針對當前情況:

[root@docking ~]# cd /proc/21796/fd
[root@docking fd]# ll
总用量 0
lrwx------ 1 root root 64 1月  18 22:21 0 -> /dev/pts/0
l-wx------ 1 root root 64 1月  18 22:21 1 -> /root/deletefile.txt (deleted)
lrwx------ 1 root root 64 1月  18 22:21 2 -> /dev/pts/0
登入後複製

恢復操作:

[root@docking fd]# cp 1 ~/deletefile.txt.backup
[root@docking fd]# cat ~/deletefile.txt.backup 
This is DeleteFile test.
Add SomeLine into deletefile for fun.
登入後複製

3、恢復完成。

誤刪除的檔案進程已經不存在,借助工具還原

#準備一些檔案目錄

#准备一份挂载的盘
mkdir backuptest
cd backuptest
mkdir deletetest
mkdir deletetest/innerfolder
echo "Delete a folder test." > deletetest/innerfolder/deletefile.txt 

echo "tcpdump:x:172:72::/:/sbin/nologin" > tmppasswd
登入後複製

最後準備的目錄結構如下:

taroballs@taroballs-PC:/media/taroballs/taroballs/backuptest$ cd ..
taroballs@taroballs-PC:/media/taroballs/taroballs$ tree backuptest/
backuptest/
├── deletetest
│   └── innerfolder
│       └── deletefile.txt
└── tmppasswd

2 directories, 2 files
登入後複製

現在開始刪除該目錄rm -rf backuptest/

taroballs@taroballs-PC:/media/taroballs/taroballs$ rm -rf backuptest/
taroballs@taroballs-PC:/media/taroballs/taroballs$  ls  -l
总用量 0
登入後複製

這種情況一般是沒有守護進行或後台程序對其持續輸入,所以刪除就真的刪除了。 lsof也看不到,故需要採用工具來恢復。

現在開始進行誤刪除檔案的復原。

我們採用的工具是extundelete第三方工具。復原步驟以及注意事項如下:

  • 停止對目前分割區做任何操作,防止inode被覆寫。 inode被覆蓋基本上就告別恢復了。

  • 誇張一點講,例如停止所在分區的服務,卸載目錄所在的設備,有必要的情況下都可以斷網。

  • 透過dd指令對 目前分割區進行備份,防止第三方軟體復原失敗導致資料遺失。

  • 適合資料非常重要的情況,這裡是例子,所以就沒有備份,例如備份可以考慮如下方式:dd if=/path/filename of=/dev/vdc1

  • 透過umount指令,對目前裝置分割區卸載。或fuser 指令umount /dev/vdb1

  • 如果提示裝置busy,可以用fuser指令強制卸載:fuser -m -v -i -k ./

  • #下載第三方工具extundelete安裝,搜尋誤刪除的檔案還原

#extundelete工具安裝

extundelete下載位址: http://extundelete.sourceforge.net/

wget https://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2
登入後複製

解壓縮該檔案tar jxvf extundelete-0.2.4.tar.bz2

#若報這種錯誤

[root@docking ~]# tar jxvf extundelete-0.2.4.tar.bz2 
tar (child): bzip2:无法 exec: 没有那个文件或目录
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
登入後複製

則使用yum -y install bzip2進行解決

[root@docking ~]# tar jxvf extundelete-0.2.4.tar.bz2 
extundelete-0.2.4/
extundelete-0.2.4/acinclude.m4
extundelete-0.2.4/missing
extundelete-0.2.4/autogen.sh
extundelete-0.2.4/aclocal.m4
extundelete-0.2.4/configure
extundelete-0.2.4/LICENSE
extundelete-0.2.4/README
...................................................
登入後複製
cd  extundelete-0.2.4
./configure
登入後複製

若這步驟報錯

[root@docking extundelete-0.2.4]# ./configure 
Configuring extundelete 0.2.4
configure: error: in `/root/extundelete-0.2.4':
configure: error: C++ compiler cannot create executables
See `config.log' for more details
登入後複製

則使用yum -y install gcc-c 解決.

若執行上一個步驟仍然報錯,

[root@docking extundelete-0.2.4]# ./configure 
Configuring extundelete 0.2.4
configure: error: Can't find ext2fs library
登入後複製

則使用yum -y install e2fsprogs e2fsprogs-devel來解決。
#Ubuntu的解決方法為sudo apt-get install e2fslibs-dev e2fslibs-dev

不出意外的話到這裡應該configure能夠順利完成.

[root@docking extundelete-0.2.4]# ./configure 
Configuring extundelete 0.2.4
Writing generated files to disk
[root@docking extundelete-0.2.4]#
登入後複製

最後make然後 make install

[root@docking extundelete-0.2.4]# make
make -s all-recursive
Making all in src
extundelete.cc: 在函数‘ext2_ino_t find_inode(ext2_filsys, ext2_filsys, ext2_inode*, std::string, int)’中:
extundelete.cc:1272:29: 警告:在 {} 内将‘search_flags’从‘int’转换为较窄的类型‘ext2_ino_t {aka unsigned int}’ [-Wnarrowing]
    buf, match_name2, priv, 0};
                             ^
[root@docking extundelete-0.2.4]# make install
Making install in src
  /usr/bin/install -c extundelete '/usr/local/bin'
登入後複製

extundelete安裝完成.

##掃描錯誤刪除的檔案:

使用

df -lh查看掛載:

taroballs@taroballs-PC:~$ df -lh
文件系统        容量  已用  可用 已用% 挂载点
udev            1.9G     0  1.9G    0% /dev
tmpfs           387M  1.8M  385M    1% /run
/dev/sda2        92G   61G   26G   71% /
tmpfs           1.9G   49M  1.9G    3% /dev/shm
tmpfs           5.0M  4.0K  5.0M    1% /run/lock
tmpfs           1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda3       104G   56G   44G   57% /home
tmpfs           387M   40K  387M    1% /run/user/1000
/dev/sda4        70G   20G   47G   30% /media/taroballs/d8423f8c-d687-4c03-a7c8-06a7fb57f96d
/dev/sdb1       6.8G  4.1G  2.8G   60% /media/taroballs/taroballs
/dev/sr0        4.0G  4.0G     0  100% /media/taroballs/2018-01-16-12-36-00-00
taroballs@taroballs-PC:~$ cd /media/taroballs/taroballs/
taroballs@taroballs-PC:/media/taroballs/taroballs$
登入後複製

可以看到,我們的目錄/media/taroballs/taroballs

掛載到/dev/sdb1這個檔案系統中.

umount我們的掛載碟

例如:

taroballs@taroballs-PC:~$ df -lh | grep /dev/sdb1
/dev/sdb1       6.8G  4.1G  2.8G   60% /media/taroballs/taroballs
登入後複製

umount這個目錄

taroballs@taroballs-PC:~$ umount /media/taroballs/taroballs
taroballs@taroballs-PC:~$ df -lh | grep /dev/sdb1
taroballs@taroballs-PC:~$ 
#记得删除一定要后umount哦,不然二次写入谁也帮不了你呢。
登入後複製

#透過inode節點復原

taroballs@taroballs-PC:~$ mkdir recovertest
taroballs@taroballs-PC:~$ cd recovertest/
taroballs@taroballs-PC:~/recovertest$
登入後複製

執行復原

extundelete /dev/sdb1 --inode 2

taroballs@taroballs-PC:/media/taroballs/taroballs$ sudo extundelete /dev/sdb1 --inode 2
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
deletetest                                        12             Deleted
tmppasswd                                            14             Deleted
登入後複製

透過掃描發現了我們刪除的資料夾,現在執行恢復操作。

(1)恢復單一檔案tmppasswd

taroballs@taroballs-PC:~/recovertest$  extundelete /dev/sdb1 --restore-file passwd   
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
Successfully restored file tmppasswd
登入後複製

恢復檔案是放到了目前目錄RECOVERED_FILES。

查看恢復的檔案:

taroballs@taroballs-PC:~/recovertest$ cat tmppasswd 
tcpdump:x:172:72::/:/sbin/nologin
登入後複製

(2)恢復目錄deletetest

extundelete /dev/sdb1 --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 ...
登入後複製

(3)恢復所有

taroballs@taroballs-PC:~/recovertest$ extundelete /dev/sdb1 --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. 
taroballs@taroballs-PC:~/recovertest$ tree 
backuptest/
├── deletetest
│   └── innerfolder
│       └── deletefile.txt
└── tmppasswd
2 directories, 2 files
登入後複製

(4)恢復指定inode

taroballs@taroballs-PC:~/recovertest$ extundelete /dev/sdb1 --restore-inode 14
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
taroballs@taroballs-PC:~/recovertest$ cat file.14 
tcpdump:x:172:72::/:/sbin/nologin
#注意恢复inode的时候,恢复 出来的文件名和之前不一样,需要单独进行改名。
登入後複製

最後附上

extundelete的用法:

$ extundelete --help
Usage: extundelete [options] [--] device-file
Options:
  --version, -[vV]       Print version and exit successfully.
  --help,                Print this help and exit successfully.
  --superblock           Print contents of superblock in addition to the rest.
                         If no action is specified then this option is implied.
  --journal              Show content of journal.
  --after dtime          Only process entries deleted on or after 'dtime'.
  --before dtime         Only process entries deleted before 'dtime'.Actions:
  --inode ino            Show info on inode 'ino'.
  --block blk            Show info on block 'blk'.
  --restore-inode ino[,ino,...]
                         Restore the file(s) with known inode number 'ino'.
                         The restored files are created in ./RECOVERED_FILES                         with their inode number as extension (ie, file.12345).
  --restore-file 'path'  Will restore file 'path'. 'path' is relative to root
                         of the partition and does not start with a '/'
                         The restored file is created in the current
                         directory as 'RECOVERED_FILES/path'.
  --restore-files 'path' Will restore files which are listed in the file 'path'.
                         Each filename should be in the same format as an option
                         to --restore-file, and there should be one per line.
  --restore-directory 'path'
                         Will restore directory 'path'. 'path' is relative to the
                         root directory of the file system.  The restored
                         directory is created in the output directory as 'path'.
  --restore-all          Attempts to restore everything.
  -j journal             Reads an external journal from the named file.
  -b blocknumber         Uses the backup superblock at blocknumber when opening
                         the file system.
  -B blocksize           Uses blocksize as the block size when opening the file
                         system.  The number should be the number of bytes.
  --log 0                Make the program silent.
  --log filename         Logs all messages to filename.--log D1=0,D2=filename   Custom control of log messages with comma-separated
   Examples below:       list of options.  Dn must be one of info, warn, or   --log info,error      error.  Omission of the '=name' results in messages   --log warn=0          with the specified level to be logged to the console.
   --log error=filename  If the parameter is '=0', logging for the specified
                         level will be turned off.  If the parameter is
                         '=filename', messages with that level will be written
                         to filename.
   -o directory          Save the recovered files to the named directory.
                         The restored files are created in a directory
                         named 'RECOVERED_FILES/' by default.
登入後複製

推荐:《linux教程

以上是linux刪除的檔案如何恢復?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

centos和ubuntu的區別 centos和ubuntu的區別 Apr 14, 2025 pm 09:09 PM

CentOS 和 Ubuntu 的關鍵差異在於:起源(CentOS 源自 Red Hat,面向企業;Ubuntu 源自 Debian,面向個人)、包管理(CentOS 使用 yum,注重穩定;Ubuntu 使用 apt,更新頻率高)、支持週期(CentOS 提供 10 年支持,Ubuntu 提供 5 年 LTS 支持)、社區支持(CentOS 側重穩定,Ubuntu 提供廣泛教程和文檔)、用途(CentOS 偏向服務器,Ubuntu 適用於服務器和桌面),其他差異包括安裝精簡度(CentOS 精

centos如何安裝 centos如何安裝 Apr 14, 2025 pm 09:03 PM

CentOS 安裝步驟:下載 ISO 映像並刻錄可引導媒體;啟動並選擇安裝源;選擇語言和鍵盤佈局;配置網絡;分區硬盤;設置系統時鐘;創建 root 用戶;選擇軟件包;開始安裝;安裝完成後重啟並從硬盤啟動。

Centos停止維護2024 Centos停止維護2024 Apr 14, 2025 pm 08:39 PM

CentOS將於2024年停止維護,原因是其上游發行版RHEL 8已停止維護。該停更將影響CentOS 8系統,使其無法繼續接收更新。用戶應規劃遷移,建議選項包括CentOS Stream、AlmaLinux和Rocky Linux,以保持系統安全和穩定。

Centos停止維護後的選擇 Centos停止維護後的選擇 Apr 14, 2025 pm 08:51 PM

CentOS 已停止維護,替代選擇包括:1. Rocky Linux(兼容性最佳);2. AlmaLinux(與 CentOS 兼容);3. Ubuntu Server(需要配置);4. Red Hat Enterprise Linux(商業版,付費許可);5. Oracle Linux(與 CentOS 和 RHEL 兼容)。在遷移時,考慮因素有:兼容性、可用性、支持、成本和社區支持。

docker原理詳解 docker原理詳解 Apr 14, 2025 pm 11:57 PM

Docker利用Linux內核特性,提供高效、隔離的應用運行環境。其工作原理如下:1. 鏡像作為只讀模板,包含運行應用所需的一切;2. 聯合文件系統(UnionFS)層疊多個文件系統,只存儲差異部分,節省空間並加快速度;3. 守護進程管理鏡像和容器,客戶端用於交互;4. Namespaces和cgroups實現容器隔離和資源限制;5. 多種網絡模式支持容器互聯。理解這些核心概念,才能更好地利用Docker。

docker desktop怎麼用 docker desktop怎麼用 Apr 15, 2025 am 11:45 AM

如何使用 Docker Desktop? Docker Desktop 是一款工具,用於在本地機器上運行 Docker 容器。其使用步驟包括:1. 安裝 Docker Desktop;2. 啟動 Docker Desktop;3. 創建 Docker 鏡像(使用 Dockerfile);4. 構建 Docker 鏡像(使用 docker build);5. 運行 Docker 容器(使用 docker run)。

centos停止維護後怎麼辦 centos停止維護後怎麼辦 Apr 14, 2025 pm 08:48 PM

CentOS 停止維護後,用戶可以採取以下措施應對:選擇兼容髮行版:如 AlmaLinux、Rocky Linux、CentOS Stream。遷移到商業發行版:如 Red Hat Enterprise Linux、Oracle Linux。升級到 CentOS 9 Stream:滾動發行版,提供最新技術。選擇其他 Linux 發行版:如 Ubuntu、Debian。評估容器、虛擬機或云平台等其他選項。

vscode需要什麼電腦配置 vscode需要什麼電腦配置 Apr 15, 2025 pm 09:48 PM

VS Code 系統要求:操作系統:Windows 10 及以上、macOS 10.12 及以上、Linux 發行版處理器:最低 1.6 GHz,推薦 2.0 GHz 及以上內存:最低 512 MB,推薦 4 GB 及以上存儲空間:最低 250 MB,推薦 1 GB 及以上其他要求:穩定網絡連接,Xorg/Wayland(Linux)

See all articles