Khôi phục tập tin bị xóa trên phân vùng Ext3 Linux

Tác giả: 06/10/2022

1. Giới thiệu về ext3grep

ext3grep là một công cụ hỗ trợ chúng ta trong việc khôi phục các tập tin trên filesystem ext3. Công cụ này sẽ hiển thị thông tin về các file tồn tại trên một phân vùng và có thể phục hồi các file chúng ta đã xóa

2. Cài đặt và sử dụng ext3grep

Để có thể khôi phục các tệp bị xóa chúng ta cần phải cài đặt công cụ ext3grep trên hệ thống Ubuntu hoặc Debian như sau:

apt install ext3grep

Sau khi cài đặt xong kiểm tra ext3grep đã được cài chưa như sau:

ext3grep -v

Sau khi đã cài đặt thành công chúng ta có thể khôi phục các file trên filesystem ext3

Đầu tiên chúng ta cần copy một đĩa bằng lệnh dd có kích thước 300M và chúng ta cần định dạng phân vùng là ext3 để sử dụng:

root@tubt:~# dd if=/dev/vda3 of=/tubt bs=1M count=300
300+0 records in
300+0 records out
314572800 bytes (315 MB, 300 MiB) copied, 1.08539 s, 290 MB/s

Kết quả của lệnh trên, chúng ta đã copy một ổ đĩa có tên tubt. List để kiểm tra kết quả:

root@tubt:~# ls -l / | grep tubt
-rw-r–r– 1 root root 314572800 Oct 5 10:02 tubt

Tiếp theo chúng ta cần định dạng phân vùng dạng filesystem ext3 để sử dụng. Lệnh sau để định dạng phân vùng tubt với ext3

root@tubt:~# mkfs.ext3 /tubt
mke2fs 1.45.5 (07-Jan-2020)
/tubt contains a ext4 file system
last mounted on / on Wed Oct 5 08:52:03 2022
Proceed anyway? (y,N) y
Discarding device blocks: done
Creating filesystem with 76800 4k blocks and 76800 inodes
Filesystem UUID: d942720e-500d-4240-88fe-9bb6c8ad230e
Superblock backups stored on blocks:
32768

Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

Sau khi định dạng phân vùng. Phân vùng mới đã có thể lưu trữ dữ liệu. Chúng ta phải gắn phân vùng vào thư mục. Thực hiện tạo thư mục /mnt/test_ext3grep như sau:

mkdir /mnt/test_ext3grep

Thực hiện mount /mnt/test_ext3grep vào /tubt bằng lệnh sau:

mount -t ext3 /tubt /mnt/test_ext3grep/

Kiểm tra dung lượng disk:

df -hT | grep test_ext3grep

root@tubt:~# df -hT | grep test_ext3grep
/dev/loop5 ext3 275M 44K 260M 1% /mnt/test_ext3grep

Tiếp theo chúng ta cần thực hiện tạo file trong thư mục test_ext3grep đã được mount vào /tubt sử dụng filesystem ext3:

root@tubt:~# cd /mnt/test_ext3grep/
root@tubt:/mnt/test_ext3grep# touch test{1..5}.txt
root@tubt:/mnt/test_ext3grep# ll
total 24
drwxr-xr-x 3 root root 4096 Oct 6 02:00 ./
drwxr-xr-x 3 root root 4096 Oct 6 01:22 ../
drwx—— 2 root root 16384 Oct 5 10:08 lost+found/
-rw-r–r– 1 root root 0 Oct 6 02:00 test1.txt
-rw-r–r– 1 root root 0 Oct 6 02:00 test2.txt
-rw-r–r– 1 root root 0 Oct 6 02:00 test3.txt
-rw-r–r– 1 root root 0 Oct 6 02:00 test4.txt
-rw-r–r– 1 root root 0 Oct 6 02:00 test5.txt
root@tubt:/mnt/test_ext3grep# echo “TEST1” > test1.txt
root@tubt:/mnt/test_ext3grep# cat test1.txt
TEST1

Bây giờ chúng ta thực hiện xóa file test1.txt từ thư mục /mnt/test_ext3grep:

rm -rf test1.txt

