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

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

不过是在工作或者学习中,我们都经常又可以触及到一个非常危险的命令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
  • 到此数据就恢复过来了

虽然实验做完了。但是我有个疑问,如果有清楚的可以在留言告诉我,就是数据删除可能是意外,我没提前备份怎么办?

(毕竟这个实验是按教程走的,不会出现意外)

目录
相关文章
|
XML JSON 监控
浅谈logback日志架构
浅谈logback日志架构
223 0
|
人工智能 前端开发 测试技术
Apipost 与 Apifox 深度对比:2025全方位解析助力 API 开发的利器
本文对比了Apipost与Apifox两款API开发与管理工具在功能、使用场景及用户评价等方面的差异。Apipost在API设计、调试、文档管理、Mock服务、离线支持及AI能力方面表现更优,尤其适合大型企业级项目和高效率需求的团队。而Apifox则适用于小型项目或对功能要求较低的团队。综合来看,Apipost在多方面具备明显优势,是高效、高质量API开发的理想选择。
250 24
|
2月前
|
开发框架 缓存 前端开发
【Axure原型】Ant Design Pro 原型后台项目-免费
Ant Design Pro 是基于 Ant Design 组件库构建的企业级中后台前端解决方案,提供丰富的页面模板、预设设计规范、路由配置及状态管理,支持快速搭建高质量应用。内置高阶组件如 ProTable、ProForm,提升开发效率,适用于复杂业务场景。
|
存储 人工智能 Java
【图文详解】基于Spring AI的旅游大师应用开发、多轮对话、文件持久化、拦截器实现
【图文详解】基于Spring AI的旅游大师应用开发、多轮对话、文件持久化、拦截器实现
571 0
|
5月前
|
弹性计算 NoSQL 数据库
阿里云服务器如何备份数据?
阿里云服务器数据备份有多种方法,用户可按需选择。主要方式包括:1)快照备份,创建云盘的时间点拷贝,支持定期备份与数据恢复;2)数据库备份DBS,适用于多种环境的数据库备份,涵盖本地及多云场景;3)云备份Cloud Backup,提供统一灾备平台,支持ECS整机、数据库、文件系统等全方位备份,保障数据安全。
|
安全 网络协议 Linux
在Linux中,什么是SSH,并且如何使用它?
在Linux中,什么是SSH,并且如何使用它?
怎样将Beyond Compare添加到系统右键菜单
怎样将Beyond Compare添加到系统右键菜单
1923 0
|
数据可视化 测试技术 uml
【掌握绘图艺术】用PlantUML绘制完美UML图表,开发者的福音
【掌握绘图艺术】用PlantUML绘制完美UML图表,开发者的福音
3146 1
|
SQL 存储 关系型数据库
group by语句查询如何确保保留重复数据
group by语句查询如何确保保留重复数据
283 0
|
PyTorch 算法框架/工具 Docker
Docker 镜像和你的操作系统或者硬件有一些兼容性问题。
Docker 镜像和你的操作系统或者硬件有一些兼容性问题。
494 2