ホームページ  >  記事  >  運用・保守  >  Linuxで削除されたファイルを復元するにはどうすればよいですか?

Linuxで削除されたファイルを復元するにはどうすればよいですか?

青灯夜游
青灯夜游オリジナル
2020-04-20 16:12:2319081ブラウズ

Linux で削除されたファイルを復元するにはどうすればよいですか?次の記事では、Linux で削除されたファイルを復元する方法を紹介します。一定の参考値があるので、困っている友達が参考になれば幸いです。

Linuxで削除されたファイルを復元するにはどうすればよいですか?

Linux には Windows のようなごみ箱がなく、基本的に rm -rf ** を使用してファイルを取得することはできません。

それでは、質問は次のとおりです:

Linux で誤って削除されたファイルについては、本当にソフトウェアを使って復元することはできないのでしょうか?

答えはもちろんノーです。削除されたファイルは次の可能性があります。ソフトウェアを介して回復することもできます。誤って削除したファイルの復元は、次の 2 つの状況に分類できます。

  • 1 つは、削除後のプロセスに削除情報が存在する場合です。

  • もう 1 つは、削除 将来、プロセスは見つからなくなり、ツールを使用してのみ復元できます。

次に、例を使用して、誤って削除した場合の 2 つの異なる回復方法を説明します。

ファイルを誤って削除したプロセスがまだ残っている状況:

これは通常、アクティブなプロセスに継続的な標準入力または出力があることを意味します。ファイルが削除された後も、プロセス PID はまだ存在します。これは、一部のサーバーが一部のファイルを削除してもディスクが解放されない理由でもあります。

ターミナルを開いて、テスト ファイルに対して cat append 操作を実行します:

[root@docking ~]# echo "This is DeleteFile test." > deletefile.txt
[root@docking ~]# ls
deletefile.txt
[root@docking ~]# cat >> deletefile.txt 
Add SomeLine into deletefile for fun.

別のターミナルを開いてファイルを表示すると、内容がはっきりと確認できます:

[root@docking ~]# ls
deletefile.txt
[root@docking ~]# cat deletefile.txt 
This is DeleteFile test.
Add SomeLine into deletefile for fun.

At this次に、ファイルを削除します。 rm -f deletefile.txt

[root@docking ~]# rm -f deletefile.txt 
[root@docking ~]# ls
#命令查看这个目录,文件已经不存在了,那么现在我们将其恢复出来。
  • lsof は、削除されたファイル プロセスがまだ存在するかどうかを確認します。

  • #インストールされていない場合は、

    yum install lsof または apt-get install lsof

  • ## 1. 同様の状況では、まず lsof を使用して、削除されたファイルがまだ存在するかどうかを確認します
[root@docking ~]# lsof | grep deletefile
cat       21796          root    1w      REG              253,1        63     138860 /root/deletefile.txt (deleted)

2. Recovery

cp /proc/pid/fd/1 /指定されたディレクトリ/ファイル名


現在の状況のプロセス ディレクトリ (通常は /proc/pid/fd/) を入力します:

[root@docking ~]# cd /proc/21796/fd
[root@docking fd]# ll
总用量 0
lrwx------ 1 root root 64 1月  18 22:21 0 -> /dev/pts/0
l-wx------ 1 root root 64 1月  18 22:21 1 -> /root/deletefile.txt (deleted)
lrwx------ 1 root root 64 1月  18 22:21 2 -> /dev/pts/0

リカバリ操作:

[root@docking fd]# cp 1 ~/deletefile.txt.backup
[root@docking fd]# cat ~/deletefile.txt.backup 
This is DeleteFile test.
Add SomeLine into deletefile for fun.

3。リカバリが完了しました。

誤って削除したファイル プロセスはもう存在しません。ツールを使用して復元してください。

いくつかのファイル ディレクトリを準備します

#准备一份挂载的盘
mkdir backuptest
cd backuptest
mkdir deletetest
mkdir deletetest/innerfolder
echo "Delete a folder test." > deletetest/innerfolder/deletefile.txt 

