15
13
Can I use dd, rsync, clonezilla or any tool to clone only space in use in my hard disk in Linux? I need to do a backup from a 1 TB HD (with only 2 GB space in use) into a 500 GB HD.
15
13
Can I use dd, rsync, clonezilla or any tool to clone only space in use in my hard disk in Linux? I need to do a backup from a 1 TB HD (with only 2 GB space in use) into a 500 GB HD.
24
You can, but you should prepare your disk first. The trick is to use sparse file or compression. This method is time consuming, it generates high I/O. In your case (2GB in use on 1 TB HDD) a file copy (as suggested in sawdust's comment) will probably be a way better solution. If – on the other hand – you had e.g. 850 GB in use out of 1 TB, many small files therein, you wanted to backup MBR, partition table, metadata, all that at once – then my method would be a reasonable way to save at least 150 GB on the image file (which still couldn't fit into 500 GB HDD, unless the data compressed well enough).
I'm writing this for users with higher disk usage. Also note that the source drive should be healthy and allow to overwrite the empty space. I'm giving the solution mainly for backup, not recovery nor forensics. The time and I/O cost will be paid not only during image creation but also when (if) the image is written back to disk. Think twice if the method is right for you.
Let's say you need to clone /dev/sdb
and there are several partitions: /dev/sdb1
, /dev/sdb2
…
To take high advantage of sparse files or compression you should overwrite the empty space with zeros:
## Most commands need sudo.
mount -o rw /dev/sdb1 /mnt
dd if=/dev/zero of=/mnt/zero_file bs=32M
## Long wait here. Expect "no space left on device".
sync
rm /mnt/zero_file
umount /dev/sdb1
## Repeat this with /dev/sdb2, /dev/sdb3 etc.
In case of Windows partition there may be some trouble due to Windows hibernation. Read this.
If there are major gaps in the partition layout then you should also fill them up with zeros. Swap partitions (if any) need special treatment in order to make the resulting image as small as possible. The Windows files like hiberfil.sys
, pagefile.sys
and swapfile.sys
may be removed before zero_file
creation. I won't cover these cases in detail here.
This method may be used if the target filesystem (where the image file will be saved) supports sparse files. To generate a sparse image file, invoke:
## dd probably needs sudo here.
dd if=/dev/sdb of=/foo/bar/my_image.dd bs=512 conv=sparse
(EDIT: originally there was bs=32M
but it's not the good choice with conv=sparse
. Compare this question.)
To write the image back:
## dd probably needs sudo here.
dd if=/foo/bar/my_image.dd of=/dev/sdb bs=32M
Advantages:
mount -o offset=…
or use kpartx
) to access the files within.Disadvantages:
cp --sparse=always
).To generate the image:
## dd probably needs sudo here.
dd if=/dev/sdb bs=32M | gzip -c > /foo/bar/my_image.dd.gz
To write the image back:
## dd probably needs sudo here.
gzip -cd < /foo/bar/my_image.dd.gz | dd of=/dev/sdb bs=32M
These commands might be built without dd
, with gzip
only. I used dd
to ensure 32 MiB buffer.
Advantages:
Disadvantages:
Long after I wrote the first version of this answer I learnt there is virt-sparsify
tool. It looks useful.
To monitor the progress invoke dd
with status=progress
operand. If dd
is already running without it (e.g. your dd
doesn't support status=progress
or you forgot to use it), send USR1
signal to the tool:
kill -s USR1 $(pidof dd)
and repeat as needed.
As an alternative you may use pv
to read. Examples:
pv -B 32m /dev/sdb | dd of=/foo/bar/my_image.dd bs=512 conv=sparse
pv -B 32m /dev/sdb | gzip -c > /foo/bar/my_image.dd.gz
To compress fast use gzip --fast
, to compress best use gzip --best
. Refer to man gzip
for more options.
Use pigz
instead of gzip
if you can. This should speed things up, because pigz
can utilize more than one processor core. You can use another compressor if you like.
1Thanks for sharing such an awesome answer. Something I noticed that you might want to mention is that ls and du will report different file sizes when constructing images that are sparse. As far as I can tell dd will report the larger of the two sizes, as does ls however du will print a more accurate size of the file as it exists on the file system. – ipatch – 2019-01-24T15:51:10.300
1
If the target disk is already formatted, the second disk is plugged into the same machine as the first, is mounted, and if you're running Linux or Mac:
rsync -avP --ignore=/media/disk2 / /media/disk2
If the target disk is already formatted, the second disk is formatted and mounted into another PC, and if you're running Linux or Mac:
rsync -avP / user@ip_of_disk2_host:/media/disk2
This assumes you're just wanting a backup of the files without regard to the underlying drive. This does a PER FILE backup and will run rather quickly on only 2 GB of data.
-2
Yeah, the "copy over drive" part is kind of the entire point of the Q. You missed that. – RichieHH – 2020-02-19T22:44:43.240
2No, you would need to use a file copy rather than a block copy or cloning tool. – sawdust – 2016-07-05T17:38:32.233
1I'm not sure, but partclone might let you do this. – Darren – 2017-01-28T13:09:59.330