Linux ifind命令:找回误删文件! (linux ifind)

Linux操作系统被众多程序员和服务器管理员视为最强的开源系统。但是,任何系统都不是完美无缺的。在Linux系统上,误删数据是一个普遍存在的问题。虽然有很多方法可以防止误删,但是如果数据已经被删除,该怎么办?

在Linux系统中,ifind是一个非常有用的命令,可以帮助你找回误删除的文件。ifind的作用是搜索已删除的文件,找到相关的inode,并将其恢复为可用状态。

ifind命令原理

首先需要明确,当在Linux系统中删除一个文件时,只是将inode表中的相关信息标记为“已删除”,而实际数据仍然驻留在磁盘上。这就是ifind命令的原理所在。ifind命令将使用类似于grep的方式搜索已经删除的inode,并将其恢复为可用状态。因此,如果在删除文件后马上执行ifind命令,则可能会找到丢失的文件并恢复它。

ifind的使用

在Linux系统中,ifind命令的使用非常简单。在开始之前,请确定您有sudo权限。

要使用ifind命令来寻找误删除的文件,请按照以下步骤操作:

1. 在终端中输入命令:sudo ifind。这将显示可以恢复的 inode 的整个列表。

2. 找到您要恢复的文件的 inode,并将其写入文件。

3. 使用istat查找inode的详细信息。

4. 如果您已经找到了需要的文件,请使用命令sudo icat 。这将输出文本到屏幕上。将其保存到文件中,然后将文件的扩展名更改为正确的扩展名。

需要注意的是,如果您发现使用ifind命令后找不到您要找的文件,则可能是由于它已经完全被覆盖或者过早地被删除。在这种情况下,恢复文件变得非常困难。

如何避免误删数据?

虽然ifind是恢复误删数据的极好方法,但是仍然建议您采取措施避免数据丢失。以下是一些常用的方法:

1. 创建备份:在Linux系统上,备份数据是非常重要的。将所有关键数据备份到本地或远程服务器中以确保可以恢复所有丢失的数据。

2. 慎用rm:在删除文件之前,更好仔细检查文件名和文件路径,以确保不会删除不必要的文件。建议使用rm -i命令,该命令会在删除之前提示确认。

3. 限制访问权限:通过设置适当的文件和目录权限来限制文件的访问。这可以帮助防止不必要的文件访问和误删除数据。

相关问题拓展阅读:

如何解决SELinux问题

首先需要确认SELinux处于激活状态,可以使用getenforce命令:

 兄凳 shell> getenforce

Enforcing

  或者使用sestatus命令:

  shell> sestatus

SELinux status: enabled

SELinuxfs mount: /selinux

Current mode: enforcing

Mode from config file:enforcing

Policy version: 24

Policy from config file:targeted

  注:关于SELinux的基础知识介绍请参考鸟哥的Linux私房菜中相关的介绍。

  我们还需要确认系统已经安装并启动了Apache,没有的话就YUM装一个,这很简单,就不多说了,接着在root目录创建一个测试文件test.html,如下:

  shell> cat /root/test.html

hello, world.

  然后把这个测试文件拷贝到Apache的DocumentRoot目录,我的Apache是通过YUM安装的话,缺省是/var/www/html目录,如下:

  shell> cp /root/test.html /var/www/html

  接着浏览一下,如果没出什么幺蛾子,应该一切都在意料之中,如下:

  shell> curl

hello, world.

  看到这,你可能觉得我废话连篇,别着急,下面就是见证奇迹的时候了:

  同样还是那个测试文件test.html,不过这次不再是拷贝,而是移动,如下:

  shell> mv /root/test.html /var/www/html

  接着浏览一下,怎么样,结果很出人意料吧,竟然提示权限错误,如下:

  shell> curl

403 Forbidden

Forbidden

You don’t have permission to access /test.html

on this server.

  当然,我们现在知道这个问题是由于SELinux引起的,但还不知其所以然,实际上问题的原因此时已经被蚂郑audit进程记录到了相应的日志里,可以这羡物旅样查看:

  shell> audit2why yum install setroubleshoot

  它本身是一个GUI套件,不过其中包含的一个sealert命令对我们命令行用户很有用:

  shell> sealert -a /var/log/audit/audit.log

