Chapter 18. Taking Backups

Table of Contents

Poor Man's Backup
System Settings
User Settings
Sample Script
More Managed Backups
Backup Ninja
Bare Metal Recovery
Stage4/5/... Installation


Before we finish this book, lets focus on backups and restores. After all, you don't want to put all that effort in your Linux system, only to see it vanish with the next power surge or daughter-spilled-milk-over-my-laptop incident.

Poor Man's Backup

Not taking any backups is living on the edge. Especially in case of Gentoo Linux, you don't want to lose all the effort you've put in setting up your system. A basic, poor man's backup, is nothing more but an off-line copy of your important files. This can be on a USB stick, an SD card or a CD/DVD. But what to backup?

The following sections describe a possible set of files and directories to back up, without resorting to lots and lots of storage requirements.

System Settings

First of all, take a copy of your /etc folder. The /etc directory contains all system settings, including your file system table (fstab), Portage-specific settings (/etc/portage), service configuration files (in /etc/conf.d and /etc/env.d), ... Really, this location is not to be forgotten.

Next, there are a few non-/etc files or directories that you want to copy as well.

  • /var/lib/portage/world is your world-file, which contains a list of all software you've installed (well, not their dependencies, but that's not needed anyhow). In case that you ever need to rebuild your system, this file will tell you what needs to be installed

  • /usr/local contains all non-Portage installed software and settings. Although you might not have anything inside that, those of you that do will most likely take a backup of that as well.

  • /proc/config.gz (or /usr/src/linux/.config) is your kernel configuration. You've probably lost a lot of sweat and oxygen while setting up your kernel configuration, so you definitely don't want to lose it

User Settings

Next to the system settings, you'll find that your end user(s) on your system also have gone through lots of trouble to set up their system as they want it. End users' configuration settings are stored in the end users' home directories. There is no single directory for all configuration settings, so finding which ones to back up and which not might be difficult. However, one way that works for me (and which usually doesn't include the end users' personal files) is to backup all ${HOME}/.[^.]* files and directories. Most configuration directories (or files) are hidden files (i.e. starting with a period).

Sample Script

The following command will make an archive of the above mentioned settings.

~# tar cvzf /var/tmp/poormanbackup.tar.gz /etc /var/lib/portage/world /usr/local /proc/config.gz /home/*/.[^.]*

