失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > rm -rf 误删除数据如何进行恢复

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

时间:2018-12-14 22:40:00

相关推荐

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

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

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

首先,我们要知道删除数据的两种场景

1.在执行删除文件时,该文件正被进程使用2.是文件未被使用,而被误删除

为什么删除的数据还可以恢复?

对于第一种情况:文件正在使用,数据可以恢复是因为Linux里,每个文件都有两个link计数器:i_count和i_nlinki_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.txttail4296 root 3rREG 253,0 20736294 /test/test.txt (deleted)#通过进程号进入文件句柄[root@centos7 test]# cd /proc/4296/fd[root@centos7 fd]# ll总用量 0lrwx------ 1 root root 64 3月 2 22:14 0 -> /dev/pts/0lrwx------ 1 root root 64 3月 2 22:14 1 -> /dev/pts/0lrwx------ 1 root root 64 3月 2 22:14 2 -> /dev/pts/0lr-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]# lstest.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/sdb1xfsdump: using file dump (drive_simple) strategyxfsdump: 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_dumpsession label entered: "xfs_dump"--------------------------------- end dialog ---------------------------------xfsdump: level 0 dump of centos7:/test1xfsdump: dump date: Wed Mar 3 10:40:05 xfsdump: session id: 9e939503-a58d-4669-bb87-0bec313db8b5xfsdump: session label: "xfs_dump"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: 25536 bytesxfsdump: /var/lib/xfsdump/inventory created============================= media label dialog =============================please enter label for media in drive 0 (timeout in 300 sec)-> sdb1media 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 22184 bytesxfsdump: dump size (non-dir files) : 544 bytesxfsdump: dump complete: 24 seconds elapsedxfsdump: 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]# lsfile1.txt haha

到此数据就恢复过来了

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

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

如果觉得《rm -rf 误删除数据如何进行恢复》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。