rm -rfを実行してしまったときにファイルを復活させる

ついうっかり、

# rm -rf

してしまった場合に使えるのが、extundeleteコマンドである。Ext3もしくはExt4パーティションに対してextundeleteを使うと、ジャーナルに残されたログを利用して、直近で削除したファイルを指定ディレクトリ下に復活してくれる。もちろん、削除後にその領域に書き込みが行われていないことが前提である。ただし、拡張属性(xattr)は失われてたままになるので、その点は注意が必要である。

作業手順としては、

  1. 間違って削除してしまった
  2. 速やかに該当パーティションをリードオンリーにする
  3. extundeleteを使ってファイルを復活させる

という流れになる。

リードオンリーでリマウント

該当パーティションをリードオンリーでマウントしなおすには、アンマウントして、「-o ro」オプションを付けてマウントすればOKだが、1コマンドで実行するなら以下のようになる。

# mount -o remount,ro /

別のパーティションでextundeleteを実行

次に、復活させるファイルを書き込む別のパーティションに移動して、extundeleteを実行する。「--restore-all」オプションを付ければ、復活可能なファイルを全復活する。

# cd /home
# extundelete --restore-all /dev/mapper/host-root
WARNING: Extended attributes are not restored. WARNING: EXT3_FEATURE_INCOMPAT_RECOVER is set.
The partition should be unmounted to undelete any files without further data loss.
If the partition is not currently mounted, this message indicates
it was improperly unmounted, and you should run fsck before continuing.
If you decide to continue, extundelete may overwrite some of the deleted
files and make recovering those files impossible. You should unmount the
file system and check it with fsck before using extundelete.
Would you like to continue? (y/n)

ここでyを入力して作業を進めていく。


y Loading filesystem metadata ... 1280 groups loaded.
Loading journal descriptors ... 7915 descriptors loaded.
Searching for recoverable inodes in directory / ...
71 recoverable inodes found.
Looking through the directory structure for deleted files ...

 状況によってさまざまなメッセージが表示されるが、カレントディレクトリ下のRECOVERED_FILESディレクトリ下にトップからのディレクトリ構造を保ったまま(相対パス)でファイルが復活しているはずだ。

--restore-allのオプションのほかにも、--restore-fileによるファイル指定、--afterによる時間指定などが使える。オプションなしで実行すれば簡単な説明を表示できる。

# extundelete
No action specified; implying --superblock.

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 'dtime'.
  --before dtime         Only process entries deleted before 'dtime'.
Actions:
  --inode ino            Show info on inode 'ino'.
  --block blk            Show info on block 'blk'.
  --restore-inode ino[,ino,...]
                         Restore the file(s) with known inode number 'ino'.
                         The restored files are created in ./RESTORED_FILES
                         with their inode number as extension (ie, file.12345).
  --restore-file 'path'  Will restore file 'path'. 'path' is relative to root
                         of the partition and does not start with a '/' (it
                         must be one of the paths returned by --dump-names).
                         The restored file is created in the current
                         directory as 'RECOVERED_FILES/path'.
  --restore-files 'path' Will restore files which are listed in the file 'path'.
                         Each filename should be in the same format as an option
                         to --restore-file, and there should be one per line.
  --output-dir 'path'    Restore files in the output dir 'path'.
                         By default the restored files are created under current directory 'RECOVERED_FILES'.
  --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.
Error parsing command-line options.

ちなみに、削除したファイルの領域になにか書き込まれていて復活不能な場合は、

Unable to restore inode 32434 (etc/bash.bashrc): Space has been reallocated.

のようなメッセージが表示される。こうなってしまう前に、削除してしまったと思ったら、速やかにリードオンリーにしてデータを書き込まれないようにすることが肝要である。

このようにextundeleteは便利なツールであるが、このツールを活用するには、

  1. extundeleteをすぐに使えるように、インストール時にファイル領域に書き込みが起きないように、あらかじめインストールしておく
  2. lvmパーティションで運用し、パーティションを分割しておく

ということも重要である。lvmが使いやすくなったいま、いつでもパーティションの領域は増やせる。パーティションを分割しない運用は避けるべきだろう。/boot、/var、/homeは最低限分けておいたほうがよい。

# aptitude install extundelete