echo "tcpdump:x:172:72::/:/sbin/nologin" > tmppasswd

最終的に準備されたディレクトリ構造は次のとおりです。次のように:

taroballs@taroballs-PC:/media/taroballs/taroballs/backuptest$ cd ..
taroballs@taroballs-PC:/media/taroballs/taroballs$ tree backuptest/
backuptest/
├── deletetest
│   └── innerfolder
│       └── deletefile.txt
└── tmppasswd

2 directories, 2 files

ここでディレクトリの削除を開始します

rm -rf Backuptest/

<pre class="brush:sql;toolbar:false">taroballs@taroballs-PC:/media/taroballs/taroballs$ rm -rf backuptest/ taroballs@taroballs-PC:/media/taroballs/taroballs$ ls -l 总用量 0</pre>この場合、通常はデーモンが存在しないか、バックグラウンド プロセスがデータを入力し続けます。したがって、削除は実際に削除されます。 lsof もそれを認識できないため、ツールを使用して復元する必要があります。

次に、誤って削除したファイルの回復を開始します。

私たちが使用するツールは、extundelete サードパーティ ツールです。リカバリ手順と注意事項は次のとおりです。

    i ノードが上書きされないように、現在のパーティションに対する操作を停止します。 i ノードが上書きされた場合、基本的には復元されます。
  • 大げさに言うと、たとえば、パーティションが配置されているサービスを停止し、ディレクトリが配置されているデバイスをアンインストールし、必要に応じてネットワークを切断します。
  • サードパーティ ソフトウェアの回復失敗によるデータ損失を防ぐために、dd コマンドを使用して現在のパーティションをバックアップします。
  • データが非常に重要な状況に適しています。これは例なので、バックアップはありません。バックアップの場合は、次の方法を検討できます: dd if=/path/filename of=/dev/vdc1
  • umount コマンドを使用して、現在のデバイス パーティションをアンマウントします。または、fuser コマンド umount /dev/vdb1
  • を使用します。デバイスがビジーであることを示すプロンプトが表示された場合は、fuser コマンドを使用してアンインストールを強制できます: fuser -m -v -i - k ./
  • サードパーティ ツールの extundelete インストールをダウンロードし、誤って削除されたファイルを検索して復元します
extundelete ツールのインストール

extundelete のダウンロード アドレス: http://extundelete.sourceforge.net/

wget https://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2

ファイルを解凍します

tar jxvf extundelete-0.2.4.tar.bz2

このエラーが報告された場合

[root@docking ~]# tar jxvf extundelete-0.2.4.tar.bz2 
tar (child): bzip2:无法 exec: 没有那个文件或目录
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now

問題を解決するには、

yum -y install bzip2

を使用してください。<pre class="brush:sql;toolbar:false">[root@docking ~]# tar jxvf extundelete-0.2.4.tar.bz2 extundelete-0.2.4/ extundelete-0.2.4/acinclude.m4 extundelete-0.2.4/missing extundelete-0.2.4/autogen.sh extundelete-0.2.4/aclocal.m4 extundelete-0.2.4/configure extundelete-0.2.4/LICENSE extundelete-0.2.4/README ...................................................</pre><pre class="brush:sql;toolbar:false">cd extundelete-0.2.4 ./configure</pre>この手順でエラーが報告された場合

[root@docking extundelete-0.2.4]# ./configure 
Configuring extundelete 0.2.4
configure: error: in `/root/extundelete-0.2.4&#39;:
configure: error: C++ compiler cannot create executables
See `config.log&#39; for more details

使用

yum -y install gcc-c

解決策.前の手順を実行した後もエラーが発生する場合は、

[root@docking extundelete-0.2.4]# ./configure 
Configuring extundelete 0.2.4
configure: error: Can&#39;t find ext2fs library

を使用してください

yum -y install e2fsprogs e2fsprogs-devel