3. Khôi phục dữ liệu sử dụng ext3grep

Chúng ta cần khôi phục lại file test1.txt đã xóa ở trên bằng ext3grep

Chúng ta thực hiện unmount bằng lệnh:

root@tubt:/mnt/test_ext3grep# cd
root@tubt:~# umount /mnt/test_ext3grep/

Để xem tất cả các file tồn tại trong /tubt chúng ta thực thi lệnh ext3grep cùng với tùy chọn --dump-name như bên dưới:

root@tubt:~# ext3grep –dump-name /tubt
Running ext3grep version 0.10.2
WARNING: I don’t know what EXT3_FEATURE_COMPAT_EXT_ATTR is.
Number of groups: 3
Minimum / maximum journal block: 828 / 4929
Loading journal descriptors… sorting… done
The oldest inode block that is still in the journal, appears to be from 1665021497 = Thu Oct 6 01:58:17 2022
Number of descriptors in journal: 48; min / max sequence numbers: 2 / 10
Finding all blocks that might be directories.
D: block containing directory start, d: block containing more directory entries.
Each plus represents a directory start that references the same inode as a directory start that we found previously.

Searching group 0: DD++++++
Searching group 1:
Searching group 2:
Writing analysis so far to ‘tubt.ext3grep.stage1’. Delete that file if you want to do this stage again.
Result of stage one:
2 inodes are referenced by one or more directory blocks, 2 of those inodes are still allocated.
1 inodes are referenced by more than one directory block, 1 of those inodes is still allocated.
0 blocks contain an extended directory.
Result of stage two:
2 of those inodes could be resolved because they are still allocated.
All directory inodes are accounted for!

Writing analysis so far to ‘tubt.ext3grep.stage2’. Delete that file if you want to do this stage again.
lost+found
test1.txt
test2.txt
test3.txt
test4.txt
test5.txt

Để khôi phục file test1.txt, chúng ta sử dụng tùy chọn --restore-all kết hợp với lệnh ext3grep như bên dưới:

root@tubt:~# ext3grep –restore-all /tubt
Running ext3grep version 0.10.2
WARNING: I don’t know what EXT3_FEATURE_COMPAT_EXT_ATTR is.
Number of groups: 3
Minimum / maximum journal block: 828 / 4929
Loading journal descriptors… sorting… done
The oldest inode block that is still in the journal, appears to be from 1665021497 = Thu Oct 6 01:58:17 2022
Number of descriptors in journal: 48; min / max sequence numbers: 2 / 10
Writing output to directory RESTORED_FILES/
Loading tubt.ext3grep.stage2… done
Restoring test1.txt
Restoring test2.txt
Restoring test3.txt
Restoring test4.txt
Restoring test5.txt

Sau khi khôi phục hoàn tất thì tất cả các file đã được khôi phục sẽ nằm ở thư mục RESTORED_FILES.

root@tubt:~# cd RESTORED_FILES/
root@tubt:~/RESTORED_FILES# ll
total 16
drwxr-xr-x 3 root root 4096 Oct 6 02:10 ./
drwx—— 6 root root 4096 Oct 6 02:10 ../
drwx—— 2 root root 4096 Oct 5 10:08 lost+found/
-rw-r–r– 1 root root 6 Oct 6 02:00 test1.txt
-rw-r–r– 1 root root 0 Oct 6 01:59 test2.txt
-rw-r–r– 1 root root 0 Oct 6 01:59 test3.txt
-rw-r–r– 1 root root 0 Oct 6 01:59 test4.txt
-rw-r–r– 1 root root 0 Oct 6 01:59 test5.txt
root@tubt:~/RESTORED_FILES# cat test1.txt
TEST1

Như vậy chúng ta đã khôi phục thành công file đã xóa trên ổ đĩa sử dụng filesystem ext3.

 

4. Lời kết

Qua bài viết trên, Nhân Hòa đã hướng dẫn các bạn khôi phục dữ liệu trên ổ đĩa sử dụng filesystem ext3 của hệ điều hành Linux bằng công cụ ext3grep.

Chúc các bạn thành công và hẹn gặp lại trong các bài viết tiếp theo tại wiki.nhanhoa.com

Trả lời