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

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

Apr 20, 2020 pm 04:12 PM
linux

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/

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>taroballs@taroballs-PC:/media/taroballs/taroballs$ rm -rf backuptest/ taroballs@taroballs-PC:/media/taroballs/taroballs$ ls -l 总用量 0</pre><div class="contentsignin">ログイン後にコピー</div></div>この場合、通常はデーモンが存在しないか、バックグラウンド プロセスがデータを入力し続けます。したがって、削除は実際に削除されます。 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

を使用してください。<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;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><div class="contentsignin">ログイン後にコピー</div></div><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>cd extundelete-0.2.4 ./configure</pre><div class="contentsignin">ログイン後にコピー</div></div>この手順でエラーが報告された場合

[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<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;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><div class="contentsignin">ログイン後にコピー</div></div>extundeleteインストールが完了しました。

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

df -lh

を使用してマウントを表示します: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;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><div class="contentsignin">ログイン後にコピー</div></div> ディレクトリ /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

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;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><div class="contentsignin">ログイン後にコピー</div></div>削除したフォルダーはスキャンによって発見されました。ここで回復操作を実行します。

(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

: の使用法を添付します。

$ 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 &#39;dtime&#39;.
  --before dtime         Only process entries deleted before &#39;dtime&#39;.Actions:
  --inode ino            Show info on inode &#39;ino&#39;.
  --block blk            Show info on block &#39;blk&#39;.
  --restore-inode ino[,ino,...]
                         Restore the file(s) with known inode number &#39;ino&#39;.
                         The restored files are created in ./RECOVERED_FILES                         with their inode number as extension (ie, file.12345).
  --restore-file &#39;path&#39;  Will restore file &#39;path&#39;. &#39;path&#39; is relative to root
                         of the partition and does not start with a &#39;/&#39;
                         The restored file is created in the current
                         directory as &#39;RECOVERED_FILES/path&#39;.
  --restore-files &#39;path&#39; Will restore files which are listed in the file &#39;path&#39;.
                         Each filename should be in the same format as an option
                         to --restore-file, and there should be one per line.
  --restore-directory &#39;path&#39;
                         Will restore directory &#39;path&#39;. &#39;path&#39; is relative to the
                         root directory of the file system.  The restored
                         directory is created in the output directory as &#39;path&#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 &#39;=name&#39; results in messages   --log warn=0          with the specified level to be logged to the console.
   --log error=filename  If the parameter is &#39;=0&#39;, logging for the specified
                         level will be turned off.  If the parameter is
                         &#39;=filename&#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 &#39;RECOVERED_FILES/&#39; by default.
ログイン後にコピー

推荐:《linux教程

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

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

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

インターネットはLinuxで実行されますか? インターネットはLinuxで実行されますか? Apr 14, 2025 am 12:03 AM

インターネットは単一のオペレーティングシステムに依存していませんが、Linuxはその上で重要な役割を果たしています。 Linuxは、サーバーやネットワークデバイスで広く使用されており、安定性、セキュリティ、スケーラビリティに人気があります。

Linuxでnginxを開始する方法 Linuxでnginxを開始する方法 Apr 14, 2025 pm 12:51 PM

Linuxでnginxを開始する手順:nginxがインストールされているかどうかを確認します。 systemctlを使用して、nginxを開始してnginxサービスを開始します。 SystemCTLを使用して、NGINXがシステムスタートアップでNGINXの自動起動を有効にすることができます。 SystemCTLステータスNGINXを使用して、スタートアップが成功していることを確認します。 Webブラウザのhttp:// localhostにアクセスして、デフォルトのウェルカムページを表示します。

nginxサーバーを開始する方法 nginxサーバーを開始する方法 Apr 14, 2025 pm 12:27 PM

NGINXサーバーを起動するには、異なるオペレーティングシステムに従って異なる手順が必要です。Linux/UNIXシステム:NGINXパッケージをインストールします(たとえば、APT-GetまたはYumを使用)。 SystemCtlを使用して、NGINXサービスを開始します(たとえば、Sudo SystemCtl Start NGinx)。 Windowsシステム:Windowsバイナリファイルをダウンロードしてインストールします。 nginx.exe実行可能ファイルを使用してnginxを開始します(たとえば、nginx.exe -c conf \ nginx.conf)。どのオペレーティングシステムを使用しても、サーバーIPにアクセスできます

Nginxが開始されるかどうかを確認する方法 Nginxが開始されるかどうかを確認する方法 Apr 14, 2025 pm 01:03 PM

nginxが開始されるかどうかを確認する方法:1。コマンドラインを使用します:SystemCTLステータスnginx(Linux/unix)、netstat -ano | FindStr 80(Windows); 2。ポート80が開いているかどうかを確認します。 3.システムログのnginx起動メッセージを確認します。 4. Nagios、Zabbix、Icingaなどのサードパーティツールを使用します。

nginx403を解く方法 nginx403を解く方法 Apr 14, 2025 am 10:33 AM

Nginx 403禁止エラーを修正する方法は?ファイルまたはディレクトリの許可を確認します。 2。HTACCESSファイルを確認します。 3. nginx構成ファイルを確認します。 4。nginxを再起動します。他の考えられる原因には、ファイアウォールルール、Selinux設定、またはアプリケーションの問題が含まれます。

nginx403エラーを解く方法 nginx403エラーを解く方法 Apr 14, 2025 pm 12:54 PM

サーバーには、要求されたリソースにアクセスする許可がなく、NGINX 403エラーが発生します。ソリューションには以下が含まれます。ファイル許可を確認します。 .htaccess構成を確認してください。 nginx構成を確認してください。 SELINUXアクセス許可を構成します。ファイアウォールルールを確認してください。ブラウザの問題、サーバーの障害、その他の可能なエラーなど、他の原因をトラブルシューティングします。

Nginxサーバーのインストールとクイック構成ガイド Nginxサーバーのインストールとクイック構成ガイド Apr 13, 2025 pm 10:18 PM

この記事では、Nginxの構造と構成方法を紹介します。 1. nginxをインストール:centosでsudoyumininstallnginxを使用し、ubuntuでsudoapt-getinstallnginxを使用し、インストール後にsudosystemctlstartnginxから始めます。 2。基本構成:/etc/nginx/nginx.confファイルを変更し、主にサーバーブロックのリスニング(ポート)およびルート(サイトルートディレクトリ)命令を変更し、変更後、sudosystemctlrestartnginxを使用して再起動して有効にします。 3。仮想ホスト構成:ingginx.co

Nginxが起動されているかどうかを確認する方法は? Nginxが起動されているかどうかを確認する方法は? Apr 14, 2025 pm 12:48 PM

Linuxでは、次のコマンドを使用して、nginxが起動されるかどうかを確認します。SystemCTLステータスNGINXコマンド出力に基づいて、「アクティブ:アクティブ(実行)」が表示された場合、NGINXが開始されます。 「アクティブ:非アクティブ(dead)」が表示されると、nginxが停止します。

See all articles