在linux系统中,我们有时会“不小心”误删除一些文件,如果是自己是测试环境服务器可能“无所谓”。但是一旦发生在客户的生产环境,那就是“重大安全事故”。
我们能不能提前对一些重要的文件系统进行备份,以便当我们真的误删除一些重要的文件时,能够及时恢复呢?本次为大家讲解如何使用xfsdump和xfsrestore命令实现RHEL7 xfs文件系统误删除文件的恢复。
1.实验环境
1.VMWare RHEL7.7
命令:
cat /etc/redhat-release
2.实施步骤
1.虚拟机关机
2.新增一块硬盘大小20G
3.虚拟机开机并查看新增磁盘信息
命令:
lsblk |grep "sdb"
4.对新增磁盘进行分区,大小5GB
命令:
echo -e "o\nn\np\n1\n\n+5G\nw" | fdisk /dev/sdb
lsblk |grep "sdb"
5.格式化磁盘分区并挂载
命令:
mkfs.xfs -f /dev/sdb1
mkdir -p /tmp/sdb1
mount /dev/sdb1 /tmp/sdb1
6.创建空文件,空目录,普通文件,普通目录
命令:
cd /tmp/sdb1
mkdir a
mkdir b
touch aa
touch bb
touch kong.txt
echo "aaa" >>aa
echo "bbb" >>bb
mv bb b/
7.使用xfsdump命令备份/dev/sdb1分区中的数据至/root/back目录
前提:如果xfsdump未安装,请安装;yum源可用
yum -install -y xfsdump
命令:
mkdir -p /root/back
xfsdump -f /root/back/dump_sdb1 /dev/sdb1
备注:dump_sdb1为自动定义备份文件名称
[root@rhel77 back]# xfsdump -f /root/back/dump_sdb1 /dev/sdb1 xfsdump: using file dump (drive_simple) strategy xfsdump: version 3.1.7 (dump format 3.0) - type ^C for status and control ============================= dump label dialog ============================== please enter label for this dump session (timeout in 300 sec)-> dump_sdb1 #label标签名,可以为空 session label entered: "dump_sdb1" --------------------------------- end dialog --------------------------------- xfsdump: level 0 dump of rhel77:/tmp/sdb1 xfsdump: dump date: Sun May 28 19:23:11 2023 xfsdump: session id: b0498d15-755e-4e33-8207-d47830a84bde 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: 30592 bytes ============================= media label dialog ============================= please enter label for media in drive 0 (timeout in 300 sec)-> sdb1 media label entered: "sdb1" #media标签名,可以为空 --------------------------------- 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 23656 bytes xfsdump: dump size (non-dir files) : 1088 bytes xfsdump: dump complete: 12 seconds elapsed xfsdump: Dump Summary: xfsdump: stream 0 /root/back/dump_sdb1 OK (success) xfsdump: Dump Status: SUCCESS [root@rhel77 back]# ls #查看备份后的信息 dump_sdb1 [root@rhel77 back]#
8.模拟删除/tmp/sdb1(/dev/sdb1)下的文件
命令:
cd /tmp/sdb1
rm -rf *
ls
9.执行恢复
命令:
cd /tmp/sdb1
xfsrestore -f /root/back/dump_sdb1 /tmp/sdb1/
ls
tree
备注:dump_sdb1为xfsdump命令备份的文件名称
[root@rhel77 sdb1]# cd /tmp/sdb1 [root@rhel77 sdb1]# xfsrestore -f /root/back/dump_sdb1 /tmp/sdb1/ xfsrestore: using file dump (drive_simple) strategy xfsrestore: version 3.1.7 (dump format 3.0) - type ^C for status and control xfsrestore: searching media for dump xfsrestore: examining media file 0 xfsrestore: dump description: xfsrestore: hostname: rhel77 xfsrestore: mount point: /tmp/sdb1 xfsrestore: volume: /dev/sdb1 xfsrestore: session time: Sun May 28 19:23:11 2023 xfsrestore: level: 0 xfsrestore: session label: "dump_sdb1" xfsrestore: media label: "sdb1" xfsrestore: file system id: f41c9297-c680-4555-887b-673d47c5693e xfsrestore: session id: b0498d15-755e-4e33-8207-d47830a84bde xfsrestore: media id: 0d886f27-6182-408d-89a1-ee0cf1265645 xfsrestore: using online session inventory xfsrestore: searching media for directory dump xfsrestore: reading directories xfsrestore: 3 directories and 5 entries processed xfsrestore: directory post-processing xfsrestore: restoring non-directory files xfsrestore: restore complete: 0 seconds elapsed xfsrestore: Restore Summary: xfsrestore: stream 0 /root/back/dump_sdb1 OK (success) xfsrestore: Restore Status: SUCCESS [root@rhel77 sdb1]# ls a aa b kong.txt [root@rhel77 sdb1]# tree . ├── a ├── aa ├── b │ └── bb └── kong.txt 2 directories, 3 files [root@rhel77 sdb1]#
至此,被删除的文件和目录恢复成功!
备注:
1.xfsdump备份和xfsrestore恢复磁盘的前提是磁盘分区始终要处于挂载状态
2.xfsdump -f /root/back/dump_sdb1 /dev/sdb1。
xfsdump -f $destination $source
xfsdump命令备份时,$source只可以使用/dev/sdb1磁盘分区,不可以使用 /tmp/sdb1(/dev/sdb1磁盘分区的挂载点)
3.xfsrestore能够恢复为空的文件和目录
4.xfsrestore -f /root/back/dump_sdb1 /tmp/sdb1
xfsrestore -f $source $destination
xfsrestore命令恢复时,$destination既可以是/tmp/sdb1(/dev/sdb1磁盘分区的挂载点),也可以是其它目录,但不能直接使用/dev/sdb1磁盘分区名称