20 Nov 2018

Rescue files from a failing Raspberry Pi SD card

Source code

I recently had a problem with a particular Raspberry Pi and was able to rescue some files. I thought I’d document this here in case it’s useful.

Raspberry Pi SD card mcc0 errors

When powered on, I got an endless output of “mmc0: fsm 1, hsts 1” scrolling on the screen. Not good!

My limited understanding of this error is that “mmc” means “multi-media card”, so the Raspberry Pi isn’t happy with the SD card. I’m not sure what caused this, but I suspect it’s corruption from age (this Raspberry Pi has been running 24/7 for a couple years), or something may have been corrupted due to power failure (despite using ext4).

I can easily flash a new SD card, but I wanted to try and retrieve the MySQL database from the broken SD card if possible. To copy to and from SD cards with my Linux PC, I usually use the dd command. However, this didn’t work in this situation because dd will exit as soon as it encounters an error, so you generally won’t get anywhere near a full image if there is a problem.

Instead I used ddrescue, like this…

First install ddrescue:

$ sudo apt-get install ddrescue

Note: on Linux Mint 20 the package is gddrescue

Next, run ddrescue on the damaged filesystem:

$ sudo ddrescue -dr3 /dev/sdc rescue.img ddrescue.log

This will take a very long time! To rescue an 8GB SD card took me about 15+ hours, so I left it running overnight.

The next day, it had finished with this result:

$ sudo ddrescue -dr3 /dev/sdc rescue.img ddrescue.log
GNU ddrescue 1.22
ipos: 7334 MB, non-trimmed: 0 B, current rate: 0 B/s
opos: 7334 MB, non-scraped: 0 B, average rate: 144 kB/s
non-tried: 0 B, bad-sector: 328192 B, error rate: 21 B/s
rescued: 7947 MB, bad areas: 127, run time: 15h 17m 18s
pct rescued: 99.99%, read errors: 2717, remaining time: 5h 21m
time since last successful read: 1m 36s
Finished

99.99% rescued is a promising result. 🙂

The raspberry pi boot partition was still not recognised, so it wouldn’t boot (just a blank screen). But I was able to read the root partition on another Linux PC and had access to all my files again for restoration.

From here I was able to retrieve some mysql data files and restore them on a fresh Raspberry Pi SD card installation.

These instructions may or may not work for you. I’ve had plenty of other corrupt SD cards that I was unable to rescue. Good luck!

Update

I found a very nice package called ddrescueview that will display a visual representation of the ddrescue operation by monitoring the log file as it’s running, as shown here:

ddrescueview screenshot

To do this:

  • Run the ddrescue command as shown above,
  • then run ddrescueview,
  • Go to File > Open mapfile and select ddrescue.log,
  • Now go to Options > Automatic refresh > 10 seconds, and it will track the progress of the rescue operation.

About the Author:

Hardware and software engineer with experience in product development and building automation. Director at Cabot Technologies and Product Manager at NEX Data Management Systems.

3 comments

  1. Ben

    I’ve always heard you shouldn’t use SD cards for write intensive applications like databases since they wear out. Good to know that advice isn’t relevant since SD cards will die of other causes first.

  2. Lukas Rox

    Hi Darian,

    Instead of ddrescue (which is perfectly fine tool for this job) I’ve used:

    dd if=old_card of=new_card bs=512 count=16777216 conv=sync,noerror

    Small block size is important: the smaller this is, the smaller the gap in data will be. The above values are for an 8 GB card.

    PS. Laptop with two built in SD readers is a great tool for this! Did this on Acer Aspire One AOA 110

  3. Gorblimey

    I started the rescue process on my 16gb sd card and it took all of 6 secs. I opened the rescue.img in gparted and it says 512Kib Unallocated. Does that mean the data is non-salvageable?

Leave a Reply

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.