在使用Linux執行個體的過程中,刪除檔案是一項非常常見的營運操作。及時清理不再使用的檔案可以有效降低磁碟佔用。然而,如果由於人為誤操作、程式運行異常、電腦病毒攻擊等原因誤刪除了本應保留的檔案,您可能需要進行資料恢複。本文將為您介紹Linux執行個體中誤刪資料後的資料恢複整體思路及操作樣本。
背景介紹
本文所介紹的資料恢複思路與工具,旨在為誤刪檔案的使用者提供儘可能全面的恢複方案。檔案恢複的程度將根據使用者的實際情況及操作方式有所不同。因此,在執行操作之前,建議您結合自身的實際情況,採取最合理的資料恢複措施,最大限度地保障資料的恢複。
前提條件
-
在進行資料恢複操作之前,請您仔細閱讀資料恢複的整體思路。
-
資料恢複涉及到較多專業知識,請確保您具備相應的知識與技能,以避免對待恢複的資料造成損害。
-
在進行資料恢複操作之前,強烈建議您為待恢複資料的磁碟建立快照,以避免在恢複過程中因操作失誤而對待恢複資料造成損壞。
-
本文所介紹的資料恢複工具為第三方開源軟體,阿里雲對檔案恢複程度不作任何承諾。
-
如您需要恢複的資料極其重要,而您在資料恢複方面並不具備相關的專業知識,建議您不要隨意按照本教程進行操作。您可以考慮尋求專業的資料恢複機構進行處理。
整體思路
誤刪檔案導致系統無法啟動的資料恢複整體思路
當您以root使用者的身份在Linux執行個體上執行了rm -rf /*命令後,您的整個根目錄下的檔案會被刪除,此時作業系統啟動後會進入grub命令列介面,如您需要恢複原系統硬碟上的資料,那麼您可以參考如下處理思路,以完成資料恢複操作。
誤刪系統檔案後的資料恢複整體思路
當您在Linux執行個體上執行了rm -rf <file_or_dir>命令後,您的檔案或目錄會被刪除,如您需要恢複原系統硬碟上的資料,那麼您可以參考如下處理思路,以完成資料恢複操作。
如果您誤刪的檔案導致您無法使用SSH協議串連Linux執行個體,那麼您可以採用VNC的方式串連執行個體。
操作指南
上述內容介紹了兩種常見的Linux執行個體誤刪資料後的資料恢複思路。接下來,將以下述3個條件為例,詳細闡述資料恢複的操作步驟。
問題情境
-
假設您執行了
rm -rf /*命令,導致檔案誤刪,從而使系統無法啟動。 -
在執行刪除操作之前,未建立快照,或不希望通過快照進行資料恢複。
-
您希望通過另一台Linux執行個體進行資料恢複操作。
操作概覽
以上問題情境的操作流程如下所示。
操作步驟
-
為當前執行個體建立快照
在執行資料恢複之前,建議您先對需要執行資料恢複操作的執行個體系統硬碟或資料盤建立快照,以實現通過快照對現有資料進行備份,防止後續恢複操作過程中誤操作損壞檔案。關於建立快照的操作,請參見手動建立單個快照。
-
卸載要恢複資料的系統硬碟或資料盤,並掛載到新執行個體。
警告在卸載系統硬碟時需要先進行關機操作,在控制台操作關機時請務必勾選強制停止選項,否則會導致關機操作失敗。
強制停止等同於斷電處理,可能丟失執行個體作業系統中未寫入磁碟的資料,請謹慎選擇。
通過卸載系統硬碟或資料盤,並將其掛載到另一台Linux執行個體,以便完成後續資料恢複操作,關於卸載及掛載資料盤的操作,請參見卸載或掛載系統硬碟。
警告在控制台操作將原系統硬碟作為資料盤掛載到新執行個體時,由於系統硬碟檔案系統可能已經損壞,因此控制台會提示選擇初始化方式。在此情況下,請勿選擇立即操作,如選擇通過控制台重新初始化該磁碟,可能會對即將恢複的資料造成損害。請選擇關閉該對話方塊即可。
-
恢複誤刪的資料
通過使用testdisk工具,可以完成已刪除資料的恢複操作。以下將以在Linux執行個體中恢複誤刪目錄為例,介紹資料恢複的操作步驟。
-
運行以下命令,在新執行個體上安裝testdisk工具。
Alibaba Cloud Linux 3/2
sudo yum install -y testdiskCentOS 6/7/8
sudo yum install -y testdiskUbuntu/Debian
sudo apt install -y testdisk -
運行以下命令,以確認當前磁碟分割情況。
sudo fdisk -lu回顯結果樣本如下。
Disk /dev/vda: 145 GiB, 155692564480 bytes, 304087040 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: F51132A7-67B1-4650-806D-FD0DE6E1210C Device Start End Sectors Size Type /dev/vda1 2048 6143 4096 2M BIOS boot /dev/vda2 6144 415743 409600 200M EFI System /dev/vda3 415744 304087006 303671263 144.8G Linux filesystem Disk /dev/vdb: 40 GiB, 42949672960 bytes, 83886080 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: F51132A7-67B1-4650-806D-FD0DE6E1210C Device Start End Sectors Size Type /dev/vdb1 2048 6143 4096 2M BIOS boot /dev/vdb2 6144 415743 409600 200M EFI System /dev/vdb3 415744 83886046 83470303 39.8G Linux filesystem上述樣本中,
/dev/vda為現有執行個體系統硬碟,/dev/vdb為原執行個體系統硬碟,作為資料盤掛載在現有執行個體中。 -
運行以下命令,使用
testdisk開啟指定裝置。sudo testdisk /dev/vdb回顯資訊樣本如下。選擇
Proceed(預設值)後按斷行符號鍵確認。TestDisk 7.1, Data Recovery Utility, July 2019 Christophe GRENIER <grenier@cgsecurity.org> https://www.cgsecurity.org TestDisk is free software, and comes with ABSOLUTELY NO WARRANTY. Select a media (use Arrow keys, then press Enter): >Disk /dev/vdb - 42 GB / 40 GiB >[Proceed ] [ Quit ] Note: Disk capacity must be correctly detected for a successful recovery. If a disk listed above has an incorrect size, check HD jumper settings and BIOS detection, and install the latest OS patches and disk drivers. -
選擇分區表類型進行掃描。一般選擇Intel。如果您的資料盤採用
GPT分區,選擇EFI GPT。選擇完成後按斷行符號鍵確認。TestDisk 7.1, Data Recovery Utility, July 2019 Christophe GRENIER <grenier@cgsecurity.org> https://www.cgsecurity.org Disk /dev/vdb - 42 GB / 40 GiB Please select the partition table type, press Enter when done. [Intel ] Intel/PC partition >[EFI GPT] EFI GPT partition map (Mac i386, some x86_64...) [Humax ] Humax partition table [Mac ] Apple partition map (legacy) [None ] Non partitioned media [Sun ] Sun Solaris partition [XBox ] XBox partition [Return ] Return to disk selection Hint: EFI GPT partition table type has been detected.Note: Do NOT select 'None' for media with only a single partitio -
選擇Analyse後按斷行符號鍵,以進行磁碟分析。
TestDisk 7.1, Data Recovery Utility, July 2019 Christophe GRENIER <grenier@cgsecurity.org> https://www.cgsecurity.org Disk /dev/vdb - 42 GB / 40 GiB CHS 83220 16 63 - sector size=512 >[ Analyse ] Analyse current partition structure and search for lost partitions [ Advanced ] Filesystem Utils [ Geometry ] Change disk geometry [ Options ] Modify options [ Quit ] Return to disk selection Note: Correct disk geometry is required for a successful recovery. 'Analyse' process may give some warnings if it thinks the logical geometry is mismatched. -
選擇Quick Search後按斷行符號鍵快速搜尋。
TestDisk 7.1, Data Recovery Utility, July 2019 Christophe GRENIER <grenier@cgsecurity.org> https://www.cgsecurity.org Disk /dev/vdb - 42 GB / 40 GiB - CHS 83220 16 63 Current partition structure: Partition Start End Size in sectors 1 P Unknown 2048 6143 4096 2 P EFI System 6144 415743 409600 3 P Linux filesys. data 415744 83886046 83470303 P=Primary D=Deleted >[Quick Search] [ Backup ] Try to locate partition在返回結果中會顯示分區資訊。選中需要恢複資料的分區,按斷行符號鍵確認。
TestDisk 7.1, Data Recovery Utility, July 2019 Christophe GRENIER <grenier@cgsecurity.org> https://www.cgsecurity.org Disk /dev/vdb - 42 GB / 40 GiB - CHS 83220 16 63 Partition Start End Size in sectors P MS Data 6144 415743 409600 >P Linux filesys. data 415744 83886039 83470296 [root] Structure: Ok. Use Up/Down Arrow keys to select partition. Use Left/Right Arrow keys to CHANGE partition characteristics: P=Primary D=Deleted Keys A: add partition -
選擇Deeper Search,並按斷行符號鍵確認。
TestDisk 7.1, Data Recovery Utility, July 2019 Christophe GRENIER <grenier@cgsecurity.org> https://www.cgsecurity.org Disk /dev/vdb - 42 GB / 40 GiB - CHS 83220 16 63 Partition Start End Size in sectors 1 P MS Data 6144 415743 409600 2 P Linux filesys. data 415744 83886039 83470296 [root] [ Quit ] [ Return ] >[Deeper Search] [ Write ] Try to find more partitions -
等待搜尋完成後,會列出可恢複的檔案。選擇Continue並按斷行符號鍵確認。
TestDisk 7.1, Data Recovery Utility, July 2019 Christophe GRENIER <grenier@cgsecurity.org> https://www.cgsecurity.org Disk /dev/vdb - 42 GB / 40 GiB - CHS 83220 16 63 The harddisk (42 GB / 40 GiB) seems too small! (< 75 GB / 70 GiB) Check the harddisk size: HD jumper settings, BIOS detection... The following partitions can't be recovered: Partition Start End Size in sectors > Linux filesys. data 677886 84148181 83470296 [root] Linux filesys. data 1202174 84672469 83470296 [root] Linux filesys. data 1726462 85196757 83470296 [root] Linux filesys. data 2250750 85721045 83470296 [root] Linux filesys. data 2775038 86245333 83470296 [root] Linux filesys. data 6969342 90439637 83470296 [root] Linux filesys. data 7493630 90963925 83470296 [root] Linux filesys. data 13260798 96731093 83470296 [root] Linux filesys. data 17461456 100931751 83470296 [root] Linux filesys. data 17461598 100931893 83470296 [root] [ Continue ] ext4 blocksize=4096 Large_file Sparse_SB Recover, 42 GB / 39 GiB按P鍵列出檔案。
TestDisk 7.1, Data Recovery Utility, July 2019 Christophe GRENIER <grenier@cgsecurity.org> https://www.cgsecurity.org P Linux filesys. data 415744 83886039 83470296 [root] Directory / >dr-xr-xr-x 0 0 4096 20-Dec-2024 10:06 . dr-xr-xr-x 0 0 4096 20-Dec-2024 10:06 .. drwxr-xr-x 0 0 4096 20-Dec-2024 10:06 usr dr-xr-xr-x 0 0 4096 20-Dec-2024 10:03 boot drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 dev drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 proc drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 run drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 sys drwxr+xr-x 0 0 0 20-Dec-2024 10:06 etc -rw------- 0 0 0 20-Dec 2024 10:06 root drwxr-xr-x 0 0 4096 20-Dec-2024 10:06 var drwxr-xr-x 0 0 4096 20 Dec-2024 10:06 usr lrwxrwxrwx 0 0 7 9-Feb-2022 20:18 bin lrwxrwxrwx 0 0 8 9-Feb 2022 20:18 sbin lrwxrwxrwx 0 0 7 9-Feb-2022 20:18 lib lrwxrwxrwx 0 0 9 9 Feb 2022 20:18 lib64 lrwxrwxrwx 0 0 7 9-Feb-2022 20:18 bin;672827fb drwxr-xr-x 0 0 8 20-Dec-2024 18:03 home -rw------- 0 0 8 20-Dec-2024 09:43 .ismount-test-file drwxr-xr-x 0 0 0 29-Dec-2024 18:03 media drwxr-xr-x 0 0 8 20-Dec-2024 10:03 mnt drwxr-xr-x 0 0 8 20-Dec-2024 18:03 opt lrwxrwxrwx 0 0 9 9-Feb-2022 20:18 sbin;672827fb drwxr-xr-x 0 0 8 29-Dec-2024 18:04 srv drwxrwxrwt 0 0 4096 20-Dec-2024 10:06 tmp Next Use Right to change directory, h to hide deleted files q to quit, : to select the current file, a to select all files C to copy the selected files, c to copy the current file恢複指定檔案、檔案夾
選中要恢複的檔案,再按c鍵以複製檔案。
TestDisk 7.1, Data Recovery Utility, July 2019 Christophe GRENIER <grenier@cgsecurity.org> https://www.cgsecurity.org Linux filesys. data 415744 83886039 83470296 [root] Directory / dr-xr-xr-x 0 0 4096 18-Dec-2024 11:14 . dr-xr-xr-x 0 0 4096 18-Dec-2024 11:14 .. drwx------ 0 0 0 18-Dec-2024 11:13 lost+found dr-xr-xr-x 0 0 4096 18-Dec-2024 11:13 boot drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 dev drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 proc drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 run drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 sys drwxr-xr-x 0 0 0 18-Dec-2024 11:15 etc dr-xr-x--- 0 0 4096 18-Dec-2024 11:14 root drwxr-xr-x 0 0 4096 4-Nov-2024 10:03 var drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 usr lrwxrwxrwx 0 0 7 9-Feb-2022 20:18 bin lrwxrwxrwx 0 0 8 9-Feb-2022 20:18 sbin Next Use Right to change directory, h to hide deleted files q to quit, : to select the current file, a to select all files C to copy the selected files, c to copy the current file恢複所有檔案
按a鍵選中所有檔案,再按C鍵以複製檔案。
TestDisk 7.1, Data Recovery Utility, July 2019 Christophe GRENIER <grenier@cgsecurity.org> https://www.cgsecurity.org P Linux filesys. data 415744 83886039 83470296 [root] Directory / >dr-xr-xr-x 0 0 4096 20-Dec-2024 10:06 . dr-xr-xr-x 0 0 4096 20-Dec-2024 10:06 .. *drwxr-xr-x 0 0 4096 20-Dec-2024 10:06 usr *dr-xr-xr-x 0 0 4096 20-Dec-2024 10:03 boot *drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 dev *drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 proc *drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 run *drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 sys *drwxr-xr-x 0 0 0 20-Dec-2024 10:06 etc *-rw------- 0 0 0 20-Dec-2024 10:06 root *drwxr-xr-x 0 0 4096 20-Dec-2024 10:06 var *drwxr-xr-x 0 0 4096 20-Dec-2024 10:06 usr *lrwxrwxrwx 0 0 7 9-Feb-2022 20:18 bin *lrwxrwxrwx 0 0 8 9-Feb-2022 20:18 sbin *lrwxrwxrwx 0 0 7 9-Feb-2022 20:18 lib *lrwxrwxrwx 0 0 9 9-Feb-2022 20:18 lib64 *lrwxrwxrwx 0 0 7 9-Feb-2022 20:18 bin;672827fb *drwxr-xr-x 0 0 0 20-Dec-2024 10:03 home *-rw------- 0 0 0 20-Dec-2024 09:43 .ismount-test-file *drwxr-xr-x 0 0 0 20-Dec-2024 10:03 media *drwxr-xr-x 0 0 0 20-Dec-2024 10:03 mnt *drwxr-xr-x 0 0 0 20-Dec-2024 10:03 opt *lrwxrwxrwx 0 0 8 9-Feb-2022 20:18 sbin;672827fb *drwxr-xr-x 0 0 0 20-Dec-2024 10:04 srv *drwxrwxrwt 0 0 4096 20-Dec-2024 10:06 tmp Next Use Right to change directory, h to hide deleted files q to quit, : to select the current file, a to select all files C to copy the selected files, c to copy the current file -
選擇目標目錄後,按C鍵將選定檔案複製至目標目錄。在本樣本中,以恢複至/home/ecs-user/data_recovery為例。
TestDisk 7.1, Data Recovery Utility, July 2019 Please select a destination where /root and any files below will be copied. Keys: Arrow keys to select another directory C when the destination is correct Q to quit Directory /home/ecs-user/data_recovery >drwxrwxr-x 1000 1000 4096 18-Dec-2024 18:05 . drwx------ 1000 1000 4096 18-Dec-2024 18:23 ..看到
Copy done! 10 ok, 0 failed,表示複製成功。TestDisk 7.1, Data Recovery Utility, July 2019 Christophe GRENIER <grenier@cgsecurity.org> https://www.cgsecurity.org Linux filesys. data 415744 83886039 83470296 [Root] Directory /root Copy done! 10 ok, 0 failed dr-xr-xr-x 0 0 4096 18-Dec-2024 11:14 . dr-xr-xr-x 0 0 4096 18-Dec-2024 11:14 .. drwx------ 0 0 0 18-Dec-2024 11:13 lost+found dr-xr-xr-x 0 0 4096 18-Dec-2024 11:13 boot drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 dev drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 proc drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 run drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 sys drwxr-xr-x 0 0 0 18-Dec-2024 11:15 etc dr-xr-x--- 0 0 4096 18-Dec-2024 11:14 root drwxr-xr-x 0 0 4096 4-Nov-2024 10:03 var drwxr-xr-x 0 0 4096 4-Nov-2024 09:48 usr lrwxrwxrwx 0 0 7 9-Feb-2022 20:18 bin lrwxrwxrwx 0 0 8 9-Feb-2022 20:18 sbin Next Use Right to change directory, h to hide deleted files St q to quit, : to select the current file, a to select all files C to copy the selected files, c to copy the current file -
重新為現有執行個體開啟一個新的串連,切換到/home/ecs-user/data_recovery目錄查看。如果您能看到檔案,說明檔案恢複成功。
[ecs-user@ root]$ pwd /home/ecs-user/data_recovery/root [ecs-user@ root]$ ls -la total 20 drwxr-xr-x 4 root root 4096 Dec 18 11:14 . drwxrwxr-x 3 ecs-user ecs-user 4096 Dec 18 18:26 .. -rw-r--r-- 1 root root 0 Nov 4 10:06 anaconda-ks.cfg -rw-r--r-- 1 root root 0 Dec 18 11:14 .bash_logout -rw-r--r-- 1 root root 0 Dec 18 11:14 .bash_profile -rw-r--r-- 1 root root 0 Dec 18 11:14 .bashrc -rw-r--r-- 1 root root 0 Dec 18 11:14 .cshrc -rw-r--r-- 1 root root 102 Nov 4 10:06 original-ks.cfg drwxr-xr-x 2 root root 4096 Dec 18 11:14 .pip -rw-r--r-- 1 root root 0 Dec 18 11:14 .pydistutils.cfg -rw-r--r-- 1 root root 0 Dec 18 11:15 script -rw-r--r-- 1 root root 0 Dec 18 11:14 sedZsHuoR drwxr-xr-x 2 root root 4096 Dec 18 11:14 .ssh -rw-r--r-- 1 root root 0 Dec 18 11:14 .tcshrc
-
相關文檔
-
關於建立快照的操作,請參見手動建立單個快照。
-
關於卸載及掛載資料盤的操作,請參見卸載或掛載系統硬碟。
-
關於重新初始化系統硬碟的操作,請參見重新初始化系統硬碟(重設作業系統)。
-
關於使用快照建立雲端硬碟的操作,請參見使用快照建立雲端硬碟。
-
關於使用VNC登入執行個體的操作,請參見通過VNC串連執行個體。
-
關於使用SSH協議登入Linux執行個體的操作,請參見使用Workbench登入Linux執行個體。
-
關於Linux執行個體中的資料恢複工具的介紹,請參見Linux執行個體資料恢複工具介紹。