How to back up the Linux bootloader to USB for emergencies
The bootloader that most Linux operating systems go with is known as Grub. It’s a simple tool that can be configured to boot up your Linux distribution, as well as other operating systems all from a menu when your PC starts up.
Grub is a reliable tool compared to other alternative Linux bootloaders, and for this reason, it has remained a favorite in the community. That said, it’s not perfect. For as reliable as it is, errors can happen, and Grub can break.
The best way to prepare for when your Grub bootloader takes a turn for the worst is to back up the Linux bootloader to an external hard drive or USB stick.
BIOS Grub – Backing up MBR
While many Linux users are transitioning to using EFI as the standard, lots of users still use the BIOS version of Grub, because not every computer can run EFI well. If you have a BIOS install of Linux, your Grub bootloader makes use of Master Boot Record. This means that during your Linux OS’s installation, the bootloader was installed in the very first sectors on your hard drive, rather than in a folder, like with Grub EFI varients.
The good thing about MBR is that it’s very easy to make a complete backup of Grub, by copying the MBR sectors of your Linux install to a TXT file. To start the process, open up a terminal window by pressing Ctrl + Alt + T or Ctrl + Shift + T on the keyboard. Then, use the lsblk command to identify what your primary Linux hard drive is.
Look through the printout of the lsblk command in the terminal and find the partition that has “/” under the mount-point. This command should help you find what your hard drive label is. In this example, the drive label will be “/dev/sda.”
In the terminal, use the DD command to create a backup of your Master Boot Record to a TXT file.
Note: you must change
/home/username/ in the command below to the username on your Linux PC! Be sure to also change /dev/sda to reflect your actual hard drive’s label.
dd if=/dev/sda of=/home/username/master-boot-record.txt count=1 bs=512
The DD tool will create a backup reasonably fast. When it’s done, you’ll have a file in the home directory (~/) labeled “master-boot-record.txt.” Take it and place it on a USB flash drive or USB external hard drive for safe keeping.
Restore MBR backup
Need to restore your MBR backup? Boot into your broken Linux PC’s system with one of these Grub tools here. Then, open up a terminal, connect your USB device that has the “master-boot-record.txt” file, place the file back into your home directory (~/), and run the following command.
Note: like before, it’s critical that you change
/dev/sda to reflect your username and actual hard drive’s label.
dd if=/home/username/master-boot-record.txt of=/dev/sda count=1 bs=512
Backup Grub configurations
Those using UEFI Grub won’t be able to use the DD tool to back up the Grub bootloader. Thankfully, it’s still possible to create a complete backup of your configuration files, custom boot entries, etc.
Note: if backing up the Grub configuration files isn’t enough, consider reading our tutorial about how to create a backup in Clonezilla. It’ll create a complete system backup, including UEFI Grub, etc.
To create a complete Grub configuration file backup, start by creating a backup folder in your home directory using the mkdir command.
mkdir -p ~/grub-backup
With the backup folder made in your home directory, we can make a copy of the Grub configuration file.
cp /etc/default/grub ~/grub-backup/
This is the Grub configuration file. Next, copy your Grub bootloader entries from the /etc/grub.d/ folder.
sudo cp -R /etc/grub.d/ ~/grub-backup/
From here, plug in a USB flash drive into your Linux PC. After that, drag the “grub-backup” folder to the drive.
Restore the backup
To restore your Grub configurations and entries to your UEFI Linux PC, plug in your USB flash drive. Then, follow the step-by-step instructions below.
Step 1: Open up your Linux file manager, click on the USB flash drive and place the “grub-backup” folder in your home folder (~/).
Step 2: Launch a terminal window on your Linux PC. Then, use the CD command to move into “grub-backup.”
Step 3: Restore the “grub” file to /etc/default/ with the mv command.
sudo mv grub /etc/default/
Step 4: Restore your Grub bootloader entries to the /etc/default/grub.d/ folder.
cd ~/grub-backup/grub.d/ sudo mv * /etc/grub.d/
With the files restored, update your Grub system.
grub-mkconfig -o /boot/grub/grub.cfg
grub2-mkconfig -o /boot/grub2/grub.cfg