ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

rm -rf 误删除数据如何进行恢复

2021-03-03 11:01:41  阅读:334  来源: 互联网

标签:误删除 test1 dump centos7 xfsdump rf test rm root


不过是在工作或者学习中,我们都经常又可以触及到一个非常危险的命令rm -rf,这个命令会直接删除数据,并且不需要我们确认,所以说使用的时候要注意。

那么,如果我们一不小心删除了数据该怎么办呢?

  • 首先,我们要知道删除数据的两种场景
1.在执行删除文件时,该文件正被进程使用
2.是文件未被使用,而被误删除
  • 为什么删除的数据还可以恢复?
对于第一种情况:文件正在使用,数据可以恢复是因为Linux里,
每个文件都有两个link计数器:i_count和i_nlink
i_count作用是当一个文件被进程引用时,它的数值会加1,也就是说
它记录的是文件被进程引用的次数
i_nlink的作用是记录文件产生硬链接的个数。
Linux系统中只有两个数值都为0时,文件才会被删除
如果此时文件被占用,那么它的i_count数值不为0
那么就可以把文件找回来
对于第二种情况:没有被进程使用的文件被删除,此时i_count和i_link都为0
这个时候文件的inode连接信息已经被删除了,我们需要通过存放文件的block单元,
做数据块的数据找回。
我们看到的数据存储依赖于inode和block两个单元
我们删除数据时,删除的是inode的链接
但block数据块并没有删除
所以我们要通过分析后端的block块,对文件进行恢复
注意:
我们通过block块对数据进行恢复,理论可以找回完整数据,
但是有一个风险:如果有进程不断往磁盘写数据时,
需要申请新的block块,如果系统分配已删除文件的block块
那么新写入的数据会覆盖原有数据,这时数据会真正丢失
如果出现这种情况,需要umount目录所在的磁盘设备

案例演示:

  • 第一种场景:文件在使用过程中被删除

  • 创建一个测试文件,并使用tail命令持续查看

[root@centos7 ~]# mkdir /test
[root@centos7 ~]# cd /test/
[root@centos7 test]# echo "hehehehehe hahahaha">test.txt
[root@centos7 test]# cat test.txt 
hehehehehe hahahaha
[root@centos7 test]# tail -f test.txt 
hehehehehe hahahaha
  • 复制一个ssh渠道,进行删除文件
[root@centos7 ~]# cd /test/
[root@centos7 test]# rm -rf test.txt 
[root@centos7 test]# ls
[root@centos7 test]# 
  • 进行数据恢复
#查看哪个进程正在使用被删除的文件
[root@centos7 test]# lsof | grep test.txt
tail      4296         root    3r      REG              253,0        20     736294 /test/test.txt (deleted)
#通过进程号进入文件句柄
[root@centos7 test]# cd /proc/4296/fd
[root@centos7 fd]# ll
总用量 0
lrwx------ 1 root root 64 3月   2 22:14 0 -> /dev/pts/0
lrwx------ 1 root root 64 3月   2 22:14 1 -> /dev/pts/0
lrwx------ 1 root root 64 3月   2 22:14 2 -> /dev/pts/0
lr-x------ 1 root root 64 3月   2 22:14 3 -> /test/test.txt (deleted)
lr-x------ 1 root root 64 3月   2 22:14 4 -> anon_inode:inotify
#进行数据恢复
[root@centos7 fd]# cp 3 /test/test.txt
[root@centos7 fd]# cd /test/
[root@centos7 test]# ls
test.txt
[root@centos7 test]# cat test.txt 
hehehehehe hahahaha
#发现数据已经恢复
  • 第二种场景:未被进程使用而文件被删除
  • 我看到网上有 专门恢复ext4类型的帖子,有专门的工具,很方便
  • 但我的系统是xfs类型的,虽然有教程但是还是有些疑惑,最底下写出来
  • 首先,再准备一块新磁盘,将其格式化为xfs类型
#首先确认新磁盘添加成功
[root@centos7 ~]# fdisk -l
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
#将其格式化为xfs类型
[root@centos7 ~]# fdisk /dev/sdb
依次输入n-p-1-回车-回车-w
[root@centos7 ~]# mkfs.xfs /dev/sdb1
  • 创建测试目录,并挂载
[root@centos7 ~]# mkdir /test1
[root@centos7 ~]# mount /dev/sdb1 /test1/
  • 创建测试内容
[root@centos7 ~]# cd /test1/
[root@centos7 test1]# echo "hdhwahdw">file1.txt
[root@centos7 test1]# mkdir haha
  • 备份文件
[root@centos7 test1]# 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 centos7:/test1
xfsdump: dump date: Wed Mar  3 10:40:05 2021
xfsdump: session id: 9e939503-a58d-4669-bb87-0bec313db8b5
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: 25536 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 22184 bytes
xfsdump: dump size (non-dir files) : 544 bytes
xfsdump: dump complete: 24 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /opt/xfs_dump OK (success)
xfsdump: Dump Status: SUCCESS
  • 删除测试文件进行恢复
[root@centos7 test1]# rm -rf ./*
[root@centos7 test1]# ls
[root@centos7 test1]# xfsrestore -f /opt/xfs_dump /test1/
  • 查看是否恢复成功
[root@centos7 test1]# ls
file1.txt  haha
  • 到此数据就恢复过来了

虽然实验做完了。但是我有个疑问,如果有清楚的可以在留言告诉我,就是数据删除可能是意外,我没提前备份怎么办?
(毕竟这个实验是按教程走的,不会出现意外)

标签:误删除,test1,dump,centos7,xfsdump,rf,test,rm,root
来源: https://blog.csdn.net/qq_42527269/article/details/114294960

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有