これを解決します。 #Ubuntu の解決策は sudo apt-get install e2fslibs-dev e2fslibs-dev
予期せぬことが何も起こらなければ、configure はここで正常に完了できるはずです。

[root@docking extundelete-0.2.4]# ./configure 
Configuring extundelete 0.2.4
Writing generated files to disk
[root@docking extundelete-0.2.4]#

最後に

make

その後、make install<pre class="brush:sql;toolbar:false">[root@docking extundelete-0.2.4]# make make -s all-recursive Making all in src extundelete.cc: 在函数‘ext2_ino_t find_inode(ext2_filsys, ext2_filsys, ext2_inode*, std::string, int)’中: extundelete.cc:1272:29: 警告:在 {} 内将‘search_flags’从‘int’转换为较窄的类型‘ext2_ino_t {aka unsigned int}’ [-Wnarrowing] buf, match_name2, priv, 0}; ^ [root@docking extundelete-0.2.4]# make install Making install in src /usr/bin/install -c extundelete &amp;#39;/usr/local/bin&amp;#39;</pre>extundeleteインストールが完了しました。

誤って削除されたファイルをスキャンします:

df -lh

を使用してマウントを表示します: <pre class="brush:sql;toolbar:false">taroballs@taroballs-PC:~$ df -lh 文件系统 容量 已用 可用 已用% 挂载点 udev 1.9G 0 1.9G 0% /dev tmpfs 387M 1.8M 385M 1% /run /dev/sda2 92G 61G 26G 71% / tmpfs 1.9G 49M 1.9G 3% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/sda3 104G 56G 44G 57% /home tmpfs 387M 40K 387M 1% /run/user/1000 /dev/sda4 70G 20G 47G 30% /media/taroballs/d8423f8c-d687-4c03-a7c8-06a7fb57f96d /dev/sdb1 6.8G 4.1G 2.8G 60% /media/taroballs/taroballs /dev/sr0 4.0G 4.0G 0 100% /media/taroballs/2018-01-16-12-36-00-00 taroballs@taroballs-PC:~$ cd /media/taroballs/taroballs/ taroballs@taroballs-PC:/media/taroballs/taroballs$</pre> ディレクトリ /media/taroballs/taroballs

が / にマウントされていることがわかります。 dev/sdb1 このファイル システム内。

マウント ディスクをマウントします。

例:

taroballs@taroballs-PC:~$ df -lh | grep /dev/sdb1
/dev/sdb1       6.8G  4.1G  2.8G   60% /media/taroballs/taroballs

このディレクトリをマウントします

taroballs@taroballs-PC:~$ umount /media/taroballs/taroballs
taroballs@taroballs-PC:~$ df -lh | grep /dev/sdb1
taroballs@taroballs-PC:~$ 
#记得删除一定要后umount哦,不然二次写入谁也帮不了你呢。

inode ノードによる復元

taroballs@taroballs-PC:~$ mkdir recovertest
taroballs@taroballs-PC:~$ cd recovertest/
taroballs@taroballs-PC:~/recovertest$
リカバリの実行

extundelete /dev/sdb1 --inode 2

<pre class="brush:sql;toolbar:false">taroballs@taroballs-PC:/media/taroballs/taroballs$ sudo extundelete /dev/sdb1 --inode 2 NOTICE: Extended attributes are not restored. Loading filesystem metadata ... 8 groups loaded. Group: 0 Contents of inode 2: . .省略N行 File name | Inode number | Deleted status . 2 .. 2 deletetest 12 Deleted tmppasswd 14 Deleted</pre>削除したフォルダーはスキャンによって発見されました。ここで回復操作を実行します。

(1) 単一ファイルのリカバリ tmppasswd

taroballs@taroballs-PC:~/recovertest$  extundelete /dev/sdb1 --restore-file passwd   
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
Successfully restored file tmppasswd

リカバリされたファイルは、現在のディレクトリ RECOVERED_FILES に配置されます。

復元されたファイルを表示します:

