4

I've tried using dd to back up my computer's hard drive, but I met with some rather confusing results. I've seen on multiple sources (e.g.: http://www.backuphowto.info/linux-backup-hard-disk-clone-dd, http://debianhelp.co.uk/ddcommand.htm) that they include something of the sort:

dd if=/dev/sda of=~/backup.img

1) Unless I'm very confused, this will try backing up the entire disk to a file on the disk, won't it?

I have attempted to use this method, but first compressed the output to save space. In order to prevent the compressed file from taking up a bunch of space by trying to copy all the free data, I used zerofree to write 0s to all my disk's unused space. My disk has a total capacity of ~320GB, and about 100GB of that is used, leaving ~220GB free.

When I tried using the following command:

dd if=/dev/sda | gzip -c > ~/image.img.gz

The backed up file reached a size of ~160GB before I quit the backup (with the intent of figuring out how to back up to an external disk, a task at which I've since succeeded).

2) Why would the backup take up so much space? Is it because dd sees the file to which it's been writing and then tries backing that up as well? Or is it possible that I didn't manage to use zerofree quite properly, and so there's just a bunch of random junk data being backed up despite my efforts to prevent that from happening?

Steven
  • 27,531
  • 11
  • 97
  • 118
Ben Sandeen
  • 211
  • 2
  • 4
  • 11
  • 2
    You cannot use a sector-based tool such as `dd` to backup a running drive. – kinokijuf Oct 01 '15 at 17:07
  • Then what could it be writing to the file? And would I then have to perform the backup on a live boot session? – Ben Sandeen Oct 01 '15 at 17:14
  • 1
    You can use this if: 1) You write to another disk than you are backing up. 2) The Disk you are backing up is not being written to. Else you might end up with an inconsistent image. Booting a la 'root=/dev/sda1 ro init=/bin/bash' (or whatever the modern incantation is) and then doing a backup to a different drive (or via netcat over the network) should work. – Hennes Oct 01 '15 at 18:50

1 Answers1

3

You cannot accurately duplicate a hard-drive while it's being modified.

Unless I'm very confused, this will try backing up the entire disk to a file on the disk, won't it?

Yes, it will try. The dd command will read a block (from the drive), compress the block, write the block (to a file on that drive), and go on to the next block. The image will have a partially written backup.img and partially written filesystem pointers to that file.

Note: I greatly simplified the pipe buffering between dd and gzip as the details are not relevant here.

Why would the backup take up so much space?

The likely cause is that it's trying to copy the partially written backup.img file. Other factors require advanced knowledge of the filesystem structures for the filesystem you're using.

Steven
  • 27,531
  • 11
  • 97
  • 118