2

I have corrupted filesystem. I would like to copy "survivors", remaining good files. So my question is -- how to copy only good files from one directory to another.

Please note, that for example one file can be 10GB and it is possible to copy 5GB of it. So if the tool responsible for copying uses target directory immediately to store copied content, it should remove such partial file as soon as it becomes clear it is impossible to copy entire file (because it is unreadable).

Good file -- a file which can be read from the beginning to the end without any I/O error.

Important: it should be done automatically, i.e. without user interaction (with user interaction I am already using Midnight Commander).

Update: for practical reasons it is required that maximum attempts to read the file can be specified, otherwise the tool could fall easily into infinite loop just trying read a file over and over again. So for example, if a given chunk of file cannot read (because of I/O error) and there was X tries -- assume the file is no good.

greenoldman
  • 6,176

2 Answers2

1

There is a utility called ddrescue or dd_rescue. Those are two different utilities that use different approaches to do the same thing. They copy data from A to B, but, unlike cp, they do not fail, but repeat attempts several times and skip if they can not read data.
Those utilities are not filesystem-aware, but you can run them on files either manually, or with find.
However, I recommend, if possible, to use dd_rescue to copy the whole damaged drive into a single image file in some safe place, then mount that image and copy files from there the usual way. The image will be readable without errors.
Use dd_recue first.

  • Thank you, however it is impossible for me to do it -- as you write, they operate on disk level, not file level. I don't have target disk as big as source volume. With file approach I can copy one directory, store the list of the bad files and remove them freeing space, copy another directory and so on. – greenoldman Oct 03 '16 at 19:29
  • You can use find utility to run dd_rescue on each file separately and preserve directory structure. Please, look this question and just replace mv there with dd_rescue – Barafu Albino Oct 04 '16 at 04:45
  • Alternatively, you can use lzo compression while creating image, so that it will not be bigger than actual data, unless your drive in question was filled with random noise at some point. Anyway, after you install dd_recue package, read man ddr_lzo – Barafu Albino Oct 04 '16 at 04:54
0

The easiest should be to run a fsck -a or fsck -y on the corrupted filesystem (if it's the root filesystem, boot with a Live CD and mount it as an external volume). All files that can be recovered will end up at their original place, the others will end up in /lost+found -- where, if you feel inclined, you can try to recover something out of them.

The mentioned options do as follows:

Options to different filesystem-specific fsck's are not standardized. If in doubt, please consult the man pages of the filesystem-specific checker. Although not guaranteed, the following options are supported by most file system checkers:

-a Automatically repair the file system without any questions (use this option with caution). Note that e2fsck(8) supports -a for backwards compatibility only. This option is mapped to e2fsck's -p option which is safe to use, unlike the -a option that some file system checkers support.

(...)

-y For some filesystem-specific checkers, the -y option will cause the fs-specific fsck to always attempt to fix any detected filesystem corruption automatically. Sometimes an expert may be able to do better driving the fsck manually. Note that not all filesystem-specific checkers implement this option. In particular fsck.minix(8) and fsck.cramfs(8) does not support the -y option as of this writing.

dr_
  • 29,602
  • Thank you, but there is misunderstanding -- I already ran fsck (actually e2fsck) and finally I am able to mount the partition, but I lost some files (pity, but I cannot do nothing about it) and some are corrupted. I can read just a part of them. And my question is about this second step -- bad files. How to copy directory ignoring bad files, or in other words -- how to copy only good ones. – greenoldman Oct 01 '16 at 20:39