Linux服务器文件删除空间未释放的问题
作者:chenpingzhao 发布时间:[ 2016/8/29 10:40:57 ] 推荐标签:Linux 操作系统
三、文件恢复
在说明问题之前,先介绍下一些文件的基本概念:
文件实际上是一个指向inode的链接, inode链接包含了文件的所有属性, 比如权限和所有者, 数据块地址(文件存储在磁盘的这些数据块中). 当你删除(rm)一个文件, 实际删除了指向inode的链接, 并没有删除inode的内容. 进程可能还在使用. 只有当inode的所有链接完全移去, 然后这些数据块将可以写入新的数据.
proc文件系统可以协助我们恢复数据. 每一个系统上的进程在/proc都有一个目录和自己的名字, 里面包含了一个fd(文件描述符)子目录(进程需要打开文件的所有链接). 如果从文件系统中删除一个文件, 此处还有一个inode的引用 :/proc/进程号/fd/文件描述符
你需要知道打开文件的进程号(pid)和文件描述符(fd). 这些都可以通过lsof工具方便获得, lsof的意思是”list open files, 列出(进程)打开的文件”. 然后你将可以从/proc拷贝出需要恢复的数据.
1.创建一个测试文件并且备份下,方面后续验证
touch testfile
cp testfile testfile.backup.2014
2.查看文件的相关信息
stat testfile
File: 'testfile'
Size: 343545 Blocks: 241 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 361579 Links: 1
Access: (0664/-rw-rw-r–) Uid: ( 505/ zhaoke) Gid: ( 505/ zhaoke)
Access: 2014-11-09 15:00:38.000000000 +0800
Modify: 2014-11-09 15:00:34.000000000 +0800
Change: 2014-04-09 15:00:34.000000000 +0800
3.删除文件
rm testfile
4.查看文件
ls -l testfile
ls: testfile: No such file or directory
stat testfile
stat: cannot stat 'testfile': No such file or directory
testfile文件删除了,但不要终止仍在使用文件的进程, 因为一旦终止, 文件将很难恢复.
现在我们开始找回数据之旅,先使用lsof命令查看下
lsof | grep testfile
tail 5317 root 4r REG 253,0 343545 361579 /root/testfile (deleted)
第一个纵行是进程的名称(命令名), 第二纵行是进程号(PID), 第四纵行是文件描述符
现在你知道5317进程仍有打开文件, 文件描述符是4. 那我们开始从/proc里面拷贝出数据.
你可能会考虑使用cp -a, 但实际上没有作用, 你将拷贝的是一个指向被删除文件的符号
ls -l /proc/5317/fd/4
lr-x—— 1 root root 64 09 15:00 /proc/5317/fd/4 -> /root/testfile (deleted)
使用cp -a命令测试恢复
cp -a /proc/5317/fd/4 testfile.backup
使用ls命令来查看
ls -l testfile.backup
lrwxrwxrwx 1 root root 29 09 15:02 testfile.backup -> /roor/testfile (deleted)
通过上面的命令我们发现,使用cp -a命令,其恢复的是一个指向被删除文件的符号链接
使用file命令分别查看文件和文件描述符
1.查看文件
file testfile.backup
testfile.backup: broken symbolic link to '/root/testfile (deleted)'
2.查看文件描述符
file /proc/5317/fd/4
/proc/5317/fd/4: broken symbolic link to '/root/myfile (deleted)'
根据上面的file结果,可以使用cp拷贝出文件描述符数据到一个文件中,如下:
cp /proc/5317/fd/4 testfile.new
使用上面的命令恢复后,我们需要终确认一下文件是否恢复,以及文件内容是否正确:
ls -l testfile.new
然后把新旧的两个文件对比
diff testfile.new myfile.backup
4、lsof的一些其他用法
1、查看端口号
root@localhost:~# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
AliYunDun 1996 root 11u IPv4 117919702 0t0 TCP 124.46.185.34:42372->140.205.140.205:http (ESTABLISHED)
aegis_qua 2114 root 11u IPv4 14868 0t0 TCP 124.46.185.34:43077->42.156.166.25:http (CLOSE_WAIT)
nginx 13706 www-data 5u IPv4 140102705 0t0 TCP 124.46.185.34:http->140.205.127.64:41851 (ESTABLISHED)
nginx 13706 www-data 15u IPv4 6150826 0t0 TCP *:http (LISTEN)
nginx 13706 www-data 35u IPv4 140096235 0t0 TCP 124.46.185.34:http->198.11.137.21:47450 (ESTABLISHED)
nginx 13706 www-data 36u IPv4 140095680 0t0 TCP 124.46.185.34:http->140.205.127.52:39887 (ESTABLISHED)
nginx 13706 www-data 39u IPv4 140102712 0t0 TCP 124.46.185.34:http->198.11.137.5:37626 (ESTABLISHED)
nginx 13706 www-data 40u IPv4 140102699 0t0 TCP 124.46.185.34:http->140.205.127.11:36374 (ESTABLISHED)
nginx 13706 www-data 41u IPv4 140102759 0t0 TCP 124.46.185.34:http->101.200.101.217:47550 (ESTABLISHED)
nginx 13706 www-data 42u IPv4 140102722 0t0 TCP 124.46.185.34:http->101.200.101.201:38595 (ESTABLISHED)
nginx 13706 www-data 43u IPv4 140102840 0t0 TCP 124.46.185.34:http->140.205.127.58:45859 (ESTABLISHED)
nginx 13706 www-data 44u IPv4 140095709 0t0 TCP 124.46.185.34:http->101.200.101.216:44828 (ESTABLISHED)
nginx 13706 www-data 46u IPv4 140070070 0t0 TCP 124.46.185.34:http->140.205.127.18:24113 (ESTABLISHED)
nginx 13706 www-data 47u IPv4 140103294 0t0 TCP 124.46.185.34:http->198.11.137.61:43262 (ESTABLISHED)
nginx 13706 www-data 48u IPv4 140102761 0t0 TCP 124.46.185.34:http->101.200.101.220:31912 (ESTABLISHED)
nginx 13706 www-data 50u IPv4 140103297 0t0 TCP 124.46.185.34:http->101.200.101.203:42752 (ESTABLISHED)
nginx 13706 www-data 52u IPv4 140095824 0t0 TCP 124.46.185.34:http->198.11.137.14:62968 (ESTABLISHED)
nginx 13706 www-data 53u IPv4 140102730 0t0 TCP 124.46.185.34:http->101.200.101.205:27518 (ESTABLISHED)
nginx 13706 www-data 54u IPv4 140102822 0t0 TCP 124.46.185.34:http->140.205.127.51:58945 (ESTABLISHED)
nginx 13706 www-data 55u IPv4 140103313 0t0 TCP 124.46.185.34:http->140.205.127.15:20785 (ESTABLISHED)
nginx 13706 www-data 56u IPv4 140102812 0t0 TCP 124.46.185.34:http->140.205.31.212:1963 (ESTABLISHED)
nginx 13706 www-data 57u IPv4 140102929 0t0 TCP 124.46.185.34:http->101.200.101.213:42452 (ESTABLISHED)
nginx 13706 www-data 58u IPv4 140095749 0t0 TCP 124.46.185.34:http->101.200.101.208:50444 (ESTABLISHED)
nginx 13706 www-data 59u IPv4 140102769 0t0 TCP 124.46.185.34:http->140.205.127.60:59679 (ESTABLISHED)
nginx 13706 www-data 60u IPv4 140102814 0t0 TCP 124.46.185.34:http->140.205.127.2:25156 (ESTABLISHED)
nginx 13706 www-data 61u IPv4 140102771 0t0 TCP 124.46.185.34:http->198.11.137.53:16935 (ESTABLISHED)
nginx 13706 www-data 62u IPv4 140102845 0t0 TCP 124.46.185.34:http->198.11.137.2:50535 (ESTABLISHED)
nginx 13706 www-data 63u IPv4 140102773 0t0 TCP 124.46.185.34:http->198.11.137.52:8521 (ESTABLISHED)
nginx 13706 www-data 64u IPv4 140102856 0t0 TCP 124.46.185.34:http->140.205.127.23:55360 (ESTABLISHED)
nginx 13706 www-data 66u IPv4 140075271 0t0 TCP 124.46.185.34:http->140.205.127.48:64726 (ESTABLISHED)
nginx 13706 www-data 67u IPv4 140102937 0t0 TCP 124.46.185.34:http->101.200.101.204:10400 (ESTABLISHED)
nginx 13706 www-data 68u IPv4 140102932 0t0 TCP 124.46.185.34:http->101.200.101.213:42455 (ESTABLISHED)
nginx 13706 www-data 69u IPv4 140102941 0t0 TCP 124.46.185.34:http->140.205.127.24:31585 (ESTABLISHED)
nginx 13706 www-data 70u IPv4 140102934 0t0 TCP 124.46.185.34:http->198.11.137.50:11837 (ESTABLISHED)
nginx 13706 www-data 71u IPv4 140103309 0t0 TCP 124.46.185.34:http->140.205.127.15:20777 (ESTABLISHED)
nginx 13706 www-data 92u IPv4 140070742 0t0 TCP 124.46.185.34:http->140.205.127.31:65012 (ESTABLISHED)
nginx 13707 www-data 5u IPv4 140092233 0t0 TCP 124.46.185.34:http->140.205.127.60:57698 (ESTABLISHED)
nginx 13707 www-data 15u IPv4 6150826 0t0 TCP *:http (LISTEN)
nginx 13707 www-data 17u IPv4 140092036 0t0 TCP 124.46.185.34:http->140.205.127.51:54339 (ESTABLISHED)
nginx 13707 www-data 30u IPv4 140092031 0t0 TCP 124.46.185.34:http->198.11.137.59:64831 (ESTABLISHED)
nginx 13707 www-data 35u IPv4 140092214 0t0 TCP 124.46.185.34:http->140.205.31.213:40510 (ESTABLISHED)
nginx 13707 www-data 39u IPv4 140092202 0t0 TCP 124.46.185.34:http->140.205.127.9:19592 (ESTABLISHED)
nginx 13707 www-data 44u IPv4 140092205 0t0 TCP 124.46.185.34:http->140.205.127.7:24059 (ESTABLISHED)
nginx 13707 www-data 46u IPv4 140103741 0t0 TCP 124.46.185.34:http->198.11.137.22:61619 (ESTABLISHED)
nginx 13707 www-data 51u IPv4 140072557 0t0 TCP 124.46.185.34:http->140.205.127.50:51697 (ESTABLISHED)
nginx 13707 www-data 61u IPv4 140056450 0t0 TCP 124.46.185.34:http->140.205.127.37:27131 (ESTABLISHED)
nginx 13707 www-data 69u IPv4 140072632 0t0 TCP 124.46.185.34:http->140.205.127.13:20352 (ESTABLISHED)
nginx 13708 www-data 15u IPv4 6150826 0t0 TCP *:http (LISTEN)
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
Linux下开源的DDR压力测试工具曝Linux恶意软件:让树莓派设备挖掘数字货币linux系统中不同颜色的文件夹及根目录介绍软件测试工程师必知必会Linux命令Linux下DNS服务器配置如何成为不可替代的Linux运维工程师?详解Linux进程(作业)的查看和杀死Linux 日志定时轮询流程详解比特币勒索病毒不只Windows系统有,Linux版的来了Linux日志定时轮询流程详解Linux iommu和vfio概念空间解构Linux系统如何低于TCP洪水攻击Linux无损调整分区大小Linux下防火墙配置实例Linux使用Jexus托管Asp.Net Core应用程序Linux中引号的那些事
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南