How To Backup And Restore Grub Bootloader?


Grub is a bootloader that is used to manage the multiple operating systems installed on a computer. A boot loader is the first program that runs when a computer starts. It is responsible for loading and transferring control to the operating system’s kernel.

The GNU GRUB is derived from the GRand Unified Bootloader, which was originally developed by Erich Stefan Boleyn and was predominantly used for Unix-like systems. An error in Grub can prevent your system from opening.

The best way to prepare for this is by taking a backup of grub bootloader settings and configuration and restore it whenever required. In this article, I will discuss how to backup and restore the Grub bootloader.

Grub: Get a backup from MBR

Many computer systems are transitioning from BIOS to EFI but most of them are still using BIOS. If your system is using BIOS then the grub installed on your system uses MBR (Master Boot Record). During the installation of Linux on these systems, the bootloader is installed on the very first sector of your hard drive.

BIOS is the first program that starts when a system boots, it reads the first sector of the hard disk and establishes a few services. It then loads all the operating systems on the system by starting the bootloader.

Taking backup of grub is very easy on the BIOS system you just need to copy the MBR sectors where the bootloader is installed to a text file. Now to take the backup first open the terminal on your system by pressing Ctrl+Alt+T and then use –

lsblk

This will display all the partitions on your system with some other information such as size, mount point, etc. In this output find the partition with / mount point for example  /dev/sda

dd if=/dev/sda  of=/home/username/mbr.txt count=1 bs=512

Don’t forget to replace the given drive with the actual one on your system. Also, replace the /home/username/ with the location where you want to save the backup file. Once done move the mbr.txt to some external drive to keep it safe.

How to restore MBR backup

To restore from MBR backup on your broken Linux pc. Boot into the system using a live disk and then copy the mbr.txt to your home directory lets say it is /home/username.

Now use the given command to restore grub –

dd if=/home/username/mbr.txt of=/dev/sda count=1 bs=512

Backup Grub in UEFI systems

UEFI(Unified extensible firmware interface ) is the firmware interface that is intended to replaces the legacy BIOS. It works a little bit differently it stores all the information such as initialization and startup of a system in a .efi file which resides in a separate partition called EFI system partition(ESP). This partition also contains the bootloader program required for booting an operating system.

To create a complete Grub configuration file backup, first, create a backup folder in your home directory –

mkdir -p ~/grub-backup-efi

Now we will copy all the grub configuration files into this newly created directory.

sudo cp /etc/default/grub ~/grub-backup-efi/

Next copy grub bootloader entries from the /etc/grub.d/ directory –

sudo cp -R /etc/grub.d/ ~/grub-backup-efi/

Now move this grub-backup-efi to a external drive.

Restore Grub on UEFI systems

Now when your system is in trouble you can restore grub from the created backup. To restore grub copy the grub-backup-efi directory from external drive to your home directory.

Next move to backup directory –

cd grub-backup-efi

By using mv command restore the grub files to /etc/default directory –

sudo mv grub /etc/default/

Further change directory to grub.d and restore your Grub bootloader entries to the /etc/default/grub.d/ folder –

cd grub.d/
sudo mv * /etc/grub.d/

Finally update grub on your system by using –

update-grub

Conclusion

Ok now you know how to backup and restore grub bootloader on a Linux system. Now if you have a query then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.