Restoring a Linux installation
Table of Contents
We want to restore a Linux installation, that is, its
- root
/
and - home
/home/$USER
directory
onto another computer.
Premises
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
, because it is
- stable,
- fast
- versatile, and
- free of dependencies, that is, it is a single executable.
Saving the Installation
First, while not strictly necessary, copying the rsync
executable, say /usr/bin/rsync
into the $BKP_FOLDER
by
cp /usr/bin/rsync $BKP_FOLDER
avoids depending on whether rsync
has been installed on the rescue system or not.
To save the partitions:
- Use
lsblk
to identify the backup partition: its device letter, sayb
, and its partition number, say1
; for example/dev/sdb1
. -
Mount the partition
$PARTITION
to a folder$FOLDER,
say/mnt
, bymount $PARTIION $FOLDER
, saymount /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"
Restoring the installation
To restore the installation,
- reinstall Linux minimally,
- copy the partitions, and
- finally restore the bootloader:
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. -
Note the 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/sdb2
, and - the root and home partitions are
/dev/sda2
and/dev/sda3
.
To first mount and then restore the files:
mkdir -p /mnt/bkp && mount /dev/sdb2 /mnt/bkp
(
cd /mnt/bkp/
mkdir -p /mnt/root && mount /dev/sda2 /mnt/root
rsync \
--info=stats1,progress2 --human-readable --compress \
--archive --executability --hard-links --modify-window=1 --acls --xattrs \
--update --delete \
--exclude='/home' \
--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' \
./ /mnt/root/
mkdir -p /mnt/home && mount /dev/sda3 /mnt/home
rsync \
--info=stats1,progress2 --human-readable --compress \
--archive --executability --hard-links --modify-window=1 --acls --xattrs \
--update --delete \
./home/ /mnt/home/
)
Restoring the bootloader
We assume (as above) that the root partition is /dev/sda1
.
To restore the bootloader:
mkdir -p /mnt/root && mount /dev/sda2 -t btrfs /mnt;
mount /dev/sda2 -o subvol=/@/boot/grub2/x86_64-efi /mnt/boot/grub2/x86_64-efi;
mount /dev/sda2 -o subvol=/@/boot/grub2/i386-pc /mnt/boot/grub2/i386-pc;
mount /dev/sda2 -o subvol=/@/var /mnt/var;
mount /dev/sda2 -o subvol=/@/usr/local /mnt/usr/local;
mount /dev/sda2 -o subvol=/@/tmp /mnt/tmp;
mount /dev/sda2 -o subvol=/@/srv /mnt/srv;
mount /dev/sda2 -o subvol=/@/root /mnt/root;
mount /dev/sda2 -o subvol=/@/opt /mnt/opt;
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