This article introduces how to recover from a full backup of Linux system taken with rsync or tar archive.
First, here we have a helper script written in Python: this script just generates the final command that can be used for making backup. Note this script is not what automates every thing of whole backup workflow. This is currently compatible with making backup on /
directory with either rsync
or tar
command.
./main.py
Raw image | a loop device as | Mounted as |
---|---|---|
manjaro.img |
/dev/loop0 |
/mnt/foo |
Get root privileges:
su -
Download the latest disk image from SSH:
mkdir /mnt/imglatest
rsync -rvn foo@bar:/path/to/src/manjaro.img.gz /mnt/imglatest
Set up loop device from a disk image:
cd /mnt/imglatest
gzip manjaro.img.gz
losetup /dev/loop0 ./manjaro.img
Mount loop device into a specified directory:
mkdir /mnt/foo
mount -t btrfs /dev/loop0 /mnt/foo
Target | The directory to be chroot ed |
---|---|
/mnt/var |
/ |
!!! note Before you execute all the commands below
* You need to make EFI System Partition if you're trying UEFI boot! (https://wiki.archlinux.org/title/EFI_system_partition)
* You need to assure the backups to have a valid root directory structure excluding critical directories (e.g. sys
, proc
, dev
, and etc.)
We are introducing how to recover the system from a full-backup taken with TAR archive.
tar -xvf /backup.tar.gz
Continues to "next step" ...
Before you start recovery, you are responsive for establishing the chroot
ed shell in the newly migrated filesystem in a live CD or live DVD environment, so in this part we gonna explain how to deal with them. By the way all the command in here are intended to be executed as a root user. As always, be careful!
sudo su -
You need at least two partitions to boot UEFI systems:
- ESP Partition (e.g.
/boot/efi
or just/boot
directory)- You must add
EFI System
flag on this partition, otherwise the system never boots
- You must add
- The partition for the
/
directory
I prefer to using fdisk
command when it comes to making partitions, but you can use other way (e.g. parted
):
# Add and edit partitions
fdisk /dev/sdx
Formatting a filesystem by each partition is also necessary, after you split the partitions:
# Format the partition for `/boot` :
mkfs.fat -F 32 /dev/sdxY
# Format the partition for `/` :
mkfs.btrfs /dev/sdxY
!!!note Example If you split the partitions in the aforementioned way, the complete detail can be like this:
# fdisk -l /dev/sda
Disk /dev/sda: 127 GiB, 136365211648 bytes, 266338304 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: C6724339-932F-AF48-8C88-55B50E884E5A
Device Start End Sectors Size Type
/dev/sda1 2048 1128447 1126400 550M EFI System
/dev/sda2 1128448 266336255 265207808 126.5G Linux filesystem
Now all you have to do is mount two of them by typing this command.
# Make directory for two partitions to be mounted
mkdir /mnt/foo; mkdir /mnt/foo/boot
# Mount directory from the block device (sdXn should be replaced)
mount /dev/sdX1 /mnt/foo/boot; mount /dev/sdX2 /mnt/foo
You need to bind other directories into the directory to be chroot
ed as well as ESP partition and /
. Normally these folder is unpopulated but when the operating system starts, then they gets populated.
# Bind other directories into '/'
for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt/foo$i; done
You need to bind EFI Variable to the directory /mnt/foo/sys/firmware/efi/efivars
so that system can read these values when it generates GRUB menu.
# Bind EFI Variables
mount -t efivarfs efivarfs /mnt/foo/sys/firmware/efi/efivars
!!!warning Preparing EFI Variable for newly installed Linux
Check patterns below is what can be used to check if EFI Variable is available on your system. You need to try modprobe efivarfs
by enabling the kernel module required, in case none of them work.
* ls /mnt/foo/sys/firmware/efi/efivars
* df -la | grep efivars
* efibootmgr
After all, you will change root by this.
chroot /mnt/foo /bin/bash
not working: just displays grub selection menu
grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB
# Reinstall GRUB
paru -Syu grub
grub-mkconfig -o /boot/grub/grub.cfg
Just to make sure you should check what is subject to be unmounted, before you unmount and modify the command below (if you need)
# Make sure to check what is currently mounted
df -a | grep /mnt/foo*
# if everything's fine ...
for i in /boot /dev/pts /dev /proc /sys/firmware/efi/efivars /sys /run /; do sudo umount /mnt/foo$i; done