Docu review done: Mon 06 May 2024 09:17:53 AM CEST
dd
Commands
Commands | Description |
---|---|
dd if=<source file name> of=<target file name> [Options] | copies block device from if-source to of-destinatnio |
dd if=<source file name> of=<target file name> status=progress [Options] | not to be used in scripts as it produces status output |
dd if=/dev/urandom of=<somfile> count=<howoften_bs> bs=1048576 | will generate a file with (<howoften_bx )MB as 1048576 bytes = 1Mb |
dd if=/dev/nvme0n1 of=/mnt/backup/disk.img bs=4M status=progress | creates a full disk image of nvme0n1 in this case which can be later used to restore to an earlier state. |
Example applications
Backup whole disk using dd before OS change or risky upgrade
First backup your whole disk using dd
Make sure you replace nvme0n1
with the correct device (use lsblk
to check) and disk.img with the name of the file you want.
dd if=/dev/nvme0n1 of=/mnt/backup/disk.img bs=4M status=progress
In case you need to do that via ssh you can also do that:
ssh sourcehost 'dd if=/dev/nvme0n1 bs=4M status=progress' > ./disk.img
If you need to extract single files from that file you can do that by creating a loop device and mount that.
Make sure you’ve got e.g. ntfs-3g installed in case this is a ntfs dump.
Since the imgage contains all partitions we need to first find the start sector of the intended disk by executing
fdisk -l disk.img
Output looks similar to this:
Device Start End Sectors Size Type
win10Disk.img1 34 262177 262144 128M Microsoft reserved
win10Disk.img2 264192 878591 614400 300M Windows recovery environment
win10Disk.img3 878592 1083391 204800 100M EFI System
win10Disk.img4 1083392 1000214527 999131136 476,4G Microsoft basic data
Get the start of the intended partition, in this case partition number 4: 1083392 and use it in this command:
sudo losetup --find --show --offset $((1083392 * 512)) win10Disk.img
this will output the created loop device (e.g. /dev/loop0
) which can then be mounted using
sudo mount /dev/loop0 /mnt
when done pulling files or looking things up you can easily unmount everything again:
sudo umount /mnt
sudo losetup -d /dev/loop0
in case you want to restore the disk to the state when pulling the image you can restore it using this command:
sudo dd if=./disk.img of=/dev/nvme0n1 bs=4M status=progress
Get status if you need it and for got status=progress
Open another sessoin (ssh/term) and run the following command:
$ kill -USR1 $(pgrep ^dd)
It will send a signal to dd that it prints out the current status like:
3053+3 records in
3053+2 records out
12809067752 bytes (13 GB) copied, 1222.97 s, 10.5 MB/s