Recovering data from ext4 to swap partition?

Asked by Sybillance

I accidentally turned my ext4 partition (/dev/sda5 - main storage) to a swap partition through a live USB. I haven't touched the partition at all so I think the data inside should be safe.

Is there a way to turn it back into ext4 w/o losing the data inside? Rewrite the partition table w/o having to reformat?

I ran

mkswap /dev/sda5

output:

mkswap: /dev/sda5: warning: wiping old ext4 signature.
Setting up swapspace version 1, size = 469.4 GiB (503968690176 bytes)
no label, UUID=a34aa5c7-f8ca-4198-9d77-207e153ffbb0

I'm looking for any solutions from safely backing up the partition as image for later, to rewriting the partition table, partial/full data recovery, etc. Any process to get the data back from a partition that was turned from ext4 to swap.

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Sybillance
Solved:
Last query:
Last reply:
Revision history for this message
actionparsnip (andrew-woodhead666) said :
#1

Switch it to Ext4 and you may find foremost will work in a live CD desktop. You will loose filename and location data but the files may be recovered. You will need a writable partition of equal or greater size than the partition you are working with to spit the files out to.

Alternatively, use your backups. You do take a regular backup of your important data, right?

Revision history for this message
Sybillance (marching365) said :
#2

I do have some backups but i'm finding that there's a possibility of solving this by "renaming" the partition label via this:

http://shearer.org/Ext4_recovery

scroll to bottom.

essentially running this and praying for the best:

mkfs.ext4 -S /dev/YOUR_DEVICE

I've been testing it out on a laptop i have and it seems to be working fine... not sure if i want to make a big go of it just yet.

Revision history for this message
actionparsnip (andrew-woodhead666) said :
#3

You could try:

resize2fs

And pretend you extended the partition. The files may pop up.

Revision history for this message
Manfred Hampl (m-hampl) said :
#4

If there is a reasonable method to recover from your situation, then most probably it is the "mkfs.ext4 ..." command.

As an additional step before trying it, you might make a backup copy of the whole hard disk to another medium, to give you a chance for a second try if the mkfs command does not help. (See first lines in the recovery paragraph at http://shearer.org/Ext4_recovery#Recovery_Technique )

Revision history for this message
Sybillance (marching365) said :
#5

Yup. Got it done!

It was certainly a nail biting procedure... I ran

    sudo mkfs.ext4 -S /dev/YOUR SDA PARTITION NUMBER (insert sda1, sda2, etc.)

And it asked if I was certain... it could break files, break the whole partition, etc. I went for it, and it was done.

then I ran

    sudo fsck.ext4 -v /dev/YOUR SDA PARTION NUMBER (sda1, sda2, etc.)

and it turned out my hard drive had a good amount of bad sectors and it prompted me to whether they should fix/clear certain things I had no idea what they're talking about. htree index, index_fl flag, dtime, etc etc. I had no idea what I was doing and started with going through each one individually but there were thousands... so i just held [y] for a good three minutes. Scary stuff.

All in all a bit of a headache because it screwed up my root partition so I had to reinstall the Ubuntu OS. It just wouldn't boot otherwise.

Good news is that somehow... it kept all my system settings during the reinstall. I linked my home partition to root and rebooted. When I boot firefox, it brought up all my tabs. I installed chrome, it brought up all my chrome tabs, installed calibre, linked immediately to my old calibre library. Amazing.

So far, it looks like the files are all intact and okay, but it's literally thousands of files so I wouldn't know there's a problem until I run across a file I can't access or play.

Additional question for those interested: I did back up my /root folder but I'm not sure if it's necessary to move that back to the new install as everything seems to be working. Should I do it anyway? Thanks for your input everyone!

Revision history for this message
Sybillance (marching365) said :
#6

WARNING - I DIDN'T MENTION IN THE LAST POST

I RAN ALL OF THIS FROM A LIVE UBUNTU USB WITHOUT EVER REBOOTING THE DRIVE

ONCE I REALIZED WHAT I DID I LEFT EVERYTHING AS IS UNTIL I FOUND THIS SOLUTION

DO NOT BOOT BACK INTO THE UBUNTU PARTITION OR YOU MAY OVERWRITE THE PARTITION WHERE YOU RENAMED THE FILE SYSTEM

For those finding this post in the future: It also kept the file addresses/names intact... as far as I can gather. It's about 500gb of data so it's hard to know if everything went well as I think it did especially after finding bad sectors in the disk.