简体   繁体   English

Linux文件删除恢复

[英]Linux file deleted recovery

Is there a way to create a file in Linux that link to a specific iNode? 有没有办法在Linux中创建链接到特定iNode的文件? Take this scenario: There is a file that is in course of writing (a log maybe) and the specific file is deleted but a link in the dir /proc is still pointing at it. 采取这种情况:有一个文件正在写入(可能是一个日志),并删除了特定文件, dir / proc中的链接仍然指向它。 In this case we need not a bare copy of it but an hard link to it so we can have the future modifications and the most last modification before the process close and the system delete it. 在这种情况下,我们不需要它的裸副本 ,而是它的硬链接,因此我们可以在进程关闭之前进行未来的修改和最后修改,系统将其删除。

If we have the iNode number is there a way to achieve this goal? 如果我们有iNode号码有没有办法实现这个目标?

Since there is no Syscall that involves iNode, because is a concept of extX fs and is not a good practice make a stove pipe but it is to make a chain of responsability (as MEL suggests), there is only a NO answer for this question because at VFS level we handle files path and names and not other internal representations. 由于没有涉及iNode的Syscall,因为这是一个extX fs的概念,并且不是一个好的做法制作一个炉管,但它是一个负责任的链(如MEL建议),这个问题只有一个答案没有答案因为在VFS级别,我们处理文件路径和名称而不是其他内部表示。

BUT to achieve the goal to track the most last modification we can use a continous monitoring and duplication with tail : 要实现追踪最后修改的目标,我们可以使用尾部连续监视和复制:

tail -c+1 -f --pid=PID /proc/PID/fd/FD > /path/to/the/copy

where PID is the pid of the process that have the deleted file still opened and FD is its file descriptor number. 其中PID是已删除文件仍然打开且FD是其文件描述符编号的进程的pid。 With -f tail open and hold the file to display further modification, with -c+1 start to "tail" from the first byte and with --pid=PID tail is informed to exit when the pid exit. 使用-f tail打开并保持文件显示进一步修改, -c + 1从第一个字节开始“尾”并且--pid = PID尾部被告知在pid退出时退出。

You can use lsof to recover deleted files (sometimes)... 您可以使用lsof恢复已删除的文件(有时)...

> lsof | grep testing.txt
less    4607    juliet  4r  REG 254,4   21  
       8880214 /home/juliet/testing.txt (deleted)

Be sure to read the original article for full details before attempting this, unless you're a Maveric like me. 在尝试此操作之前,请务必阅读原始文章以获取完整的详细信息,除非您是像我这样的Maveric。

> ls -l /proc/4607/fd/4
lr-x------ 1 juliet juliet 64 Apr  7 03:19 
         /proc/4607/fd/4 -> /home/juliet/testing.txt (deleted)
> cp /proc/4607/fd/4 testing.txt.bk

http://www.linuxplanet.com/linuxplanet/tips/6767/1 http://www.linuxplanet.com/linuxplanet/tips/6767/1

Enjoy 请享用

It's always difficult to answer a question like "can I do" confidently in the negative. 总是很难回答“我能做什么”这样的问题。 But as far as I see, neither /sys/ nor /proc provide a mapping of open files descriptors that are not symlinks. 但据我所知,/ sys / nor / proc都没有提供不是符号链接的打开文件描述符的映射。 I assume by "BUT a link in the dir /proc is still pointing at it" you mean that the /proc//fd/ entries look like symlinks? 我假设“但是dir / proc中的链接仍然指向它”你的意思是/ proc // fd / entries看起来像符号链接? I'm almost sure you cannot recover the original file. 我几乎可以肯定你无法恢复原始文件。

I take that back: As user user2676075 pointed out, copying does work. 我把它拿回来:用户user2676075指出,复制确实有效。 Just hardlinking doesn't ... 只是硬链接不...

UPDATE : If you think about it, it's quite logical. 更新 :如果你考虑一下,这是合乎逻辑的。

  • /proc and /sys are file systems different from your hard disk. / proc和/ sys是与硬盘不同的文件系统。 So they can't provide file like directory entries which one could hardlink to a destination on the hard disk. 因此,他们无法提供类似目录条目的文件,这些条目可以硬链接到硬盘上的目标。
  • The /proc/*/fd/ entries pretend to be symlinks, but actually they are different, else the copying would not work. / proc / * / fd / entries假装是符号链接,但实际上它们是不同的,否则复制将不起作用。 I think they pretend to be symlinks to provide meaningful information with 'ln -l'. 我认为他们假装是符号链接,用'ln -l'提供有意义的信息。

  • Regarding the (missing) capability to hardlink to some inode (let's say with some system call): This cannot be part of the kernel or the VFS-Interface, for the following reasons: 关于(缺少)硬链接到某些inode的能力(假设有一些系统调用):由于以下原因,这不能成为内核或VFS接口的一部分:

    • It would violate the integrity of the file system. 它会违反文件系统的完整性。 The filesystem is not supposed to keep the disk blocks of files that are completely deleted around in the same manner as files that persist. 文件系统不应该以与持久文件相同的方式保留完全删除的文件的磁盘块。

    • The inodes might be a completely virtual concept to identify a "slot where a datastream is stored'. I assume there can be implementations that would have a problem converting a slot that has no reference back to a slot which is refered to by a name in the file system. inode可能是一个完全虚拟的概念,用于标识“存储数据流的插槽”。我假设可能存在将没有引用的插槽转换回由一个名称引用的插槽的问题的实现。文件系统。

    I admit the case against the possibility of such a system call is not water tight. 我承认,这种系统调用的可能性不是防水的。 But given the current state of the VFS interface (which AFAIR doesn't provide for such a call), it would be a heavy burden for any file system implementation (including eg distributed file systems) to provide a call to link a file into a directory by inode. 但是考虑到VFS接口的当前状态(AFAIR没有提供这样的调用),对于任何文件系统实现(包括例如分布式文件系统)来说,提供调用将文件链接到一个文件系统将是一个沉重的负担。目录由inode。

ATM I wonder if calling fstat before and after deleting the last reference is actually requires to return the same inode information ... t ATM我想知道在删除最后一个引用之前和之后调用fstat实际上是否需要返回相同的inode信息... t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM