Backing up and restoring operating systems
Backing up and restoring operating systems
Introduction
Have you ever lost your OS install due to breaking it and being unable to fix?
Have you ever lost your OS install due to drive failure?
Have you ever lost your OS install due to a theft or a fire?
Have you ever wanted to copy your OS install to another computer without
reinstalling it and redoing all the modifications to the base distro?
Well, hopefully you haven't suffered from the first three. But if you have, you
know the value of backups. And if you haven't, well, let me tell you that you
don't want those things to happen. As for the fourth - if you are anything like
me, you probably have your favorite, carefully prepared system configuration
that you don't stray away from (except to check stuff out sometimes, maybe).
Wouldn't it be great if you could just "freeze" and "teleport" your OS whenever
and wherever you want to? Protecting you both from those listed hazards and
saving time / effort redoing modifications in case you want to deploy your OS
elsewhere. This article will teach you those magic spells. What we will do here
is make a snapshot of your current running OS and store it on other partitions
and / or drives - and then learn how to bring it back. The main reason for doing
this is to immunize you against OS breaks; you can always play around with your
current install and just restore the backup in case of a break. Anyway let's
go:
Backing up your OS
1. Log in to another Linux installation (on the same computer). This might be an
OS that's already installed on another partition / drive, or even a live USB
disk.
2. Launch GParted
3. Right click and copy the partition that has the system you want to backup (it
is easier to do this entire process if your /home is on the same partition -
in that case, all your settings will be backed up, as well. But not strictly
essential):
Copying a partition in GParted4. Find an amount of unallocated space that is higher than the size
of your copied partition (if you don't have, you are going to need to shrink and
/ or delete partitions), right click and paste: Pasting a partition in
GPartedPasting a partition
in GParted5. Proceed with
the operation: Applying an operation in
GPartedConfirming an
operation in GParted6.
Wait until it finishes, and close: Closing operations in
GParted7. Now, shrink that
copied partition (not the actual one!) to the minimum possible space.
Though, you will need a little more than what GParted says is the minimum - so
if you have a 7GB system, try 9GB just to be safe (if there's too little, it
will result in the New size smaller than the minimum error): Shrinking a
partition in GPartedShrinking a partition in
GParted8. Apply and
confirm the operation as in step 5. What the shrinking accomplishes is that your
OS backup will fit into less space later (e.g a smaller pendrive). If you want
to use the dd command to restore it later, it will also take much less time,
then.
9. Copy the newly made partition as in step 3 (except a different identifier
this time - sda6 in our screenshots).
10. We will make yet another backup - this time on another drive. This way - if
your first drive dies and takes with it your backup - you still have
another. Switch your drive (you do have more than one, right?):
Switching a drive in GParted11. Paste the partition in unallocated space again (just on the
other drive): Pasting a partition in
GParted12. Apply and
confirm as usual. Now attach a pendrive and refresh devices: Refreshing
devices in GParted13. A
new device will appear. Switch to it (this will be the one with the "furthest"
letter in the alphabet): Switching a drive in
GParted14. Copy and paste
your OS again, this time onto the pendrive: Pasting a partition in
GParted15. Apply and
confirm as usual. Now, you can stick that pendrive backup onto a relative or a
friend in case of a fire or theft in your house. Remember to label your backups,
so that it is easier to recognize them: Labeling a backup in
GPartedLabeling a backup
in GParted16. Do that for all
of the backups.
Restoring your backup
17. To restore a backup, we will redo the entire process but in reverse. So copy
the backup:
Copying a partition in GParted18. Switch your drive to whichever you want to restore the
backup to. Paste to unallocated space: Pasting a partition in
GParted19. Apply
and confirm as usual. Here is our restored backup: Showing a restored backup
in GParted20. We're going
to need to enlarge it now (since presumably, you will still install stuff on it
after that). Click Resize/Move and input your desired size: Enlarging a
partition in GParted
Making your backups bootable
UPDATE February 2025: the Lilo included by default in Slackware 15 might
not work for this, and you might need to downgrade (or rather upgrade in this
case) to the one from 14.2 for example. A few weeks ago I tried to setup a few
bootable OSes in my drive(s), but whatever I did with the configs, drive
positions, etc. was failing - Lilo could still only boot from /dev/sda1
regardless. I thought it might have been because of some modifications I've done
to my usual OS, so I tried installing Lilo from a pure Slackware 15, which did
nothing either. At the final sign of desperation I thought to try the last
remaining option, which was to use another version of Lilo - even though the
Internet had nothing about a "bug" in the new version. And voila! It fixed the
problem. I never noticed it earlier because I was almost always booting
/dev/sda1; and even when I was trying another option and it failed, I just
assumed I had setup Lilo incorrectly and didn't think too much of it. But no;
there is probably some combination of old hardware (like my junkyard one) that
activates this "bug" in Lilo. Keep this in mind before you read the following
part of the tutorial.
21. To boot your backups, you're going to have to inform your bootloader about
them. Run sudo liloconfig and pick the expert mode. Note: this entire
process will assume you're using a live system that runs the same kernel as
your restored backups; if it's not the case, the following instructions are
still valid, but will need a tiny
modification
after you're done.
Showing a terminal emulator with liloconfig
running22. Now pick Begin:
Beginning the liloconfig setup23. The next screen should not be needed, so just OK past it: A
useless screen in liloconfig24. Just pick standard on the next screen: Choosing how lilo will
look like at boot25. Pick
MBR now: Picking MBR option to install the bootloader
to26. Going to assume
you've picked the first drive, but you can change this to /dev/sdb or any
other drive you want to install the bootloader to: Liloconfig drive install
choice screen, with sda input27. Pick 30 seconds here, so that you can actually manage to choose
which OS you want to boot (main or backup): Liloconfig duration choice screen,
with 30 seconds chosen28.
If using Salix, you will get to choose if you want its logo to display on boot.
Just deny it (or allow if you like, whatever): Liloconfig image choice
screen29. Now add a
bootable system: Adding a system in
lilo30. First,
add the main system (sda1 - which was the origin of all our backups and that
you are presumably using regularly): Picking a system for the bootloader to
recognize31.
Input a name that will remind you of it and OK past the screen: Showing the
system name input screen in
liloconfig32.
Add another linux system, this time the backup. Pick Linux again, and add the
sda3 partition on which the backup is installed: Adding the restored
backup as a bootable system in
liloconfig33. Input the
appropriate name: Showing the system name input screen in
liloconfig34.
Finally install the bootloader: Finally installing the
bootloaderLilo
finally installed35. Now
you will be able to choose between booting your main system and your restored
backup if you choose the first drive at the BIOS level (you could have
theoretically put as many OSes in there as you wanted to, even ones installed on
other drives; we're going to keep it simple for now, though). Before the backup
is operational - we're going to have to point its fstab partitions towards the
correct identifiers. Type sudo mount /dev/sda3 /mnt/tmp in a terminal. Then sudo
leafpad /mnt/tmp/etc/fstab: Showing the fstab file from the restored
backup
The fstab file decides from which partition the system is actually booted (and
every other partition that will be mounted at launch). See how it's still trying
to boot from sda1, even though the backup is on sda3? That's because
the original fstab had sda1 in it - as it should have. But now we're trying to
run the restored backup, so this is counterproductive. Just change it to sda3
and save.
Booting OSes with other kernels
Adding a Linux distro in liloconfig's interactive menu will insert an entry
pointing the bootloader to the same kernel that the OS you ran liloconfig from
has. For example, the lilo config file might have a section like this:
# Linux bootable partition config begins
image = /boot/vmlinuz-generic
initrd = /boot/initrd.gz
root = /dev/sdb1
label = Slack14
read-only # Partitions should be mounted read-only for checking
# Linux bootable partition config ends
In Slackware, vmlinuz-generic is actually just a symlink to whatever the
kernel version it's using, eg. vmlinuz-generic-5.15.63 in 15. If the OS you
ran liloconfig from has that, then lilo will "expect" all the OSes defined in
the config file to be using it too. Yet, imagine that you wanted lilo to boot an
older or newer Slackware version (like the 14 in the above example), or even a
completely different distro. The bootloader will expect the booted OS to be
using the kernel specified in the config file, but it doesn't, so it will fail
to load. To fix this problem, you will need to copy the kernel from the other
distro to your current /boot folder, and point lilo to it. First, mount the
partition that contains the "other" distro you want to boot (eg sudo mount
/dev/sdb1 /mnt/tmp), and copy its kernel to /boot (where liloconfig loads
them from by default), eg sudo cp /mnt/tmp/boot/vmlinuz-generic-4.16.9 /boot;
remember to pick the one with the numbers at the end. Now modify the lilo config
file entry as such:
# Linux bootable partition config begins
image = /boot/vmlinuz-generic-4.16.9
initrd = /boot/initrd.gz
root = /dev/sdb1
label = Slack14
read-only # Partitions should be mounted read-only for checking
# Linux bootable partition config ends
What was just vmlinuz-generic became vmlinuz-generic + the kernel
version your "different" OS is using. Save and re-run lilo so that it "absorbs"
the updated config file (just sudo lilo is enough). There is no problem for lilo
be able to load many different Slackware version installs, or even different
distros, though I have no experience with the latter. Some internet posters are
saying that certain distros also have their own unique initrd files; if so,
those will have to be copied to /boot and inserted into the config file, as
well.
Summary
36. That's it! We've restored a backup. You can reboot in peace now. Even though
there seem to be a lot of steps, this is actually easier than it seems if
you become familiar with it. I use it every time I break shit and it's
surely a lot easier than reinstalling a distro and redoing your changes
(ugh). TBD: dd command backups.