I have overwritten a file in an ext filesystem. What now?
I was preparing to write a small analysis of my first Code Kata exercise, created a run script for it and:
NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!!!
It wasn’t versioned yet!
So essentially I have overwritten the entire file I was working on yesterday! That was 15 minutes of work, but recreate the same code without afterthoughts I have done already in my would be almost impossible!
I was pissed off. My gf was like “you have to write it once again, not a big deal”. I googled “how to undelete a file in linux”. It wasn’t a recipe that I was looking for - it was only about files that were just removed, not overwritten.
There is a solution though.
Prerequisites
- overwritten file
- linux (ext3 partition) - I am the Ubuntu guy
- head (preferably yours)
- separate partition with data (essentially you’ll have to run live cd/usb to make the same result)
Step 1: umount the partition
This is crucial for the entire process. As long as you do any writes to the partition you can overwrite existing loose file. This will make your data unrecoverable. So, just do:
Of course, if it’s different partition (in my case it was /dev/sdb1
) you have
to use a different one.
This makes you a little safer, the real data won’t be overwritten by an accident now.
Step 2: install extundelete
The tool called extundelete is available in your apt repository already:
It’s relatively lightweight so don’t worry if you will install on your system partition.
Step 3: recover all files!
This is the point I haven’t found in docs. The overwritten file is no longer existing as a node with the particular path, therefore you can’t find it in the specific one, using:
Oh, I am devastated. But the point is I have overwritten the old file and it’s really existing so there is nothing to recover! Does it make sense? A little.
So what I had to do was to recover all files:
And that was the best part. Equipped with the experience I have noticed that
there are like 123124 files names like file1234324
. This was a God’s sign
definitely, so I have started to search through them using grep.
This was fun because I remember the stupid name I gave for one of the variables. And I have found it, put it back to my beloved folder, committed and pushed to the Github repo forever. Finally!
I hope you will find way out of your accident as well so happy!