How to make a disk copy with dd and gzip commands to compress de output.

Post Reply
valgonzalez
Site Admin
Posts: 56
Joined: Fri Aug 30, 2024 4:30 pm

How to make a disk copy with dd and gzip commands to compress de output.

Post by valgonzalez »

1. Identify the Source and Destination
First, you need to identify the source (input) and destination (output) devices. You can use tools like lsblk or fdisk -l to list the available block devices on your system.

2. Use dd to Copy the Disk
Use the dd command to copy the contents of the source disk to the destination disk. Adjust the input and output device accordingly.

sudo dd if=/dev/sdX of=/dev/sdY bs=4M status=progress

if=/dev/sdX: Specifies the input device (source).
of=/dev/sdY: Specifies the output device (destination).
bs=4M: Sets the block size to 4 megabytes for better performance.
status=progress: Displays the progress of the dd command.

3. Compress the Output with gzip
After copying the disk, you can use gzip to compress the output file. dd will create an image file on the destination disk, and you can pipe this through gzip to compress it.

sudo dd if=/dev/sdX bs=4M | gzip > /path/to/output/file.img.gz

Example with Input and Output Files
If you want to create an image file on your local machine instead of copying to another disk, you can use the following example:

sudo dd if=/dev/sdX bs=4M | gzip > /path/to/output/file.img.gz

if=/dev/sdX: Specifies the input device (source).
bs=4M: Sets the block size to 4 megabytes for better performance.
| gzip > /path/to/output/file.img.gz: Pipes the output of dd to gzip and writes the compressed image file.

Decompressing the Image
If you need to restore the image, you can use gunzip and dd in reverse:

gunzip -c /path/to/output/file.img.gz | sudo dd of=/dev/sdY bs=4M status=progress

This command decompresses the image file with gunzip and then writes it to the destination disk with dd. Adjust the input and output devices accordingly.
Remember to replace /dev/sdX, /dev/sdY, and /path/to/output/file.img.gz with your actual source, destination, and file path. Double-check everything before executing these commands to avoid data loss.
Post Reply