4

I created a new partition on my server /dev/sda4 with a filesystem based on ext3.

I mounted the new partition /dev/sda4 on /mnt/sda4/ and created an image with dd of the partition /dev/sda3. sda3 is mounted on the root folder (cd /) so it contains the most important files.

Creation of the image:

dd if=/dev/sda3 of=/mnt/sda4/backup.img

Then I removed a file on /var/www/html/ which is on the partition sda3. I tried to restore the image and it worked, but the file which I removed earlier isn't in back in place.

restoring:

dd if=/mnt/sda4/backup.img of=/dev/sda3

Output:

[root@h9999 sda4]# dd if=backup.img of=/dev/sda3
982128+0 records in
982128+0 records out
502849536 bytes (503 MB) copied, 1.13645 s, 442 MB/s

So actually it should work, shouldn't it?

5
  • What does mount say? /var might well be in another partition... Feb 5, 2016 at 12:02
  • You mean mounting the image file? I checked df -h and the /var folder isnt located elsewhere.
    – ugo
    Feb 5, 2016 at 12:05
  • Wait... Have you restored the backup while /dev/sda3 was still mounted?
    – lgeorget
    Feb 5, 2016 at 12:06
  • Hey Igeorget, yes I tried it. Since the /dev/sda3 is mounted on the root filesystem I cant unmount it because I then have no other place to save the image.
    – ugo
    Feb 5, 2016 at 13:25
  • 1
    @ugo that's pretty dangerous. dd works at byte level. If you modify the partition directly instead of through the filesystem while it's mounted you have a fair chance of corrupting it.
    – lgeorget
    Feb 6, 2016 at 13:10

1 Answer 1

3

There are several reasons why this may have failed: /var/www/html could actually be a symlink to somewhere else in your filesystem (try ls -la /var/www/ to see if there is a line like html -> /foo/bar/html. If /dev/sda3 was mounted during either of your dd processes, the filesystem may have been corrupted so that you don't see that file any more.

Using dd is in most cases not the best way to make a backup image of a disk, because dd copies everything including empty sectors and can only restore to a disk/partition of exactly the same size. Try partimage instead, which will copy only used sectors.

In most cases, using tar is actually the best option to create a backup of linux filesystems because this gives you independence of the filesystem and partition size used in the original partition and thus make the restoring process much more flexible. If you are worried about loosing the boot loader, the link I gave above also explains how you can restore the boot loader during the restoration process using a live cd, which is just a few additional lines of code apart from creating the partitions.

11
  • what does can only restore to a disk/partition of exactly the same size mean? i dont think that's how dd works. and the question does specify a partition image - not a filesystem.
    – mikeserv
    Feb 5, 2016 at 12:38
  • Hey Thawn, so to sum up. The partition does not have to be mounted or otherwise it can produce a corrupted image. But since /dev/sda3 is mounted on the root filesystem which also includes my /mnt/ folder, Its not really possible to unmount it. Here is what it says: umount: /: device is busy. Actually I want to create a full filesystem backup in order to recreate my server in a matter of minutes. How does Amazon Web Services does this? You can create there AMIs (Amazon Machine Images) in a few minutes...
    – ugo
    Feb 5, 2016 at 13:22
  • Update: tried it with partimage, same error, the partition has to be unmounted.
    – ugo
    Feb 5, 2016 at 16:57
  • That's why I recommended tar. That works on a mounted partition.
    – Thawn
    Feb 6, 2016 at 13:29
  • If you want to back up you root partition with dd or partimage, you have to boot from a live cd such as systemRescueCD.
    – Thawn
    Feb 6, 2016 at 13:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .