Updated: Automatically restore files from lost+found

Today, in an IRC channel near you, the discussion came to recover files from lost+found somehow. Two years ago I wrote some scripts to automatically recover files from lost+found, so this is some sort of a repost. There are two scripts: one that generates some kind of ls-LR file to have all the information needed for the second script, which restores those files in lost+found to their original folders. Here is the information from the original blog post: 

make-lsLR.sh – call this regularly (cron) to create the needed files that are stored in /root/. Of course you can alter the location easily and exclude other directories from being scanned.

check_lost+found.py – The second script is to be run when your fsck managed to mess up with your files and stored them into lost+found directory. It takes 3 arguments: 1) the source directory where your messed up lost+found directory is, 2) the target directory to which the data will be saved and 3) a switch to actually make it happen instead of a dry-run.

You can find both files as attachment end the end of this blog post.

I’ve chosen to copy the files to a different place instead of moving them within the same filesystem to their original place for safety reasons. Primary goal is to retrieve the files from lost+found, not to replace a full featured backup and restore application. Because of this the script doesn’t handle hard- nor symlinks correctly. It just copy files.

Of course there’s still room for improvements, like handling hard-/symlinks correctly or using inode number instead of md5sums to move data back to its prior location. But it works for me[tm] well enough in this way, so I’m satisfied so far. You’re welcome, though, to improve this piece of ugliness if you like.

Maybe someone else finds this usefull as well. Use it on your own risk, of course. 🙂


10 thoughts on “Updated: Automatically restore files from lost+found

  1. Please merge this into
    Please merge this into locate/updatedb so that we can have this out of the box!

    1. Yeah, would be nice to have
      Yeah, would be nice to have something like this out of the box. In fact, I was wondering why nobody before me wrote such a script?
      But anyway… I’m not a programmer and would like to leave the task of merging this to someone else. On the other hand I’m unsure whether it would be sane to merge this code into locate/updatedb, because I wanted something simple and plain. Hence the plain text files. So that you can even restore things with very basic tools from initrd or else.

  2. how to use these scripts
    I am new to linux and have just used testdisk to recover lost partitions containing my ubuntu 11.04 install and now all my files are in sdb1 /lost+found. Can I use your script to rewrite everything to the proper directorys they came from & if yes, what parts of the scripts do I need to modify to direct them to the proper drive/partition (sdb1) in which to copy and restore the files? Thanks for any help you can offer.

    1. The first script needs to be
      The first script needs to be run by cron, at least it needs to be run *before* something happens and you end with files in lost+found.
      The second script is then to recover those files from lost+found by comparing their md5sums to the ones stored in the previous generated file.

      The scripts operating on filesystem level, meaning it starts scanning / and restores files from /lost+found to another directory you can define.

  3. Stops after Making the Directories
    It appears as if I have everything running correctly with this. When I run the check_lostfound.py it runs and then stops after it makes the directories. I have checked to see if the TXT files are in the correct location under root/ls-md5sum-files.txt and it is. Thanks for any help!!

  4. Difficult to say what’s going
    Difficult to say what’s going wrong without the exact error output. If there’s no error output, please insert some debugging print lines and see where it fails.

    Does make-lsLR.sh run without error? Do the resulting text files look correct?

  5. Can I use your script to
    Can I use your script to rewrite everything to the proper directorys they came from & if yes, what parts of the scripts do I need to modify to direct ?

Comments are closed.