Centos7 xfs与ext4文件备份与恢复

一、XFS文件备份与恢复

1、下载安装xfsdump恢复工具(如果最小安装没有的话)

[root@localhost ~]# yum -y install xfsdump

2、首先使用fdisk -l命令查看硬盘及分区,这里我们就使用sdb这块硬盘模拟数据备份与恢复

[root@localhost ~]# fdisk -l

磁盘 /dev/sda:322.1 GB, 322122547200 字节,629145600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b9fa2

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    10227711     4064256   82  Linux swap / Solaris
/dev/sda3        10227712   115085311    52428800   83  Linux
/dev/sda4       115085312   629145599   257030144    5  Extended
/dev/sda5       115087360   629145599   257029120   83  Linux

磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

3、使用fdisk命令对磁盘进行分区

[root@localhost ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0x29fd492e 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-41943039,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):
将使用默认值 41943039
分区 1 已设置为 Linux 类型,大小设为 20 GiB

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。

4、对刚创建的分区sdb1进行格式化

[root@localhost ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=1310656 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5242624, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

4、将格式化完的磁盘挂载到/mnt目录下

[root@localhost ~]# mount /dev/sdb1 /mnt    ##将分区挂载到/mnt目录下
[root@localhost ~]# cd /mnt   ##进入/mnt目录下
[root@localhost mnt]# [root@localhost mnt]# touch 1.txt | echo "123456789" > 1.txt    ##创建一个文件模拟磁盘下的数据

5、使用xfsdump工具进行备份

[root@localhost mnt]# xfsdump -f /opt/xfs_dump /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)
 -> xfs_dump              ##设备备份文件位置
session label entered: "xfs_dump"

 --------------------------------- end dialog ---------------------------------

xfsdump: level 0 dump of localhost.localdomain:/mnt
xfsdump: dump date: Thu Jul  2 00:19:39 2020
xfsdump: session id: ee0ebfa4-3b8c-4f6b-8099-9ff42715c3c2
xfsdump: session label: "xfs_dump"
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: 25216 bytes
xfsdump: /var/lib/xfsdump/inventory created

 ============================= media label dialog =============================

please enter label for media in drive 0 (timeout in 300 sec)
 -> sdb1         ##设置备份文件设备
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 22208 bytes
xfsdump: dump size (non-dir files) : 544 bytes
xfsdump: dump complete: 29 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /opt/xfs_dump OK (success)
xfsdump: Dump Status: SUCCESS

6、备份完成后,模拟误删除,将/mnt目录下所有文件删除

[root@localhost /]# rm -rf /mnt/*

7、使用xfsrestore命令恢复删除的文件到/mnt目录下

[root@localhost /]# xfsrestore -f /opt/xfs_dump /mnt
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: localhost.localdomain
xfsrestore: mount point: /mnt
xfsrestore: volume: /dev/sdb1
xfsrestore: session time: Thu Jul  2 00:19:39 2020
xfsrestore: level: 0
xfsrestore: session label: "xfs_dump"
xfsrestore: media label: "sdb1"
xfsrestore: file system id: 68b91dcc-26a5-4ac8-ad59-6ec48b9d6dcd
xfsrestore: session id: ee0ebfa4-3b8c-4f6b-8099-9ff42715c3c2
xfsrestore: media id: 9d3d1158-bb66-4065-9998-b57a5c537ea7
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 1 directories and 1 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /opt/xfs_dump OK (success)
xfsrestore: Restore Status: SUCCESS

8、恢复成功,进入/mnt目录下查看原文件是否存在

[root@localhost /]# cd /mnt
[root@localhost mnt]# ll
总用量 4
-rw-r--r--. 1 root root 10 7月   2 00:18 1.txt
[root@localhost mnt]# cat 1.txt 
123456789

文件恢复成功!

二、EXT3/4文件恢复

1、查看一下系统是否挂载了光盘

我这里已经挂载了CD-ROM,如果没有挂载可以使用 mount /dev/cdrom /mnt 命令进行挂载

[root@localhost Packages]# df -Th
文件系统       类型      容量  已用  可用 已用% 挂载点
/dev/sda3      xfs        50G  1.1G   49G    3% /
devtmpfs       devtmpfs  1.9G     0  1.9G    0% /dev
tmpfs          tmpfs     1.9G     0  1.9G    0% /dev/shm
tmpfs          tmpfs     1.9G   12M  1.9G    1% /run
tmpfs          tmpfs     1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda5      xfs       246G   33M  245G    1% /home
/dev/sda1      xfs      1014M  138M  877M   14% /boot
tmpfs          tmpfs     378M     0  378M    0% /run/user/0
/dev/sr0       iso9660   4.3G  4.3G     0  100% /mnt

2、安装依赖包

[root@localhost Packages]# rpm -ivh e2fsprogs-libs-1.42.9-13.el7.x86_64.rpm 
准备中...                          ################################# [100%]
	软件包 e2fsprogs-libs-1.42.9-13.el7.x86_64 已经安装
	
[root@localhost Packages]# rpm -ivh e2fsprogs-devel-1.42.9-13.el7.x86_64.rpm 
错误:依赖检测失败:
	libcom_err-devel(x86-64) = 1.42.9-13.el7 被 e2fsprogs-devel-1.42.9-13.el7.x86_64 需要
	pkgconfig(com_err) 被 e2fsprogs-devel-1.42.9-13.el7.x86_64 需要
	
##在安装第二个包时提示安装失败,需要先安装依赖包

[root@localhost Packages]# rpm -ivh libcom_err-devel-1.42.9-13.el7.x86_64.rpm 
准备中...                          ################################# [100%]
正在升级/安装...
   1:libcom_err-devel-1.42.9-13.el7   ################################# [100%]

##依赖包安装完成后再回头安装之前报错的那个包

[root@localhost Packages]# rpm -ivh libcom_err-devel-1.42.9-13.el7.x86_64.rpm 
准备中...                          ################################# [100%]
正在升级/安装...
   1:libcom_err-devel-1.42.9-13.el7   ################################# [100%]
[root@localhost Packages]# rpm -ivh e2fsprogs-devel-1.42.9-13.el7.x86_64.rpm 
准备中...                          ################################# [100%]
正在升级/安装...
   1:e2fsprogs-devel-1.42.9-13.el7    ################################# [100%]


3、下载软件包并安装

[root@localhost opt]# yum -y install wget   ##安装wget工具
[root@localhost Packages]# cd /opt
[root@localhost opt]# wget http://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2    ##下载软件包
[root@localhost opt]# yum -y install bzip2   ##安装bzip2
[root@localhost opt]# tar xvf extundelete-0.2.4.tar.bz2  ##解压软件包
[root@localhost opt]# cd extundelete-0.2.4
[root@localhost extundelete-0.2.4]# yum -y install gcc gcc-c++ make ##安装依赖环境
[root@localhost extundelete-0.2.4]# ./configure --prefix=/usr/local/extundelete && make && make install    ##开始编译安装
[root@localhost extundelete-0.2.4]# cd /usr/local/extundelete/bin
[root@localhost bin]# ln -s /usr/local/extundelete/bin/* /usr/sbin/

4、在根目录下新建一个data目录,将sdb1分区挂载进去

[root@localhost bin]# cd /
[root@localhost /]# mkdir /data
[root@localhost /]# mount /dev/sdb1 /data
[root@localhost data]# cp /var/log/messages /data   ##将系统日志文件拷贝到data中模拟分区中的文件
[root@localhost data]# rm -rf messages  ##模拟误删除messages
[root@localhost data]# cd ~  #退回到家目录
[root@localhost /]# umount /data  ##解挂载分区
[root@localhost /]# extundelete /dev/sdb1 --restore-all   #进行恢复操作
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 160 groups loaded.
Loading journal descriptors ... 20 descriptors loaded.
Searching for recoverable inodes in directory / ... 
1 recoverable inodes found.
Looking through the directory structure for deleted files ... 
0 recoverable inodes still lost.

#恢复已完成,我们发现当前目录下生成了一个恢复文件夹,cd进入该文件夹
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 1472 6月  18 23:08 anaconda-ks.cfg
drwxr-xr-x. 2 root root   22 7月   2 09:15 RECOVERED_FILES
[root@localhost ~]# cd RECOVERED_FILES/
[root@localhost RECOVERED_FILES]# ll
总用量 48
-rw-r--r--. 1 root root 440538 7月   2 09:15 messages
## 发现文件已经恢复成功了

三、总结

1、extundelete 工具可以针对ext3/4文件系统进行恢复

​ xfsdump 工具可以针对xfs文件系统进行恢复

2、对ext3文件系统进行恢复之前必须要卸载该分区,而对xfs恢复则不需要

3、一旦误删除文件,应当立即解挂载误操作的分区,防止误删数据被覆盖导致无法恢复

猜你喜欢

转载自blog.csdn.net/u014042047/article/details/107136616