当前位置: 首页 >> Linux >> 【恢复】Linux 文件恢复(XFS & EXT4) >> 正文

【恢复】Linux 文件恢复(XFS & EXT4)

2年前 (2021-12-28)     作者:Jiaozn     分类:Linux     阅读次数:2595     评论(0)    

Linux中,删除rm命令使用需谨慎,有时候可能由于误操作,导致重要文件删除了,这时不要太紧张,操作得当的话,还是可以恢复的。


EXT 类型文件恢复#

删除一个文件,实际上并不清除inode节点和block的数据,只是在这个文件的父目录里面的block中,删除这个文件的名字。Linux是通过Link的数量来控制文件删除的,只有当一个文件不存在任何Link的时候,这个文件才会被删除。

当然,这里所指的是彻底删除,即已经不能通过回收站找回的情况,比如使用rm -rf来删除数据。针对Linux下的EXT文件系统,可用的恢复工具有debugfsext3grepextundelete等。 其中extundelete是一个开源的Linux数据恢复工具,支持ext3ext4文件系统。

在数据被误删除后,第一时间要做的就是卸载被删除数据所在的分区,如果是根分区的数据遭到误删,就需要将系统进入单用户模式,并且将根分区以只读模式挂载。这样做的原因很简单,因为将文件删除后,仅仅是将文件的inode节点中的扇区指针清零,实际文件还存储在磁盘上,如果磁盘继续以读写模式挂载,这些已删除的文件的数据块就可能被操作系统重新分配出去,在这些数据库被新的数据覆盖后,这些数据就真的丢失了,恢复工具也回天无力。所以以只读模式挂载磁盘可以尽量降低数据库中数据被覆盖的风险,以提高恢复数据成功的比例。

Demo#

在编译安装extundelete之前需要先安装两个依赖包e2fsprogs-libse2fsprogs-devel,这两个包在系统安装光盘的/Package目录下就有,使用rpmyum命令将其安装。e2fsprogs-devel安装依赖于libcom_err-devel包。

1.系统使用的是rhel6.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-1.41.12-18.el6.x86_64.rpmwarning: e2fsprogs-libs-1.41.12-18.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing...                ########################################### [100%]
	package e2fsprogs-libs-1.41.12-18.el6.x86_64 is already installed
[root@localhost Packages]# rpm -ivh libcom_err-devel-1.41.12-18.el6.x86_64.rpmwarning: libcom_err-devel-1.41.12-18.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing...                ########################################### [100%]
   1:libcom_err-devel       ########################################### [100%]
[root@localhost Packages]# rpm -ivh e2fsprogs-devel-1.41.12-18.el6.x86_64.rpmwarning: e2fsprogs-devel-1.41.12-18.el6.x86_64.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.解压extundelete软件包。

[root@localhost ~]# tar jxvf extundelete-0.2.4.tar.bz2 -C ~extundelete-0.2.4/extundelete-0.2.4/acinclude.m4extundelete-0.2.4/missingextundelete-0.2.4/autogen.shextundelete-0.2.4/aclocal.m4extundelete-0.2.4/configureextundelete-0.2.4/LICENSEextundelete-0.2.4/READMEextundelete-0.2.4/install-shextundelete-0.2.4/config.h.inextundelete-0.2.4/src/extundelete-0.2.4/src/extundelete.ccextundelete-0.2.4/src/block.hextundelete-0.2.4/src/kernel-jbd.hextundelete-0.2.4/src/insertionops.ccextundelete-0.2.4/src/block.cextundelete-0.2.4/src/cli.ccextundelete-0.2.4/src/extundelete-priv.hextundelete-0.2.4/src/extundelete.hextundelete-0.2.4/src/jfs_compat.hextundelete-0.2.4/src/Makefile.inextundelete-0.2.4/src/Makefile.amextundelete-0.2.4/configure.acextundelete-0.2.4/depcompextundelete-0.2.4/Makefile.inextundelete-0.2.4/Makefile.am

4.配置、编译、安装extundelete软件包

[root@localhost ~]# cd extundelete-0.2.4[root@localhost extundelete-0.2.4]# lsacinclude.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-0.2.4]# ./configureConfiguring extundelete 0.2.4Writing generated files to disk
[root@localhost extundelete-0.2.4]# makemake -s all-recursiveMaking all in src
extundelete.cc:571: 警告:未使用的参数‘flags’
[root@localhost extundelete-0.2.4]# make installMaking install in src
  /usr/bin/install -c extundelete '/usr/local/bin'

5.准备好用于测试的分区,/dev/sdb1ext4格式,挂载到/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]# lsa  b  c  lost+found

