How to create a disk or usb image, and compress it on the fly? And how to restore it?
I have own operating system on USB key. To create a full-backup and then possible restore to another device, I use linux command dd (dd – convert and copy a file).
Now, we must determine, on which patch we have s source disk. I my case, it is
sudo fdisk -l /dev/sdb Disk /dev/sdb: 29,5 GiB
First, I install additional software for monitoring and best compressing on more cores
sudo apt-get install pigz pv
Then, I create a full copy of the usb key. Without compression it takes 30GB, with compression, it take only 3GB. With command “pv” we can watch progress. Pigz compress the source image with multiple threads and cores. With parameter -c it writes all processed output to stdout. So with operand “>” we write this pigz output to a file:
sudo dd if=/dev/sdb | pv | pigz -c > /home/vasil/Documents/corsair-work.dd.gz
If we had som bad blocks on source disk, and we want to clone it anyway, we can use another conv options. Like:
conv=sync,noerror
This means:
- noerror – This makes use dd continue even after a read error is encountered;
- sync – This option has sense especially when used together with noerror.
In such a case the noerror
option will make dd continue running even if it a sector cannot be read successfully, and the sync
option will make so that the amount of data failed to be read its replaced by NULs
, so that the length of the data is preserved even if the actual data is lost (since it’s not possible to read it).
Then, I remove the source usb key and insert new one. It also has a path /dev/sdb. Now, I restore it with this command:
pigz -cdk Documents/corsair-work.dd.gz |pv| sudo dd of=/dev/sdb bs=4M
Parameter -c also write output to stdout and program dd writes it to disk. Parameter -k menas, that keep original file after decompress. And parameter -d means decompress.
Now, we can boot system with new usb key. And this image is identical as the source.
I hope, that this help someone. Have a nice day.