Cloning a Drive in Linux via Commands
It's been a long journey. A lot of software and methods have been tried over the years. All of them work, but with varying degrees of difficulty.
Wait, let's back up (so to speak) and lay out the objective: A Bare Metal Recovery Methodology that allows for the complete backup of an entire Rocky Linux OS based on a 'live / running' Source Drive that can then be restored on a Destination or Target Drive.
Here we go...
Premise and Circumstances
None of this works unless the drive structure has been set up properly. In this instance, things are about as plain and simple as it gets. A single GPT Partitioned Drive with the ROOT File System as EXT4 on an LVM Partition (has to be on an LVM Partition to allow for 'SnapShots').
Below is the Example Drive;
nvme1n1 259:2 0 238.5G 0 disk
├─nvme1n1p1 259:3 0 1G 0 part /boot/efi
├─nvme1n1p2 259:4 0 4G 0 part /boot
├─nvme1n1p3 259:5 0 128G 0 part
│ └─VG.NVMe.P3-LV.ROOT 253:0 0 64G 0 lvm /
├─nvme1n1p4 259:6 0 80G 0 part
│ └─VG.NVMe.P4-LV.Storage 253:1 0 60G 0 lvm /mnt/NVMEx1
└─nvme1n1p5 259:7 0 20G 0 part [SWAP]The Steps
Step(s) 1 - BackUp
- BackUp the Source Drive Structure:
sgdisk --backup=nvme1n1.bak /dev/nvme1n1 - BackUp each of the Partitions;
- nvme1n1p1:
dd if=/dev/nvme1n1p1 of=/BackUps/nvme1n1p1 bs=64M iflag=fullblock status=progress conv=noerror,sync,fsync - nvme1n1p2:
dd if=/dev/nvme1n1p2 of=/BackUps/nvme1n1p2 bs=64M iflag=fullblock status=progress conv=noerror,sync,fsync - nvme1n1p3;
- LVM Configuration:
dd if=/dev/nvme1n1p3 of=/BackUps/nvme1n1p3.LVM bs=512 count=2048 iflag=fullblock status=progress conv=noerror,sync - Create a Snapshot:
lvcreate --snapshot -l 100%FREE --name "LV.ROOT.SnapShot" "/dev/VG.NVMe.P3/LV.ROOT" - File System:
dd if=/dev/VG.NVMe.P3/LV.ROOT.SnapShot bs=64M iflag=fullblock status=progress conv=noerror,sync | pigz -1 -c > /BackUps/LV.ROOT.SnapShot.gz - Delete the SnapShot:
lvremove -f /dev/VG.NVMe.P3/LV.ROOT.SnapShot
- LVM Configuration:
- nvme1n1p4;
- LVM Configuration:
dd if=/dev/nvme1n1p4 of=/BackUps/nvme1n1p4.LVM bs=512 count=2048 iflag=fullblock status=progress conv=noerror,sync - Create a Snapshot:
lvcreate --snapshot -l 100%FREE --name "LV.Storage.SnapShot" "/dev/VG.NVMe.P4/LV.Storage" - File System:
dd if=/dev/VG.NVMe.P4/LV.Storage.SnapShot bs=64M iflag=fullblock status=progress conv=noerror,sync | pigz -1 -c > /BackUps/LV.Storage.SnapShot.gz - Delete the SnapShot:
lvremove -f /dev/VG.NVMe.P4/LV.Storage.SnapShot
- LVM Configuration:
- nvme1n1p5:
dd if=/dev/nvme1n1p5 of=/BackUps/nvme1n1p5 bs=64M iflag=fullblock status=progress conv=noerror,sync,fsync
- nvme1n1p1:
Step(s) 2 - Restoration
Restore to the Destination Drive: sgdisk --load-backup=WhatEverFileName.bak /dev/WhatEverSDy
Re-Read Drive Information on the Destination System: partprobe /dev/WhatEverSDy
If this error occurs: "Not all of the space available to /dev/sdc appears to be used, you can fix the GPT to use all of the space (an extra 2014 blocks) or
continue with the current setting?", it is probably because of a mismatch in size between the Source and Destination Drives Storage Capacity. Easy to fix with: parted /dev/SDy, print, Fix
And for the Critics
nvme1n1p1 and nvme1n1p2: Yes it would be great if a SnapShot could be taken of these too, but as far as research and experiments have shown, UEFI will not tolerate that, so it won't work. Plus, realistically, what's gonna change on those Partitions in the brief time DD is running for them? Hint: NOTHING!
SWAP: Yes, the SWAP partition could just be recreated, and it won't be consistent, etc. when cloned with DD, but it doesn't make a difference at all because when the cloned system boots, the SWAP Partition is essentially reset. Interesting question on which is faster, and whether one could run the various commands to create a new SWAP Partition, with the same parameters, UUID, etc. VS running the DD restore command.