I want to use
dd to clone my running Linux OS which is installed on a PI4 MicroSD 64GB media, to an image. Because it is a Raspberry PI I cannot (easily) boot from a USB stick and run the job with the PI4 OS not running. If I do such a job, using a console login, can I restore later without issue? Essentially can
dd create a proper, restorable image of the main drive (/dev/sda) from a terminal session from within a running system?
I want to use
There are two general types of methods how you can clone GNU/Linux to another hard drive, an SD card or a floppy. The first is when you clone a whole file system with everything it contains and when you copy all required files of your system and configure it to work somewhere else.
The first type: clone the whole file system
dd is the most reliable and old-fashioned way to clone the whole file system. If you want to make an image of your entire disk with all partitions in it you can simply execute in your terminal
dd if=/dev/sdX of=/path/backup_sdX.img
dd if=/path/backup_sdX.img of=/dev/sdY
to deploy your image on
sdY disk. It may require
sudo. backup_sdX.img will have the same size as the whole
lsblk to figure out
Be careful with dd command. It may irreversibly erase your operating system and all your data.
dd doesn't have a verbose option and you might want to see progress of dd with pipe data monitor pv. Make sure you've installed pv (for Debian or Ubuntu)
sudo apt-get install pv pv /dev/sdX | dd of=/path/backup_sdX.img
It may require
dd command. If you have SSH access from your Raspberry PI to another machine, then you can do
dd if=/dev/sdX | ssh username@hostname "dd of=/path/backup_sdX.img"
pv /dev/sdX | ssh username@hostname "dd of=/path/backup_sdX.img"
to see progress. hostname could be a local IP address of another machine. Make sure you have enough space on your receiving size because file.img will be the same size as your whole sdX disk.
You can even mount your image to your system.
sudo mount /path/backup_sdX.img /mnt
which could be helpful if you want to change something. If it won't work for some reason try this.
Why you might dislike this method:
- It may take a long time to copy each block of your disk, especially if that disk is huge.
sdYshould have a bigger size than
sdYwill have the same partitions of
sdXand will be simply resized to sdX (of course you can fix that later but with a risk to lose your data).
- You could be very disappointed if you mix up with
Once you understand all the advantages and disadvantages of using
dd command, you would probably want to try something more specific as
The second type: copy and configure
Simply clone the required files of your operating system and configure it. This is a much more advanced way to clone a system.
- make partitions on your target hard drive with
partedor any other program
- mount all required partitions
- transfer files using
rsyncwhich could be done via SSH or just DHCP network locally
- change /etc/fstab
- install or update grub or configure uefi (if you use it) though
A good guide on how to do this could be found here.