Monday, April 06, 2009

Creation and Maintenance of a Gentoo Backup

    This guide will instruct you in create a tarball with a up to date Gentoo system which can be used to save much time when formatting and reinstalling.

First of all, I will call those tarballs as follows (from here on):

  1. stage4→A complete bootable system with kernel, grub but without Xorg, Desktop environment nor anything Xorg related.
  2. stage5→It will contain all from stage4 and it includes too Xorg, Desktop Environment and every application you use.
  3. stage6→It will contain all from stage5 including a full desktop environment (like gnome, cinnamon, etc)

Starting installing Gentoo

    First of all, you need to install gentoo from scratch following Gentoo's Handbooks until the "Configuring Boot Loader" but without reboot nor unmount nor exitting chroot at the end.

Updating Gentoo

    Now it's time to update the base system of gentoo by running: emerge --sync && emerge -uDN world
After update, you can proceed with installation of any non graphical application and config that will be saved: For example, emerge networkmanager and executing rc-update add NetworkManager default

Compress the stage4

    Now it's time to compress the stage4 image to be used in case we need to format and reinstall.
If your idea is to have stage5 instead of stage4, just skip this section, and continue to Install Xorg and Desktop Environment.

    Now type exit in order to exit from chroot, and mount the destination filesystem to store the tarball but keep in mind that it will need to be stored in a partition different than where you installed Gentoo!

    If you followed the gentoo's handbook, you will have Gentoo's root partition mounted at /mnt/gentoo, so change to that directory (cd /mnt/gentoo) and type the following command to save the backup:

tar -cvjpf /stage4.tar.bz2 *

 
    For example, if you want to save the stage4 tarball under /mnt/harddrive, the command will be:

tar -cvjpf /mnt/harddrive/stage4.tar.bz2 *

Now you can reboot your system, and start using Gentoo. For instructions to restore this backup when needed, see Restoring Backup section at the end of document.


Install Xorg and Desktop Environment

    If your idea is to create a full backup of a complete Gentoo System, you must proceed with this method.

    Just emerge everything you need and configure it inside this chroot (propietary display drivers and KDE, for example)


Compress the stage5

    Once finished installing all Xorg and Desktop Environment related applications, it's time to compress the new and full stage5 tarball to have always a DE prepared Gentoo tarballed to be used in a future.
In order to do this, you have to mount the destination partition (must be other than where you installed Gentoo!) and run:

tar -cvjpf /stage5.tar.bz2 *

    For example, if destination partition is mounted under /mnt/harddrive:


tar -cvjpf /mnt/harddrive/stage5.tar.bz2 *

Updating tarball

    In order to keep stages up to date, you can update them in a running environment without the need to format nor modify your current installation.

    In order to do this, you have to unpack your current stage tarball, chroot to it, update system, and repack. Step by step:

  1. Create /mnt/stage
  2. Extract stage to /mnt/stage by executing: tar -xvjpf /stage5.tar.bz2 -C /mnt/stage
  3. mount -t proc none /mnt/stage/proc
  4. mount -o bind /dev /mnt/stage/dev
  5. If you have any system folder in a separate partition, mount them (for example boot partition)
  6. chroot /mnt/stage /bin/bash
  7. env-update
  8. source /etc/profile
  9. After that, just run emerge --sync && emerge -uDN world
  10. Once updated, we need to delete previous stage tarball and recreate. Now exit from chroot and do umount /mnt/stage/dev && umount /mnt/stage/proc
  11. To repack stage tarball, go to Compress the stage* section


Restoring Gentoo from stored stage

    Note: The safest way to restore a Gentoo from image we created, is to boot from a liveCD, format destination partition and unpack image, though replacing running Gentoo should work despite it is not tested.
 
    In order to restore a saved stage when formatting is simple: just unpack it to where you want to install gentoo by doing tar -xvjpf origin/stage5.tar.bz2 -C destination and after chroot to it to update grub install (refer to gentoo's documentation for doing that)

    I hope this guide will save a lot of time to anyone other than me. It isn't something new really, I didn't invent anything, only I wanted to post this for helping others too in restoring a gentoo's installation when something fails.

No comments: