Backing up and restoring operating systems

UPDATE January 2023: split from Linux Essentials and heavily refurbished!

- Introduction -
- Backing up your OS -
- Restoring your backup -
- Making your backups bootable -
- Booting OSes with other kernels -
- Summary -

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):
  4. Copying a partition in GParted
  5. 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:
  6. Pasting a partition in GParted Pasting a partition in GParted
  7. Proceed with the operation:
  8. Applying an operation in GParted Confirming an operation in GParted
  9. Wait until it finishes, and close:
  10. Closing operations in GParted
  11. 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):
  12. Shrinking a partition in GParted Shrinking a partition in GParted
  13. 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.
  14. Copy the newly made partition as in step 3 (except a different identifier this time - sda6 in our screenshots).
  15. 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?):
  16. Switching a drive in GParted
  17. Paste the partition in unallocated space again (just on the other drive):
  18. Pasting a partition in GParted
  19. Apply and confirm as usual. Now attach a pendrive and refresh devices:
  20. Refreshing devices in GParted
  21. A new device will appear. Switch to it (this will be the one with the "furthest" letter in the alphabet):
  22. Switching a drive in GParted
  23. Copy and paste your OS again, this time onto the pendrive:
  24. Pasting a partition in GParted
  25. 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:
  26. Labeling a backup in GParted Labeling a backup in GParted
  27. Do that for all of the backups.
  28. Restoring your backup

  29. To restore a backup, we will redo the entire process but in reverse. So copy the backup:
  30. Copying a partition in GParted
  31. Switch your drive to whichever you want to restore the backup to. Paste to unallocated space:
  32. Pasting a partition in GParted
  33. Apply and confirm as usual. Here is our restored backup:
  34. Showing a restored backup in GParted
  35. 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:
  36. Enlarging a partition in GParted

    Making your backups bootable

  37. 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.
  38. Showing a terminal emulator with liloconfig running
  39. Now pick Begin:
  40. Beginning the liloconfig setup
  41. The next screen should not be needed, so just OK past it:
  42. A useless screen in liloconfig
  43. Just pick standard on the next screen:
  44. Choosing how lilo will look like at boot
  45. Pick MBR now:
  46. Picking MBR option to install the bootloader to
  47. 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:
  48. Liloconfig drive install choice screen, with sda input
  49. Pick 30 seconds here, so that you can actually manage to choose which OS you want to boot (main or backup):
  50. Liloconfig duration choice screen, with 30 seconds chosen
  51. 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):
  52. Liloconfig image choice screen
  53. Now add a bootable system:
  54. Adding a system in lilo
  55. First, add the main system (sda1 - which was the origin of all our backups and that you are presumably using regularly):
  56. Picking a system for the bootloader to recognize
  57. Input a name that will remind you of it and OK past the screen:
  58. Showing the system name input screen in liloconfig
  59. Add another linux system, this time the backup. Pick Linux again, and add the sda3 partition on which the backup is installed:
  60. Adding the restored backup as a bootable system in liloconfig
  61. Input the appropriate name:
  62. Showing the system name input screen in liloconfig
  63. Finally install the bootloader:
  64. Finally installing the bootloader Lilo finally installed
  65. 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:
  66. 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

  67. 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.

Back to the front page