Backup your system disk when its not active
Boot-up another system with your system disk attached. Mount it and backup all system files:
mount ${srcdev} /mnt
cd /mnt
tar --exclude sys.tar.bz2 -czpf sys.tar.bz2 ./
umount /mnt
-z - infer the compression format from the extension
-p - save all permission flags
Copy sys.tar.bz2 to your backups.
Backup your dot files
cd ${HOME}
tar -czpvf ${HOME}/dot_files.tar.bz2 $(find ./ -maxdepth 1 -path './\.*')
In addition to the options above I added -v to show the list of the files being archived. I didn't do it in the previous command because it's a significant hit on performance.
Restore the system files
Mount the new empty formatted system partition:
mount ${tgtdev} /mnt
Decompress the system files:
cd /mnt
tar -xpf /home/sys.tar.bz2
chroot to the restored system files
cd /mnt
mount -t proc /proc proc/
mount --rbind /sys sys/
mount --rbind /dev dev/
mount --rbind /run run/
chroot /mnt /bin/bash
Activate MBS (Master Boot Record) and install Grub 2
grub-install --target=i386-pc ${tgtdisk}
Note that tgtdisk should the the disk, not the partition. For example: /dev/sdd
Now we need to generate and update grub.cfg in the new system.
cd /boot/grub
grub-mkconfig -o new.cfg
Check that the config makes sense and replace the current config:
mv new.cfg grub.cfg
Restore the home dot files
useradd -m user01
cd /home/user01
tar -xpvf /home/dot_files.tar.bz2
Custom grub menu entry
Sometimes you may need to use labels instead of disk's UUID to generate grub.cfg. This is required when you detach the disk and use it in another VM host. The UUID can change and the generated grub.cfg can become invalid.
Create custom.cfg in /boot/grub directory with the following content:
menuentry "Linux Today" --id ubuntu-by-label {
insmod gzio
insmod part_gpt
insmod ext2
search --no-floppy --set=root --label systemroot
linux /boot/vmlinuz-5.4.0-33-generic root=LABEL=systemroot ro maybe-ubiquity
initrd /boot/initrd.img-5.4.0-33-generic
}
Open /etc/default/grub and update GRUB_DEFAULT:
GRUB_DEFAULT=ubuntu-by-label
Generate grub.cfg as in "Activate MBS (Master Boot Record) and install Grub 2" above. For curious, custom.cfg is a special name that the default configuration includes from grub.cfg.