How to recover a corrupted SD-Card (maybe!)

Hi all, as many of you know, I've long held that people spend too long agonising over the use of SD-Cards in Pi's. A decent card (so that it supports wear levelling) with plenty of free space should last years.

Well I now have another data point on that subject. And I bring you a possible recovery solution. :rofl:

Last week the SD-Card on my Pi3 failed. It is a Samsung EVO Plus 32GB. It lasted some 1,182 days, that is 3 years, 2 months and 25 days. A similar card in my older Pi2 has lasted 2,133 days so far (5 years, 10 months and 3 days) and is still going strong. I did nothing on either Pi to reduce the number of writes to the cards.

If you want to work out the age of your filing system, use the sudo lsblk command to find the name of the partition. Then use the command sudo dumpe2fs /dev/mmcblk0p2 | grep 'Filesystem created:' to get the date the filing system was created.

Whilst I do have all the data I needed backed up, I thought that it would be a useful exercise to see if I could actually recover the data from the card. In my case it was pretty easy - of course, your mileage is very likely to vary.

Always have a backup! :grinning:

In this case, the card wouldn't mount or be read either by Windows nor by Linux.

To have the best chance at getting a successful image of the broken card:

sudo apt install gddrescue
# Obviously change the block device id to the one you discovered using lsblk
sudo ddrescue -d -r3 /dev/mmcblk0 sd-card-copy-rescue.img sd-card-copy-rescue.logfile

This will take some time to run. you can adjust the settings if you like. -d uses direct disk access (not the kernel cache), -r3 means retry bad sector reads 3 times. Retry's can make matters worse of course on a failing disk but isn't likely to have a negative impact on a corrupted SD-Card.

Once you have a successful image (assuming it works), mount the image file:

sudo losetup --partscan --find --show ./sd-card-copy-rescue.img /dev/loop0
# You might want to check that /mnt is actually empty first or mount to a sub-folder
# In my case p2 is the root partition and p1 the boot partition. That should be common for Pi cards.
sudo mount /dev/loop0p2 /mnt
ls /mnt

For me, that worked just fine and seems to have recovered all files that I would be interested in. Again, it might not work for you and you should always have a backup of course.

An interesting exercise and didn't take very long even with the Google research to piece everything together. In a hurry, you could do it all in 1/2 hr. Anyway, documented here in case of future need.


And here i thought you were a windows man only !!

Just to add to this

  1. This requires another Linux system to run on - one that can physically access the SD Card i.e. some form of SD Card reader attached.

  2. This assumes the device you are going to write the rescued image to has enough space as this is a one for one block copy and hence will produce quite a large file.


1 Like

After 40+ years in IT, I've seen and done it all mate!

In any case, Windows has a pretty good version of Linux in it as well these days. Though I haven't tried using WSL for this, maybe I will just for the excersice though if I get time. My home server is an old Lenovo laptop running Debian which is my distro of choice. Thankfully it has a built-in card reader and 25GB of spinning rust. Running in low-power mode so no fans and <20w of 'leccy.

Hence another reason why I didn't worry about the card failure. Not much other than my WiFi controller running on that Pi. The controller is now running in Docker on the PC (yes, I know, I don't like Docker but A Java app that requires MongoDB and a bunch of other stuff is an ideal candidate for wrapping in Docker, especially as there is a well supported container for the Ubuquiti controller).

Quick additional note to say that as at Windows 10 21H1, WSL does not support hardware access to external drives. However it is supposed to be coming in the next release.

Get started mounting a Linux disk in WSL 2 | Microsoft Docs

1 Like

Ha ha well done.

I hadn't don much with Debian lately (other than on Pis) - usually use Centos and Ubuntu (and WSL) - ran up an older Dell Laptop on Debian the other day and could not believe what a pain it was to get all the right drivers etc installed !


1 Like

I generally use Debian because it is a good and stable server and is the basis for many other versions.

I generally get fed up with Ubuntu though I'm using it for WSL and I have a Docker Ubuntu remote VM running XFCE in case I want a simple Linux desktop.

The only thing I've not managed to get working on the laptop is sound which is a little annoying but about par for the course in my experience for Linux.

I generally use the Ubuntu Mate version as I find itā€™s not so ā€œtricked upā€ as the main version and the minimal install has all I need for a basic desktop (inc sound).

This topic was automatically closed after 30 days. New replies are no longer allowed.