Recovering data from a Linux box using a live USB thumb drive
Occasioned by an imperfect backup strategy (ahem) and a used ThinkPad off eBay that may just be a little dodgy, here are my notes on how to recover data from a Linux machine that refuses to boot.
I’m mainly writing this down to save myself some trouble the next time this happens, as it inevitably will.
Starting situation: The computer turns on fine, but won’t boot into the installed Linux OS.
Here’s what I did. Note: Unless you have access to a USB thumb drive with a live system on it, you will need another computer in working order to do this stuff.
What you’ll need
- A second computer in working order
- An USB thumb drives with at least 4GB of capacity
- An internet connection
- A backup hard drive that’s large enough to hold all the data you want to rescue.
Check that the problem is the computer, not you
First off, I verify that it’s actually a real problem, rather than just a dumb user (ie. me): Check that I don’t have any external boot media plugged in. No USB drives, no SD cards hidden in any slots.
Seems like the problem really is on the computer itself. We can’t boot into the OS. But we can make a USB thumb drive with a live system on it!
Preparing a live USB thumb drive
Find a USB thumb drive with at least 4GB of capacity. (Note to self: that orange one with the bottle opener on the end works fine!)
Make sure there’s nothing stored on that drive that you want to keep. It’ll all get overwritten by what happens next.
Download a bootable distro image. How about some nice Debian? You’re looking for an .iso file.
Install a tool to turn USB thumb drives into bootable media. In the blessed year of 2023, the one you want is probably Balena Etcher.
Plug in the USB thumb drive. Start Balena Etcher (or whatever you’re using), and follow its instructions to put the ISO file on the thumb drive.
Once that’s done, it’s finally time to rescue your data.
Using the live system to rescue your data
The instructions for the following steps are from this post by that most prolific source of help: random people on the internet.
All of the following steps happen on the failed machine.
Plug in your USB thumb drive with the live system, and turn on the computer. With any luck, you’ll get a boot menu where you can select the live system. If you don’t get the boot menu, you might have to change the boot order in the BIOS. (It’s no big deal, but I won’t go into this here.)
Once your live system is booted up, open a terminal. That’s where all the following stuff happens.
Get admin rights: sudo -l (there’s no root password on a live system.)
Create a mount point for the drive on the broken computer that has your data on it:
sudo mkdir /mnt/recovery
Try to figure out which is the disk where your data is stored:
sudo fdisk -l
To quote the source of this part of the howto:
> Some educated guesswork from the results is required here, which will provide the device filesystem Type; it almost certainly isn't an EFI System, or Linux swap for example. Typically one is trying to access something like /dev/sdaX.
Let’s say your data is stored on device sda2. Then the next commands are (adapt device name as needed):
Mount the device with your data to the mount point that you’ve just created:
mount /dev/sda2 /mnt/recovery
Plug in your backup medium. If everything goes right, the live system should automatically mount it to something like /media/user/YOUR-BACKUP-DRIVE.
Go to where your data is stored:
sudo cd /mnt/recovery
A quick “ls” should show you the user directories.
Copy the data over to the backup drive. Depending on how much data we’re talking about, that can take quite a lot of time:
cp -r your-user-directory /media/user/YOUR-BACKPUP-DRIVE
The rest is basically waiting. Feel free to say a prayer to the deity of your choice, especially if you don’t have backups. (Like me. I’m an idiot. Make backups. Don’t be like me.)
Once everything is done, you can just hit the “Install” button on your live system, and install a fresh OS.