How to clone/migrate/backup EVERY system (even Windows with BitLocker!) from old to new disk/machine or file
Hi everyone! Today we're here to have a look at a script I use very frequently as a software developer & system consultant at my company Goldmark Solutions.
The problem
One problem I often face is that clients ask me to upgrade their servers or desktop workstations to an SSD from an old HDD drive, or maybe migrate from digital disk to virtual machine-compatible images for infrastructure migrations.
Compatibility
It has worked for me everytime, with Windows installations (even with BitLocker enabled!), Linux and macOS disks; it should be virtually compatible for every disk you throw at it, but make sure you have at least a destination disk of the same size as the origin one (or space, in case of backup to raw disk destination file)!
Finding the origin and destination disk names
The methodology to migrate a disk to another form or place is usually done by me with an Ubuntu or Arch Linux live ISO booted right from the current appliance installation, in which I already made sure to connect the future drive before booting it. After boot, I open a terminal and check via the following command for the device name of the "origin" disk to clone:
sudo fdisk -l
With that command, we will get disk and relative partitions for those disks: we only need to collect the disk name, not the partition names, e.g. if we have a disk called /dev/nvme0n1
with two partitions (/dev/nvme0n1p1
for boot and /dev/nvme0n1p2
for the root filesystem), we need the disk name /dev/nvme0n1
without the following p*
part, which is part of the partition naming scheme.
With that, in case of a disk-to-disk migration, we annotate also the disk of the "destination" installation SSD e.g. /dev/newdisk
.
If you wanted to make the destination a raw file for virtualization of physical machines (or also for backup dumps of the current whole state of the system for 1:1 restoration purposes), you can dump to a file instead of a physical disk and later backup the dump file to an external disk, network storage or somewhere else you have available.
The migration command
If you wanted to migrate from disk to disk, use the following command:
sudo dd if=/dev/nvme0n1 of=/dev/newdisk bs=1M conv=noerror,sync status=progress
If, instead, you wanted to make a backup image for further backup or virtual machine use, use this command:
sudo dd if=/dev/nvme0n1 of=./disk.raw bs=1M conv=noerror,sync status=progress
It will save the image of the disk to the current path at the file named disk.raw
.
How to restore from an image
If you, like me, use this command to create a full 1:1 image of the system for testing and restore, you can revert the command and go from file to disk like that:
sudo dd if=./disk.raw of=/dev/nvme0n1 bs=1M conv=noerror,sync status=progress
Conclusions
I hope it helped someone as it always has helped me in my work and homelab projects!
If you fancy see what I'm working on, have a look at my GitHub or write me an email!