Again, this is just a poor man's backup, because it is not really managed, but it is still better than nothing. Make sure you copy the poormanbackup.tar.gz to a safe(r) place. Note that it will still be fairly large, because of the end user copy (as it contains lots of software caches, like your browsers' cache). If you drop the end user directories (the /home/*/.[^.]*) the file is reduced from many dozens (up to hundreds) of megabytes down to about half a megabyte.

To restore files, get the file on your system (mount the USB stick/CD) and extract the file(s) you need. For instance, to extract all /etc/portage/* files:

~# tar xvzpf /media/usb/poormanbackup.tar.gz -C / etc/portage

Note the space between / and etc/portage!

More Managed Backups

A slightly more complicated, but probably safer method, is to use tools that provide you with a managed backup solution. Managed means that you tell them what to backup (although they do propose certain backup situations) and they take care of the rest, such as repetitive backups, as well as easy restore activities in case things go wrong.

If you take a look at the packages that Gentoo offers in its app-backup category, you'll find that there are plenty of them. I can't tell you which one is better, because that is a personal taste. But I will provide a few pointers so you can get started.

Backup Ninja

The backupninja application is a tool which is scheduled to run every night (for instance) and that will read in the configuration file(s) in /etc/backup.d. Inside this directory, you define what needs to be backed up, how often, etc. For instance, the same poor man's backup settings of above would result in a file similar to the following:

File /etc/backup.d/20-basic-system.rdiff to backup regular files:

when = daily
options = --exclude-special-files
nicelevel = 15

label = myhostname.mydomain
type = local
keep = 60
include = /etc
include = /var/lib/portage/world
include = /usr/local
include = /home/*/.[^.]*
exclude = /home/*/.mozilla/firefox/*.default/Cache

type = local
directory = /var/backups/backupninja

File /etc/backup.d/

zcat /proc/config.gz > /etc/kernel-config

File /etc/backup.d/

rsync -avug /var/backups/backupninja /media/usb

The result of this is that the kernel configuration file is generated first (into /etc/kernel-config) after which (incremental) backups are taken of the presented locations, and stored in /var/backups/backupninja. Finally, these files are copied (synchronized) to /media/usb (which might be a USB stick or external disk drive). If you ever want to store that backup outside of your home (for instance, at your parents/kids house), detach the USB stick and store it there. Just make sure another USB stick is attached and mounted for the next backup cycle.

Bare Metal Recovery

When you want to make sure your system is up and running in no time, even when the entire system crashed, you need to take bare metal backups (full system backups) which you can restore quickly. A popular method to accomplish this is to use imaging software tools.


If you installed Gentoo using the sysresccd CD, then you already have the tools at your disposal to perform imaging backups. A popular tool is PartImage, and is available on the sysresccd.

The tool offers a graphical (or curses) based interface, but you don't need to use that: the tool also supports command-line backups.

An example usage would be the following. Boot from the sysresccd, and attach USB storage to your system on which you want to back up your entire system:

~# mount /dev/sdc1 /media/usb

Next, take image(s) of your partitions, or take a full disk backup. Don't worry, PartImage is able to detect when disk space isn't used, and will skip those empty blocks. The following takes a full backup of /dev/sda in blocks of 700Mbyte (in case you ever want to store the files on CD or DVD):

~# partimage -b -z1 -o V700 save /dev/sda /media/usb/gentoo_full.sda.partimg.gz

If you ever need to restore the system, use the following command:

~# partimage -e restore /dev/sda /media/usb/gentoo_full.sda.partimg.gz.000

Stage4/5/... Installation

Specifically for Gentoo, some users have created enhancements on top of the standard "stage3" installation used by Gentoo Linux. With stage4 or stage5 installations, the extracted tarball is a lot larger (it contains almost an entire system) and might be accompanied with additional script(s) that set up or recover the system. These scripts can, for instance, restore your partition layout, reformat the partitions, reinstall the bootloader, etc.

Compared with the poor man's backup approach described above, this can be seen as an "entire system poor man's backup", with optionally

  • Backup & restore of the master boot record and partition table

    dd if=/dev/sda of=/media/usb/full-stage-backup/mbr.img bs=512 count=1
    sfdisk -d /dev/sda > /media/usb/full-stage-backup/sfdisk.dat
    dd if=/media/usb/full-stage-backup/mbr.img of=/dev/sda
    sfdisk /dev/sda < /media/usb/full-stage-backup/sfdisk.dat
  • Restoration of the boot loader

    (Restore: command after restoring /boot content)
    grub-install --root-directory=/mnt/gentoo/boot /dev/sda
  • Reformatting of partitions (be careful here, this is very error-prone and specific for your system)

    mount | grep -v 'dev/mapper' | grep -v 'dev/md' | grep 'type ext' | awk -F' ' '{print "mkfs."$5" "$1}' > /media/usb/full-stage-backup/

I personally prefer imaging software for such large recovery scenarios. However, such stage4/stage5 installations prove to be useful when you migrate your system from one disk to another. Using imaging tools might fail here, because they often require that the target hard disk is equally sized, whereas you probably want to migrate to a larger disk.

So, if you are interested in a stage4 or stage5 backup/restore, you can take one from a running system or booted from a livecd. I suggest using a livecd (or other live environment) as that will ensure that the restore of the archive will give you a sane situation. If you take such an archive while running inside the system itself, you will also copy state files and other files that should not be there when a system is being booted.

Mount your file systems (say at /mnt/gentoo) except for the dynamically created ones (like proc, dev, sys, ...). Then, run the following tar command to create the archive:

~# tar -cvzf /media/usb/backup/stage4.tar.gz --strip-components=2 /mnt/gentoo

By using --strip-components=2, all files are stored in the archive without the leading "/mnt/gentoo".

If you need to put back the backup, you can recreate your partitions (or create new ones), mount them somewhere (say /mnt/gentoo again) and run:

~# tar xvzpf /media/usb/backup/stage4.tar.gz -C /mnt/gentoo

Don't forget to reinstall the boot loader before you reboot!