7.删除测试文件。

[root@localhost ext4]# rm -f a b[root@localhost ext4]# lsc  lost+found

8.卸载对应的分区。

[root@localhost ext4]# cd[root@localhost ~]# umount /mnt/ext4/

9.恢复删除的内容。

[root@localhost ~]# extundelete /dev/sdb1 --restore-allNOTICE: 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文件系统目前也没有比较成熟的文件恢复工具,所以建议提前做好数据备份,以避免数据丢失。

xfs类型的文件可使用xfsdumpxfsrestore工具进行备份恢复。若系统中未安装xfsdumpxfsrestore工具,可以通过yum install -y xfsdump命令安装。xfsdump按照inode顺序备份一个xfs文件系统。

xfsdump的备份级别有两种:0表示完全备份;1-9表示增量备份。默认为0

xfsdump -f 备份存放位置 要备份路径或设备文件

-f:指定备份文件目录
-L:指定标签session label
-M:指定设备标签media label
-s:备份单个文件,-s后面不能直接跟路径。

1.xfsdump不支持没有挂载的文件系统备份,所以只能备份已挂载的;
2.xfsdump必须使用root的权限才能操作(涉及文件系统的关系);
3.xfsdump只能备份XFS文件系统;
4.xfsdump备份下来的数据(档案或储存媒体)只能让xfsrestore解析;
5.xfsdump是透过文件系统的UUID来分辨各个备份档的,因此不能备份两个具有相同UUID的文件系统。

xfsrestore -f 恢复文件的位置 存放恢复后文件的路径

Demo#

1.准备好用于测试的分区,/dev/sdb1ext4格式,挂载到/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.txt1 directory, 2 files

4.使用xfsdump命令备份整个分区。

[root@localhost ~]# xfsdump -f /opt/dump_sdb1 /dev/sdb1xfsdump: using file dump (drive_simple) strategyxfsdump: version 3.1.4 (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      //指定备份会话标签session label entered: "dump_sdb1"

 --------------------------------- end dialog ---------------------------------xfsdump: level 0 dump of localhost.localdomain:/mnt/xfsxfsdump: dump date: Fri Sep  6 13:36:12 2019xfsdump: session id: 74232f85-124c-4486-8d91-f35208534f74xfsdump: session label: "dump_sdb1"xfsdump: ino map phase 1: constructing initial dump listxfsdump: ino map phase 2: skipping (no pruning necessary)xfsdump: ino map phase 3: skipping (only one dump stream)xfsdump: ino map construction completexfsdump: estimated dump size: 21760 bytesxfsdump: /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 mapxfsdump: dumping directoriesxfsdump: dumping non-directory filesxfsdump: ending media filexfsdump: media file size 22952 bytesxfsdump: dump size (non-dir files) : 0 bytesxfsdump: dump complete: 46 seconds elapsedxfsdump: Dump Summary:xfsdump:   stream 0 /opt/dump_sdb1 OK (success)xfsdump: Dump Status: SUCCESS

5.查看备份信息与内容。

[root@localhost ~]# xfsdump -Ifile 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-84a55949c1faxfsdump: 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) strategyxfsrestore: version 3.1.4 (dump format 3.0) - type ^C for status and controlxfsrestore: searching media for dumpxfsrestore: examining media file 0xfsrestore: dump description:xfsrestore: hostname: localhost.localdomainxfsrestore: mount point: /mnt/xfsxfsrestore: volume: /dev/sdb1xfsrestore: session time: Fri Sep  6 13:36:12 2019xfsrestore: level: 0xfsrestore: session label: "dump_sdb1"xfsrestore: media label: "sdb1"xfsrestore: file system id: f8805a3e-089e-4875-ad54-d31e5dc98835xfsrestore: session id: 74232f85-124c-4486-8d91-f35208534f74xfsrestore: media id: cc32446f-42e8-489b-867f-84a55949c1faxfsrestore: using online session inventoryxfsrestore: searching media for directory dumpxfsrestore: reading directoriesxfsrestore: 2 directories and 3 entries processedxfsrestore: directory post-processingxfsrestore: restoring non-directory filesxfsrestore: restore complete: 0 seconds elapsedxfsrestore: 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.txt1 directory, 2 files


除非注明,发表在“Jiaozn的博客”的文章『【恢复】Linux 文件恢复(XFS & EXT4)』版权归Jiaozn所有。 转载请注明出处为“本文转载于『Jiaozn的博客』原地址https://www.jiaozn.com/reed/748.html
0

评论

发表评论   

昵称*

E-mail*(建议输入,以便收到博主回复的提示邮件)

网站

分享:

支付宝

微信