最近使用Code Server进行Node项目开发时,不小心误操作导致整个项目源码被删除。Code Server对文件进行删除,不是简单的rm -rf。这里记录一下文件恢复的过程。

尝试方法1 使用extundelete:

1.编译安装(报错):

wget  http://zy-res.oss-cn-hangzhou.aliyuncs.com/server/extundelete-0.2.4.tar.bz2
yum -y install  bzip2  e2fsprogs-devel  e2fsprogs  gcc-c++ make
tar -xvjf extundelete-0.2.4.tar.bz2
cd extundelete-0.2.4 ./configure
make && make install

2.直接安装:

yum install -y extundelete

3.查看帮助

extundelete --help

4.查看分区

[root@centos ~]# df /
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/vda1       61860632 32892240  26351728  56% /

5.查看文件夹inode值

[root@centos ~]# ls -id  /root/coding/PJ3-1/
801537 /root/coding/PJ3-1/

6.尝试恢复

查看vda1分区根目录下可被恢复的文件及文件夹:

extundelete /dev/vda1 --inode 801537
extundelete --restore-directory /root/folder /dev/vda1

其中/root/folder是被删除的文件夹的路径,/dev/vda1是文件系统所在的设备名称。

(恢复无果)

尝试方法2 使用testdisk:

1.直接安装:

yum install -y testdisk

2.运行

[root@centos ~]# testdisk
TestDisk 7.1, Data Recovery Utility, July 2019
Christophe GRENIER <grenier@cgsecurity.org>
https://www.cgsecurity.org

TestDisk 7.1, Data Recovery Utility, July 2019
Christophe GRENIER <grenier@cgsecurity.org>
https://www.cgsecurity.org


TestDisk is free data recovery software designed to help recover lost
partitions and/or make non-booting disks bootable again when these symptoms
are caused by faulty software, certain types of viruses or human error.
It can also be used to repair some filesystem errors.

Information gathered during TestDisk use can be recorded for later
review. If you choose to create the text file, testdisk.log , it
will contain TestDisk options, technical information and various
outputs; including any folder/file names TestDisk was used to find and
list onscreen.

Use arrow keys to select, then press Enter key:
>[ Create ] Create a new log file
 [ Append ] Append information to log file
 [ No Log ] Don't record anything

按照提示依次选择操作:
[ Create ] -> 选择分区 -> [ Proceed ] -> [ Continue ] -> 选择文件系统[Intel] -> [ Analyse ] ->[Quick Search]

进行恢复

在完成[Quick Search]后,按P进入恢复模式,导航到被删除的文件夹:

TestDisk 7.1, Data Recovery Utility, July 2019
Christophe GRENIER <grenier@cgsecurity.org>
https://www.cgsecurity.org
   * Linux                    2   0 33 124830   7 39  125827072
Directory /root/coding

>drwxr-xr-x     0     0     28672 17-Jun-2023 05:04 .
 dr-xr-x---     0     0      4096 17-Jun-2023 18:38 ..
 drwxr-xr-x     0     0      4096 21-Jun-2022 19:59 Demo

红色的就是(曾经)被删除的文件,按C复制当前选择,选择好存放路径后再按一次C就会恢复到指定路径。
但很不幸,在用这个工具恢复时,被删的目录和文件能恢复。但只有0kb,内部都是空白的。

(恢复无果)

尝试方法3 使用lsof:

如果删除文件的进程仍在运行,可以尝试以下方法:

1.查看被删文件

lsof | grep deleted

2.尝试恢复

[root@centos ~]# cd /proc/(pid)/fd/
[root@centos fd]# ll

理论上可以找到对应的文件并使用cp命令进行拷贝,但在这里找不到被删除的文件。

(恢复无果)

尝试方法4 VScode时间线:

VSCode的时间线功能可以记录文件的操作、工作区和Git记录等功能。当文件被修改和保存后,时间线中会出现一个新的节点,表示代码文件的历史版本。可以使用时间线实现回退和比较等功能,类似于Git的版本控制。

在 VSCode 中打开对应文件,时间线功能会记录的当前开启文件的相关操作记录,但如果一个文件被误删除了,那么它在 VSCode 中将无法被显示、打开,所以也就没有所以时间线功能面板了。

以下是不同操作系统下VSCode时间线的文件路径:

#Mac OS 系统   xxx为用户名
/Users/xxx/Library/Application Support/Code/User/History

#Windows 系统  xxx为用户名
C:\Users\xxx\AppData\Roaming\Code\User\History

#Linux 系统 code-server
/root/.local/share/code-server/User/History

每个文件夹表示一个文件的操作记录,可以使用全局搜索功能定位到被删除的文件,并按时间排序找回被删除的文件。
(成功恢复)

最后修改:2023 年 06 月 18 日
如果觉得我的文章对你有用,请随意赞赏