Backing up and restoring operating systems
UPDATE January 2023: split from Linux Essentials and heavily refurbished!
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
- Log in to another Linux installation (on the same computer). This can be done easily with a live USB drive, and is the way I'll assume for this tutorial. Make sure that live USB has the same kernel as your running OS - this will make it a lot easier to do stuff later.
- Launch GParted
- 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):
- 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:
- Proceed with the operation:
- Wait until it finishes, and close:
- 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):
- 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.
- Copy the newly made partition as in step 3 (except a different identifier this time - sda6 in our screenshots).
- 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?):
- Paste the partition in unallocated space again (just on the other drive):
- Apply and confirm as usual. Now attach a pendrive and refresh devices:
- A new device will appear. Switch to it (this will be the one with the "furthest" letter in the alphabet):
- Copy and paste your OS again, this time onto the pendrive:
- 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:
- Do that for all of the backups.
Restoring your backup
- To restore a backup, we will redo the entire process but in reverse. So copy the backup:
- Switch your drive to whichever you want to restore the backup to. Paste to unallocated space:
- Apply and confirm as usual. Here is our restored backup:
- 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:
Making your backups bootable
- 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; this is still doable otherwise but a little complicated and not covered here ATM):
- Now pick
Begin
:
- The next screen should not be needed, so just
OK
past it:
- Just pick
standard
on the next screen:
- Pick
MBR
now:
- 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:
- Pick
30 seconds
here, so that you can actually manage to choose which OS you want to boot (main or backup):
- 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):
- Now add a bootable system:
- First, add the main system (sda1 - which was the origin of all our backups and that you are presumably using regularly):
- Input a name that will remind you of it and
OK
past the screen:
- Add another linux system, this time the backup. Pick
Linux
again, and add the sda3 partition on which the backup is installed:
- Input the appropriate name:
- Finally install the bootloader:
- 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
:
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.
Summary
- 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 get 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, backups of OSes with other kernels.
Back to the front page