Summary:

SELinux is preventing /usr/in/httpd “getattr” access to

/var/www/html/test.html.

Detailed Description:

SELinux denied access requested by httpd. /var/www/html/test.html may be a

mislabeled. /var/www/html/test.html default SELinux type is httpd_sys_content_t,

but its current type is admin_home_t. Changing this file back to the default

type, may fix your problem.

File contexts can be assigned to a file in the following ways.

* Files created in a directory receive the file context of the parent

directory by default.

* The SELinux policy might override the default label inherited from the

parent directory by specifying a process running in context A which creates

a file in a directory labeled B will instead create the file with label C.

An example of this would be the dhcp client running with the dhclient_t type

and creating a file in the directory /etc. This file would normally receive

the etc_t type due to parental inheritance but instead the file is labeled

with the net_conf_t type because the SELinux policy specifies this.

* Users can change the file context on a file using tools such as chcon, or

restorecon.

This file could have been mislabeled either by user error, or if an normally

confined application was run under the wrong domain.

However, this might also indicate a bug in SELinux because the file should not

have been labeled with this type.

If you believe this is a bug, please file a bug report against this package.

Allowing Access:

You can restore the default system context to this file by executing the

restorecon command. restorecon ‘/var/www/html/test.html’, if this file is a

directory, you can recursively restore using restorecon -R

‘/var/www/html/test.html’.

Fix Command:

/in/restorecon ‘/var/www/html/test.html’

  这次应该看懂了吧!原因是说Apache下文件上下文类型应该是httpd_sys_content_t,但是现在是admin_home_t,所以权限错误,并且在结尾处给出了修复命令。

  可httpd_sys_content_t,admin_home_t都怎么看啊?很简单,借助ls命令的-Z参数即可:

  shell> ls -Z /path

  回到问题的开始,拷贝之所以没出现问题,是因为cp自动修改上下文属性,而移动之所以出现问题是因为mv保留原文件的上下文属性。

  注:关于SELinux和Apache的详细介绍,可以参考『man httpd_selinux』。

  知道了如何解决SELinux问题,以后如果遇到类似的情况不要急着武断的关闭SELinux。

有碰到即使是permissive也不行的,最后是改成disabled后重启机器才可以。

上面有个小错,应该是echo “0” > /selinux/enforce (少了>)selinux如果有开很容易雀尘造成ifind问题,参看下面系统日志Aug 2 20:52:06 cc-lnx-imglib setroubleshoot: SELinux is preventing /opt/simpana/iDataAgent/ifind “execstack”梁岁枯 access to . For complete SELinux messages. run sealert -l 4a0b3993-b18f-4c72-9e1d-4f5d8175ad41Jul 31 00:30:09 cc-lnx-imglib setroubleshoot: SELinux is preventing /橡洞bin/hostname access to a leaked /tmp/.gxsetup/cvpkglo g.pipe.26262 file descriptor. For complete SELinux messages. run sealert -l e675a6a6-efc9-461f-bc0fd2d308

别一种查看SElinux方法:# sestatusSELinux status: disabled

如何解决 artdot indi.mail system

上面陪神有个小错,应该是echo “0” > /selinux/enforce (少了>)selinux如果有开很容易造成ifind问题,参看下面系统日志兆乱郑Aug 2 20:52:06 cc-lnx-imglib setroubleshoot: SELinux is preventing /opt/simpana/iDataAgent/族颂ifind “execstack” access to . For complete SELinux messages. run sealert -l 4a0b3993-b18f-4c72-9e1d-4f5d8175ad41Jul 31 00:30:09 cc-lnx-imglib setroubleshoot: SELinux is preventing /bin/hostname access to a leaked /tmp/.gxsetup/cvpkglo g.pipe.26262 file descriptor. For complete SELinux messages. run sealert -l e675a6a6-efc9-461f-bc0fd2d308

设置-兼容性视图设置-添加此网站(将需打开的邮箱网址增加进去)ok

关于linux ifind的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。


数据运维技术 » Linux ifind命令:找回误删文件! (linux ifind)