Aim

Restore a Linux installation,

  • the root / and
  • the home /home/$USER directory

onto another hardware.

We assume that the installed Linux system (in our case, it happened to be OpenSUSE 42.2) uses

  • the bootloader grub2

and has separate partitions for

  • the root / and
  • the home /home folders

As a backup tool, we will use rsync. Its advantages:

  • stable,
  • fast
  • versatile, and
  • free of dependencies, that is, it is a single executable.

Saving the installation

Saving the partitions

  1. Use lsblk to identify the backup partition: its device letter, say b, and its partition number, say 1; for example /dev/sdb1.
  2. Mount your partition $PARTITION to a folder $FOLDER, say /mnt, by mount $PARTIION $FOLDER, say mount /dev/sdb1 /mnt.
MOUNT=/mnt
BKP_LABEL=USB_BKP

BKP_MOUNT="$MOUNT"/"$BKP_LABEL"
mkdir --parents "$BKP_MOUNT"
mount /dev/sdb1 "$BKP_MOUNT"

Copy

  • the home partition, skipping trash and cache files, and
  • the root partition, skipping the home folder (which is on a separate partition) and folders that only contain
    • boot images and kernel modules,
    • backups
    • removable media, and
    • temporary and cache files:
BKP_FOLDER="$BKP_MOUNT"

HOME_FOLDER="/home/$USER"
HOME_BACKUP_FOLDER="$BKP_FOLDER/home"
mkdir --parents "$HOME_BACKUP_FOLDER"

rsync -avxEHA --delete --human-readable --info=progress2 \
  --exclude="/.local/share/Trash" \
  --exclude="/.cache" \
  "$HOME_FOLDER/" "$HOME_BACKUP_FOLDER/"

ROOT_FOLDER="/"
ROOT_BACKUP_FOLDER="$BKP_FOLDER/root"
mkdir --parents "$BKP_FOLDER/root"

rsync -avxEHA --delete --human-readable --info=progress2 \
  --exclude=/home \
  --exclude='/etc/fstab' \
  --exclude='/boot/' \
  --exclude='/lib/' \
  --exclude='/lib64/' \
  --exclude='/.snapshots' \
  --exclude='/media' \
  --exclude='/mnt' \
  --exclude='/run' \
  --exclude='/dev' \
  --exclude='/proc' \
  --exclude='/sys' \
  --exclude='/tmp' \
  --exclude='/var/run' \
  --exclude='/var/lock' \
  --exclude='/var/tmp' \
  "$ROOT_FOLDER" "$ROOT_BACKUP_FOLDER/"

umount --verbose "$BKP_MOUNT"
rmdir --verbose "$BKP_MOUNT"

Store the backup program

This is not strictly necessary, but avoids depending on rsync been installed on the rescue system or not: Copy the fsarchiver executable, say /usr/bin/rsync into the $BKP_FOLDER by

cp /usr/bin/rsync $BKP_FOLDER

Restoring the installation

Reinstalling Linux

To

  • partition the hard drive, and
  • install the bootloader (Grub2 in our case),

install the Linux distribution in its most basic configuration (that is, installing as few packages as possible).

Once Linux is installed, boot into a rescue system, such as that on the installation medium (DVD or USB drive) of the Linux distribution. Log into a terminal. Use ls -l /dev/disk/by-uuid to detect which device letter (say /dev/sda) is assigned to which hard disk and which number to which partition. We note letter and number of the

  • backup partition, as well as
  • the root and home partitions.

(For example, /dev/sda might be the hard disk, \dev\sda2 the root partition and \dev\sda3 the home partition).

Restoring the partitions

We assume that

  • the backup partition is /dev/sdb1, and
  • the root and home partitions are /dev/sda1 and /dev/sda2.

We mount and then restore the files:

mkdir -p /mnt/bkp
mkdir -p /mnt/root
mkdir -p /mnt/home

mount /dev/sdb1 /mnt/bkp
mount /dev/sda1 /mnt/root
mount /dev/sda2 /mnt/home

cd /mnt/bkp/
rsync -avxEHA --delete --human-readable --info=progress2 \
root/ /mnt/root/;
rsync -avxEHA --delete --human-readable --info=progress2 \
home/ /mnt/home/

Restoring the bootloader

We assume (as above) that the root partition is /dev/sda1.

Then

mkdir -p /mnt/root
mount /dev/sda1 /mnt/root
mkdir -p /mnt/root/dev
mount --bind /dev /mnt/root/dev

chroot /mnt/root
  mkdir -p /mnt/root/proc
  mount -t proc proc /proc
  mkdir -p /mnt/root/sys
  mount -t sysfs sysfs /sys

  grub2-mkconfig -o /boot/grub2/grub.cfg
  grub2-install /dev/sda
exit