4

I made regular complete backups of my hard disk (with all partitions) using dd tool as backup.img file. I have several such files.

Now that my old hard disk died, how can I use backup.img file to write on a new hard disk so that I have all the partitions and file intact as in the image.

Also is it possible to extract some part of data (few files/folders) from such file backup.img?

9

Restore

You can restore the image back to your replacement HDD with something along the lines of:

# dd if=backup.img of=/dev/sd?

You will end up with a clone of your original disk including all partitions and data.

The downside to this is that the partitions won't be resized by dd so your replacement disk must be identical to or larger in capacity than the original.

If you're happy with that you can use a bootable image that contains gparted or similar (such as Parted Magic) and resize the partitions to fit the new disk.

Mounting

To mount the image you'll need to use losetup.

Older Method

The older way to do this is by calculating the offset to the start of the partition, as follows:

Run fdisk and look for the sector size:

# fdisk -l backup.img
Disk backup.img: 8 GiB, 8589934592 bytes, 16777216 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x91999386

Device      Boot   Start      End  Sectors  Size Id Type
backup.img1 *       2048  1026047  1024000  500M 83 Linux
backup.img2      1026048 16777215 15751168  7.5G 8e Linux LVM

to calculate your offset, read the Start column for the partition you're interested in and multiply that value by your sector size. For example, to read backup.img1 above, multiply 2048 x 512 = 1048576.

Use this value as the offset for losetup as follows:

# losetup -o 1048576 /dev/loop0 backup.img

then mount the loopback device:

# mount -t ext4 /dev/loop0 /mnt

Later Method

Newer versions of losetup will scan and automatically create /dev/loop entries for all partitions in the image, as follows:

# losetup -Pf backup.img
# ls /dev/loop0*
/dev/loop0  /dev/loop0p1  /dev/loop0p2
# mount -t ext4 /dev/loop0p1 /mnt

LVM Partitions

My example above shows the second partition as LVM. To extract files from these, simply allow LVM to do it's magic on the loopback device. First scan:

# pvs
  PV           VG            Fmt  Attr PSize PFree 
  /dev/loop0p2 fedora-server lvm2 a--  7.51g 40.00m

then activate the VG (note the VG name from the above command, yours will probably be different to mine):

# vgchange -a y fedora-server
  2 logical volume(s) in volume group "fedora-server" now active

check what partitions you have:

# ls /dev/fedora-server/
root  swap

and mount one of them:

# mount -t ext4 /dev/fedora-server/root /mnt

Exit

Once you've extracted the files:

# umount /mnt
# losetup -d /dev/loop0

or for LVM mounts:

# umount /mnt
# vgchange -a n fedora-server
  0 logical volume(s) in volume group "fedora-server" now active
# losetup -d /dev/loop0
4
  • More recent versions of mount understand the loop option to set up a loop device for you and remove it on unmounting, e. g.: mount -o loop backup.img /mnt May 21 '16 at 18:15
  • 1
    @DavidFoerster - With partitions within the image? I've just tried that on Arch (util-linux 2.28) and it failed :-( May 21 '16 at 18:22
  • Oh, you're right. I missed the part of the question in parentheses about the image being of the entire disk. Nevermind then. May 21 '16 at 18:46
  • 1
    If you don't have losetup -P you still might not have to do manual offset calculation — just do losetup followed by kpartx -a. Should work on most systems under 10 years old.
    – hobbs
    May 21 '16 at 22:39

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.