0

I have a raspberry pi backup.img file that I was trying to extract some of its inside files, I used different softwares to do that but all failed. I used Winzip but I got a message sys that this file is corrupted. The file size is 31G, it is an .img file of my Raspberry Pi and was on an SD card. I tried also Isoburn, File Repair,...but none of these software helped unfortunately! Is there any way to to do that whether on PC or Linux?

The SD card was working just fine on the Raspberry pi, but when I did a backup I used the dd command on Linux and cloned it and seemed to be successful! But when I tried to use the same SD card it just didn't boot up. After tons of attempts I just gave up and decided to format the SD card and load the cloned backup image. I used the dd command again and after finishing loading the image to the SD card nothing was working! Actually, there was no Boot file or even root files! So I suspected the SD card itself. I bought a new SD card yet it didn't work too. Then I said ok let me load an old .img that I have, and it worked on both SD cards, But I wanted to have this recent updated .img so badly because it has a lot of projects and stuff.

16
  • Hi and welcome to Stack Exchange @electech. How did you generate this backup image in the first place? If you followed a tutorial, or know the exact steps, please add them to your post.
    – David
    Mar 24 '19 at 22:08
  • If you've still got the original image (not modified by any software) why not just flash to an sd card and boot in Pi?
    – CoderMike
    Mar 24 '19 at 22:14
  • How have you created the backup if you used something like win32disk imager then you'll have backed up the file system as well, windows cant read the linux filesystem so says its corrupt. Try opening the image file on a working install of raspian or other linux distro and you should have more luck. As suggested above if it is a full image backup you could try flashing it to a new card and use it that way.
    – rohtua
    Mar 24 '19 at 22:34
  • please, see updated post
    – electech
    Mar 24 '19 at 22:43
  • I used the dd command line on linux and cloned it please show the command you used - if there's no boot partition, you probably only cloned the root partition - please check the boot partition which should be visible in windows Mar 24 '19 at 22:44
1

If you haven't destroyed the image with all your attempts with different tools you are able to mount it like an SD Card and get access to its files. For example I will take the Raspbian Buster Lite image how this can be done. Create a mount point:

linux ~$ sudo mkdir /mnt/img

Mount partitions from the image

Alternative 1:
Attach the image partitions to device files with:

linux ~$ sudo losetup --find --partscan --show 2019-09-26-raspbian-buster-lite.img
/dev/loop0

linux ~$ ls /dev/loop0*   # check if partitions are attached
/dev/loop0  /dev/loop0p1  /dev/loop0p2

Mount the partitions as usual:

linux ~$ sudo mount /dev/loop0p2 /mnt/img        # p2 = root partition
linux ~$ sudo mount /dev/loop0p1 /mnt/img/boot   # p1 = boot partition

Have attention to the order of the mount commands because the mount point for boot/ is only available after the first mount. Now go to section Copy data from the image.

Alternative 2:
If you want to know what's going on you can also mount the image partitions direct with offsets. You will find the correct values for your backup.img taking the example as template. Show the partitions inside the image with:

linux ~$ sudo parted 2019-09-26-raspbian-buster-lite.img unit s print
Model:  (file)
Disk /home/ingo/devel/raspi/2019-09-26-raspbian-buster-lite.img: 4390912s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start    End       Size      Type     File system  Flags
 1      8192s    532479s   524288s   primary  fat32        lba
 2      532480s  4390911s  3858432s  primary  ext4

You can see where the partitions start so you can mount them with giving the offset to the mount program. We have to do a bit arithmetic because parted shows sectors (512 bytes) not bytes.

linux ~$ sudo mount -o loop,offset=$((532480*512)),sizelimit=$((3858432*512)) 2019-09-26-raspbian-buster-lite.img /mnt/img
linux ~$ sudo mount -o loop,offset=$((8192*512)),sizelimit=$((524288*512)) 2019-09-26-raspbian-buster-lite.img /mnt/img/boot

Have attention to the order of the mount commands because the mount point for boot/ is only available after the first mount.

Copy data from the image

linux ~$ sudo ls /mnt/img
bin  boot  dev  etc  home  lib  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

linux ~$ sudo ls /mnt/img/boot
COPYING.linux           bcm2708-rpi-zero.dtb      bcm2711-rpi-4-b.dtb  fixup4cd.dat  issue.txt     start.elf     start_db.elf
--- snip ---

Copy what you want from the image and unmount it:

linux ~$ sudo umount /mnt/img/boot   # this first
linux ~$ sudo umount /mnt/img
linux ~$ sudo losetup --detach-all   # if you have used alternative 1
linux ~$ sudo rmdir /mnt/img
linux ~$
0

To restore a Raspbian image use the dd command in reverse. Use a Linux machine and follow the instructions in the official documentation.

The command you're looking for is

sudo dd bs=4M if=backup.img of=/dev/sdb
5
  • I will try that and let you know!
    – electech
    Mar 24 '19 at 23:22
  • it didn't work!
    – electech
    Mar 25 '19 at 2:14
  • 1
    @electech The didn't work is not helping. What did not work? Did the command failed? Did the sd card had no partitions after the restore? You need to be more specific.
    – GramThanos
    Mar 25 '19 at 12:44
  • @GramThomas Thanks fir your support ans willingness to help.... Yes, Ingo, the cmdline was: sudo dd if=/dev/sdb of=backup.img ... and what I meant bby it didnt work was the suggested cmdlune of David...
    – electech
    Mar 26 '19 at 13:11
  • @Ingo Thanks fir your support ans willingness to help.... Yes, Ingo, the cmdline was: sudo dd if=/dev/sdb of=backup.img ... and what I meant bby it didnt work was the suggested cmdlune of David...
    – electech
    Mar 26 '19 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.