taroballs@taroballs-PC:~/recovertest$ cat tmppasswd 
tcpdump:x:172:72::/:/sbin/nologin

(2) ディレクトリを復元します deletetest

extundelete /dev/sdb1 --restore-directory  deletetest
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
Searching for recoverable inodes in directory deletetest ... 
5 recoverable inodes found.
Looking through the directory structure for deleted files ...

(3) すべてを復元します

taroballs@taroballs-PC:~/recovertest$ extundelete /dev/sdb1 --restore-all
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
Searching for recoverable inodes in directory / ... 
5 recoverable inodes found.
Looking through the directory structure for deleted files ... 
0 recoverable inodes still lost. 
taroballs@taroballs-PC:~/recovertest$ tree 
backuptest/
├── deletetest
│   └── innerfolder
│       └── deletefile.txt
└── tmppasswd
2 directories, 2 files

(4) 指定された i ノードを復元します

taroballs@taroballs-PC:~/recovertest$ extundelete /dev/sdb1 --restore-inode 14
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
taroballs@taroballs-PC:~/recovertest$ cat file.14 
tcpdump:x:172:72::/:/sbin/nologin
#注意恢复inode的时候,恢复 出来的文件名和之前不一样,需要单独进行改名。

最後に

extundelete

: の使用法を添付します。<pre class="brush:sql;toolbar:false">$ extundelete --help Usage: extundelete [options] [--] device-file Options: --version, -[vV] Print version and exit successfully. --help, Print this help and exit successfully. --superblock Print contents of superblock in addition to the rest. If no action is specified then this option is implied. --journal Show content of journal. --after dtime Only process entries deleted on or after &amp;#39;dtime&amp;#39;. --before dtime Only process entries deleted before &amp;#39;dtime&amp;#39;.Actions: --inode ino Show info on inode &amp;#39;ino&amp;#39;. --block blk Show info on block &amp;#39;blk&amp;#39;. --restore-inode ino[,ino,...] Restore the file(s) with known inode number &amp;#39;ino&amp;#39;. The restored files are created in ./RECOVERED_FILES with their inode number as extension (ie, file.12345). --restore-file &amp;#39;path&amp;#39; Will restore file &amp;#39;path&amp;#39;. &amp;#39;path&amp;#39; is relative to root of the partition and does not start with a &amp;#39;/&amp;#39; The restored file is created in the current directory as &amp;#39;RECOVERED_FILES/path&amp;#39;. --restore-files &amp;#39;path&amp;#39; Will restore files which are listed in the file &amp;#39;path&amp;#39;. Each filename should be in the same format as an option to --restore-file, and there should be one per line. --restore-directory &amp;#39;path&amp;#39; Will restore directory &amp;#39;path&amp;#39;. &amp;#39;path&amp;#39; is relative to the root directory of the file system. The restored directory is created in the output directory as &amp;#39;path&amp;#39;. --restore-all Attempts to restore everything. -j journal Reads an external journal from the named file. -b blocknumber Uses the backup superblock at blocknumber when opening the file system. -B blocksize Uses blocksize as the block size when opening the file system. The number should be the number of bytes. --log 0 Make the program silent. --log filename Logs all messages to filename.--log D1=0,D2=filename Custom control of log messages with comma-separated Examples below: list of options. Dn must be one of info, warn, or --log info,error error. Omission of the &amp;#39;=name&amp;#39; results in messages --log warn=0 with the specified level to be logged to the console. --log error=filename If the parameter is &amp;#39;=0&amp;#39;, logging for the specified level will be turned off. If the parameter is &amp;#39;=filename&amp;#39;, messages with that level will be written to filename. -o directory Save the recovered files to the named directory. The restored files are created in a directory named &amp;#39;RECOVERED_FILES/&amp;#39; by default.</pre><p>推荐:《<a href="https://www.php.cn/linux.html" target="_blank">linux教程</a>》</p>

以上がLinuxで削除されたファイルを復元するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。