Linux实例数据恢复最佳实践

更新时间:
复制 MD 格式

在使用Linux实例的过程中,删除文件是一项非常常见的运维操作。及时清理不再使用的文件可以有效降低磁盘占用。然而,如果由于人为误操作、程序运行异常、计算机病毒攻击等原因误删除了本应保留的文件,您可能需要进行数据恢复。本文将为您介绍Linux实例中误删数据后的数据恢复整体思路及操作示例。

背景介绍

本文所介绍的数据恢复思路与工具,旨在为误删文件的用户提供尽可能全面的恢复方案。文件恢复的程度将根据用户的实际情况及操作方式有所不同。因此,在执行操作之前,建议您结合自身的实际情况,采取最合理的数据恢复措施,最大限度地保障数据的恢复。

前提条件

  • 在进行数据恢复操作之前,请您仔细阅读数据恢复的整体思路

  • 数据恢复涉及到较多专业知识,请确保您具备相应的知识与技能,以避免对待恢复的数据造成损害。

  • 在进行数据恢复操作之前,强烈建议您为待恢复数据的磁盘创建快照,以避免在恢复过程中因操作失误而对待恢复数据造成损坏。

警告
  • 本文所介绍的数据恢复工具为第三方开源软件,阿里云对文件恢复程度不作任何承诺。

  • 如您需要恢复的数据极其重要,而您在数据恢复方面并不具备相关的专业知识,建议您不要随意按照本教程进行操作。您可以考虑寻求专业的数据恢复机构进行处理。

整体思路

误删文件导致系统无法启动的数据恢复整体思路

当您以root用户的身份在Linux实例上执行了rm -rf /*命令后,您的整个根目录下的文件会被删除,此时操作系统启动后会进入grub命令行界面,如您需要恢复原系统盘上的数据,那么您可以参考如下处理思路,以完成数据恢复操作。

image

误删系统文件后的数据恢复整体思路

当您在Linux实例上执行了rm -rf <file_or_dir>命令后,您的文件或目录会被删除,如您需要恢复原系统盘上的数据,那么您可以参考如下处理思路,以完成数据恢复操作。

说明

如果您误删的文件导致您无法使用SSH协议连接Linux实例,那么您可以采用VNC的方式连接实例。

image

操作指南

上述内容介绍了两种常见的Linux实例误删数据后的数据恢复思路。接下来,将以下述3个条件为例,详细阐述数据恢复的操作步骤。

问题场景

  • 假设您执行了rm -rf /*命令,导致文件误删,从而使系统无法启动。

  • 在执行删除操作之前,未创建快照,或不希望通过快照进行数据恢复。

  • 您希望通过另一台Linux实例进行数据恢复操作。

操作概览

以上问题场景的操作流程如下所示。

image

操作步骤

  1. 为当前实例创建快照

    在执行数据恢复之前,建议您先对需要执行数据恢复操作的实例系统盘或数据盘创建快照,以实现通过快照对现有数据进行备份,防止后续恢复操作过程中误操作损坏文件。关于创建快照的操作,请参见手动创建单个快照

  2. 卸载要恢复数据的系统盘或数据盘,并挂载到新实例。

    警告

    在卸载系统盘时需要先进行关机操作,在控制台操作关机时请务必勾选强制停止选项,否则会导致关机操作失败。

    强制停止等同于断电处理,可能丢失实例操作系统中未写入磁盘的数据,请谨慎选择。

    通过卸载系统盘或数据盘,并将其挂载到另一台Linux实例,以便完成后续数据恢复操作,关于卸载及挂载数据盘的操作,请参见卸载或挂载系统盘

    警告

    在控制台操作将原系统盘作为数据盘挂载到新实例时,由于系统盘文件系统可能已经损坏,因此控制台会提示选择初始化方式。在此情况下,请勿选择立即操作,如选择通过控制台重新初始化该磁盘,可能会对即将恢复的数据造成损害。请选择关闭该对话框即可。

  3. 恢复误删的数据

    通过使用testdisk工具,可以完成已删除数据的恢复操作。以下将以在Linux实例中恢复误删目录为例,介绍数据恢复的操作步骤。

    1. 运行以下命令,在新实例上安装testdisk工具。

      Alibaba Cloud Linux 3/2

      sudo yum install -y testdisk

      CentOS 6/7/8

      sudo yum install -y testdisk

      Ubuntu/Debian

      sudo apt install -y testdisk
    2. 运行以下命令,以确认当前磁盘分区情况。

      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为原实例系统盘,作为数据盘挂载在现有实例中。

    3. 运行以下命令,使用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.
    4. 选择分区表类型进行扫描。一般选择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
    5. 选择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.
    6. 选择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
    7. 选择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
    8. 等待搜索完成后,会列出可恢复的文件。选择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
    9. 选择目标目录后,按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
    10. 重新为现有实例打开一个新的连接,切换到/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

相关文档