在 Linux 中,使用删除命令
rm
应谨慎。有时重要文件会因为误操作而被删除。这个时候,不要太紧张。如果你操作得当,你仍然可以恢复。
EXT 类型文件恢复
删除文件实际上并不会清除inode节点和块数据,而是删除文件父目录下块中的文件名。Linux通过链接数控制文件删除,只有当文件没有链接时才会删除文件。
当然,我这里指的是完全删除,也就是无法通过回收站找回的情况,比如使用rm-rf删除数据。对于Linux下的EXT文件系统,可用的恢复工具有debugfs、ext3grep、extundelete等。extundelete 是一个开源的 Linux 数据恢复工具,支持 ext3 和 ext4 文件系统。
误删数据后,首先要做的就是卸载被删除数据所在的分区。如果根分区的数据被误删,需要进入系统进入单用户模式,并以只读模式挂载根分区。这样做的原因很简单,因为删除文件后,只清除了文件inode节点中的扇区指针,实际文件也存储在磁盘上。如果磁盘继续以读写方式挂载,则被删除文件的数据块可能会被操作系统重新分配,这些数据库将是新的。数据覆盖后,这些数据真的就丢失了,恢复工具也无能为力了。因此,以只读方式挂载磁盘可以将数据库中数据覆盖的风险降到最低。
演示
在编译安装extundelete之前,需要安装两个依赖包e2fsprogs-libs和e2fsprogs-devel,它们在系统安装光盘/Package目录下,使用rpm或yum命令安装。E2fsprogs-devel 安装依赖于 libcom_err-devel 包。
1.系统使用RHEL 6.5挂载光盘,安装依赖包。这里使用rpm安装方式。
[root@localhost ~]# mkdir /mnt/cdrom
[root@localhost ~]# mount /dev/cdrom /mnt/cdrom/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# cd /mnt/cdrom/Packages/
[root@localhost Packages]# rpm -ivh e2fsprogs-libs-12-elx86_rpm
warning: e2fsprogs-libs-12-elx86_rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing... ########################################### [100%]
package e2fsprogs-libs-12-elx86_64 is already installed
[root@localhost Packages]# rpm -ivh libcom_err-devel-12-elx86_rpm
warning: libcom_err-devel-12-elx86_rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing... ########################################### [100%]
1:libcom_err-devel ########################################### [100%]
[root@localhost Packages]# rpm -ivh e2fsprogs-devel-12-elx86_rpm
warning: e2fsprogs-devel-12-elx86_rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing... ########################################### [100%]
1:e2fsprogs-devel ########################################### [100%]
2.创建本地yum源并安装编译环境。
[root@localhost ~]# yum install gcc gcc-c++ -y
3.解压 extundelet 包。
[root@localhost ~]# tar jxvf extundelete-tar.bz2 -C ~
extundelete-4/
extundelete-4/acinclude.m4
extundelete-4/missing
extundelete-4/autogen.sh
extundelete-4/aclocal.m4
extundelete-4/configure
extundelete-4/LICENSE
extundelete-4/README
extundelete-4/install-sh
extundelete-4/config.h.in
extundelete-4/src/
extundelete-4/src/extundelete.cc
extundelete-4/src/block.h
extundelete-4/src/kernel-jbd.h
extundelete-4/src/insertionops.cc
extundelete-4/src/block.c
extundelete-4/src/cli.cc
extundelete-4/src/extundelete-priv.h
extundelete-4/src/extundelete.h
extundelete-4/src/jfs_compat.h
extundelete-4/src/Makefile.in
extundelete-4/src/Makefile.am
extundelete-4/configure.ac
extundelete-4/depcomp
extundelete-4/Makefile.in
extundelete-4/Makefile.am
4.配置、编译和安装 extundelete 包
[root@localhost ~]# cd extundelete-4
[root@localhost extundelete-4]# ls
acinclude.m4 aclocal.m4 autogen.sh config.h.in configure configure.ac depcomp install-sh LICENSE Makefile.am Makefile.in missing README src
[root@localhost extundelete-4]# ./configure
Configuring extundelete 4
Writing genera、ted files to disk
[root@localhost extundelete-4]# make
make -s all-recursive
Making all in src
extundelete.cc:571: Warning: Unused parameters'flags'
[root@localhost extundelete-4]# make install
Making install in src
/usr/bin/install -c extundelete '/usr/local/bin'
5.准备测试分区,/dev/sdb1为ext4格式,挂载在/mnt/ext4目录下。
[root@localhost ~]# mkdir /mnt/ext4
[root@localhost ~]# mount /dev/sdb1 /mnt/ext4/
[root@localhost ~]# df -hT /mnt/ext4/
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb1 ext4 20G 172M 19G 1% /mnt/ext4
6.创建测试文件。
[root@localhost ~]# cd /mnt/ext4/
[root@localhost ext4]# echo 1 a
[root@localhost ext4]# echo 2 b
[root@localhost ext4]# echo 3 c
[root@localhost ext4]# ls
a b c lost+found
7.删除测试文件。
[root@localhost ext4]# rm -f a b
[root@localhost ext4]# ls
c lost+found
8.卸载对应的分区。
[root@localhost ext4]# cd
[root@localhost ~]# umount /mnt/ext4/
9.恢复已删除的内容。
[root@localhost ~]# extundelete /dev/sdb1 --restore-all
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 160 groups loaded.
Loading journal descriptors ... 24 descriptors loaded.
Searching for recoverable inodes in directory / ...
2 recoverable inodes found.
Looking through the directory structure for deleted files ...
0 recoverable inodes still lost.
10.恢复的文件将在当前目录的 RECOVERED_FILES 文件夹中。
[root@localhost ~]# ls RECOVERED_FILES/
a b
XFS 类型文件备份和恢复
extundelete 工具只能恢复 EXT 类型的文件,不能恢复CentOS 7系统默认使用的 xfs 类型的文件。xfs文件系统目前没有成熟的文件恢复工具,建议提前做好数据备份,避免数据丢失。
可以使用 xfsdump 和 xfsrestore 工具备份和恢复 xfs 类型的文件。如果系统中没有安装 xfsdump 和 xfsrestore 工具,可以通过
yum install -y xfsdump
命令安装。Xfsdump 按 inode 顺序备份 xfs 文件系统。
xfsdump 有两个备份级别:0 表示完全备份,1-9 表示增量备份。默认值为 0。
Xfsdump-f backup storage location backup path or device file
-f:指定备份文件目录
-L:指定标签会话标签
-M:指定设备标签媒体标签
-s:备份单个文件,-s 不能直接跟随路径。
-
在使用 xfsdump 时,需要注意以下限制:
1.xfsdump不支持不挂载文件系统备份,所以只能备份挂载文件。
2.xfsdump必须使用root权限才能操作(涉及文件系统关系);
3.xfsdump只能备份XFS文件系统;
4.xfsdump(档案或存储介质)备份的数据只能由xfsrestore解析。
5.xfsdump通过文件系统的UUID来区分备份文件,所以不能备份两个UUID相同的文件系统。
The location of xfsrestore-f restore file and the path of restored file
案例演示
1.准备测试分区,/dev/sdb1为ext4格式,挂载在/mnt/ext4目录下。
[root@localhost ~]# mkdir /mnt/xfs
[root@localhost ~]# mount /dev/sdb1 /mnt/xfs/
[root@localhost ~]# df -hT /mnt/xfs/
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb1 xfs 20G 33M 20G 1% /mnt/xfs
2.创建测试文件。
[root@localhost ~]# cd /mnt/xfs/
[root@localhost xfs]# mkdir test
[root@localhost xfs]# touch a.txt
[root@localhost xfs]# touch test/b.txt
3.可以使用tree查看目录结构。
[root@localhost ~]# yum install tree -y
[root@localhost ~]# tree /mnt/xfs/
/mnt/xfs/
├── a.txt
└── test
└── b.txt
1 directory, 2 files
4.使用 xfsdump 命令备份整个分区。
[root@localhost ~]# xfsdump -f /opt/dump_sdb1 /dev/sdb1
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 4 (dump format 0) - type ^C for status and control
dump label dialog
please enter label for this dump session (timeout in 300 sec)
-dump_sdb1 //Specify a backup session label
session label entered: "dump_sdb1"
--------------------------------- end dialog ---------------------------------
xfsdump: level 0 dump of localhost.localdomain:/mnt/xfs
xfsdump: dump date: Fri Sep 6 13:36:12 2019
xfsdump: session id: 74232f85-124c-4486-8d91-f35208534f74
xfsdump: session label: "dump_sdb1"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 21760 bytes
xfsdump: /var/lib/xfsdump/inventory created
media label dialog
please enter label for media in drive 0 (timeout in 300 sec)
-sdb1 //Specifying device labels is a description of the device to be backed up.
media label entered: "sdb1"
--------------------------------- end dialog ---------------------------------
xfsdump: creating dump session media file 0 (media 0, file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 22952 bytes
xfsdump: dump size (non-dir files) : 0 bytes
xfsdump: dump complete: 46 seconds elapsed
xfsdump: Dump Summary:
xfsdump: stream 0 /opt/dump_sdb1 OK (success)
xfsdump: Dump Status: SUCCESS
5.查看备份信息和内容。
[root@localhost ~]# xfsdump -I
file system 0:
fs id: f8805a3e-089e-4875-ad54-d31e5dc98835
session 0:
mount point: localhost.localdomain:/mnt/xfs
device: localhost.localdomain:/dev/sdb1
time: Fri Sep 6 13:36:12 2019
session label: "dump_sdb1"
session id: 74232f85-124c-4486-8d91-f35208534f74
level: 0
resumed: NO
subtree: NO
streams: 1
stream 0:
pathname: /opt/dump_sdb1
start: ino 68 offset 0
end: ino 70 offset 0
interrupted: NO
media files: 1
media file 0:
mfile index: 0
mfile type: data
mfile size: 22952
mfile start: ino 68 offset 0
mfile end: ino 70 offset 0
media label: "sdb1"
media id: cc32446f-42e8-489b-867f-84a55949c1fa
xfsdump: Dump Status: SUCCESS
6.删除创建的测试文件,模拟数据丢失。
[root@localhost ~]# rm -rf /mnt/xfs/*
[root@localhost ~]# tree /mnt/xfs/
/mnt/xfs/
0 directories, 0 files
7.丢失文件的恢复。
[root@localhost ~]# xfsrestore -f /opt/dump_sdb1 /mnt/xfs/
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 4 (dump format 0) - type ^C for status and control
xfsrestore: searching media for dump
xfsrestore: examining media file 0
xfsrestore: dump description:
xfsrestore: hostname: localhost.localdomain
xfsrestore: mount point: /mnt/xfs
xfsrestore: volume: /dev/sdb1
xfsrestore: session time: Fri Sep 6 13:36:12 2019
xfsrestore: level: 0
xfsrestore: session label: "dump_sdb1"
xfsrestore: media label: "sdb1"
xfsrestore: file system id: f8805a3e-089e-4875-ad54-d31e5dc98835
xfsrestore: session id: 74232f85-124c-4486-8d91-f35208534f74
xfsrestore: media id: cc32446f-42e8-489b-867f-84a55949c1fa
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 2 directories and 3 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore: stream 0 /opt/dump_sdb1 OK (success)
xfsrestore: Restore Status: SUCCESS
[root@localhost ~]# tree /mnt/xfs/
/mnt/xfs/
├── a.txt
└── test
└── b.txt
1 directory, 2 files
标签:数据恢复,EXT4,mnt,extundelete,xfsrestore,xfsdump,XFS,root,localhost
From: https://www.cnblogs.com/Alwayslearn/p/16719473.html