Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save michele-tn/306de7684deac6aa2dd5719707cc0041 to your computer and use it in GitHub Desktop.
Save michele-tn/306de7684deac6aa2dd5719707cc0041 to your computer and use it in GitHub Desktop.

πŸ›‘οΈ Backup and Restore GRUB Configuration to Prevent Boot Issues

Ensure your Linux system remains bootable by backing up GRUB and the Master Boot Record (MBR).


πŸ“˜ Overview

GRUB (GRand Unified Bootloader) is a critical part of the Linux boot process.
Misconfiguring or damaging it can lead to system boot failures.

This guide walks you through safely backing up and restoring GRUB to prevent such issues and guarantee recoverability.


πŸ” Backup GRUB

πŸ’‘ Before any system changes, always create a full backup of GRUB and your MBR.

🧭 Step 1: Check Disk Layout

Run the following command to inspect your current disk and partition layout:

df -h

πŸ“‚ Step 2: Backup GRUB Configuration

Create a copy of your GRUB directory:

sudo cp -r /boot/grub /boot/grub.bak

or

sudo cp -r /boot/efi /boot/efi.bak

πŸ’Ύ Step 3: Backup MBR

Replace /dev/vda with your disk if different:

sudo dd if=/dev/vda of=/boot/mbr_backup.img bs=512 count=1

πŸ“ This command saves the first 512 bytes of the disk (includes the MBR) into an image.


πŸ”„ Restore GRUB

⚠️ Use these steps only if GRUB is corrupted or the system is unbootable.

♻️ Step 1: Restore GRUB Configuration

Revert to your saved backup:

sudo mv /boot/grub.bak /boot/grub

or

sudo cp -r /boot/efi.bak/* /boot/efi/

πŸ’½ Step 2: Restore MBR

Write the backed-up image back to the disk:

sudo dd if=/boot/mbr_backup.img of=/dev/vda bs=512 count=1

πŸ”§ Step 3: Reinstall GRUB

If needed, perform a full reinstallation of GRUB:

sudo grub-install /dev/vda
sudo update-grub

βœ… Conclusion

Creating a backup of GRUB and the MBR is a simple but powerful step to prevent boot-related catastrophes.
By following this guide, you'll ensure you can quickly recover from misconfigurations or accidental changes.


πŸ“Ž Related Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment