Recovery from a TAR backup

Introduction

This is my personal backup and recovery process that I've used many times over the years. There are more sophisticated backup and recovery tools around but I prefer a slightly more manual process, one which gives me an understanding of what is involved in taking a backup and restoring from the backup. It's also handy when you decide to change your filesystem type, take a backup and when restoring chose your filesystem type as part of the recovery process.

I run Gentoo Linux so what I show you will have to be carried over to whatever flavour of Linux you've chosen. It should work regardless of what Linux you're running.

The Backup

I have a simple script which mounts the root partition to /mnt/root and takes a TAR backup. In this case, root is mounted on /dev/sda1 so it's a simple task of remounting it so that it can be backed up without the interference of /dev and /proc. Be aware that if you have more than one filesystem making up your installation, you'll have to adjust the script accordingly. Oh, and make sure the target directory has plenty of space *and* that it's excluded from the backup.

The recovery

WARNING: This will trash your installation. You are recovering your OS after all so whatever is on disk will be overwritten.

Firstly, install SystemRescueCd on to a USB stick and boot off it into either 32 bit or 64 bit OS. Failure to boot into the correct flavour will mean that later down the track, your chroot will fail.

Create your partitions. Use gdisk (or fdisk) to create whatever disk partition you need for your new installation. In this recovery process, I will need the following

/dev/sda1 /
/dev/sda2 swap

then:

# mkfs.xfs /dev/sda1
# mkswap /dev/sda2

If you need to recreate a software raid array, make sure the partitions you have created are suitable to the type of RAID array you will be building.

# mdadm --create --verbose /dev/md1 --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1

Mount the new filesystem and unpack the backup that you have somehow made available from another system

# mkdir /new
# mount /dev/sda1 /new
# mkdir -p /new/tmp
# cd /new/tmp
# wget http://anothermachine/backup.tbz
# cd /new
# tar -xvjp --numeric-owner -f tmp/backup.tbz

In my case, the backup is on an encrypted drive, sdb2, so this bit of documentation is for my benefit as well as yours

# cd /new
# cryptsetup -y create secret /dev/sdb2
# mkdir /mnt/secret
# mount /dev/mapper/secret /mnt/secret
# tar -xvz --numeric-owner -f /mnt/secret/xxxx.tgz
# umount /mnt/secret
# cryptsetup remove secret

If you're wanting to use SSH to connect, disable the firewall and find out the IP address

# /sbin/iptables -F
# /sbin/iptables -X
# /sbin/iptables -P INPUT ACCEPT
# passwd root
# ifconfig

Now it's time to chroot into the unpacked archive and install the boot loader. I am a bit old school and still prefer to use LILO. Also, allow IP traffic in if you're using ssh to connect

# cd /
# mount --rbind /dev /new/dev
# mount --rbind /sys /new/sys
# mount -t proc proc /new/proc
# chroot /new /bin/bash
$ env-update
$ source /etc/profile
$ lilo

Check the contents of the following files for correctness. I run Gentoo Linux so the location of your files may differ.

/etc/conf.d/hostname
/etc/conf.d/net
/etc/fstab
/etc/udev/rules.d/*

If ghosting a new machine, also

# rm /etc/ssh/*key*

Clean up and reboot (remembering to take out the bootable USB when rebooting).

$ exit
# cd /
# umount /new/proc /new/dev /new
(for each raid partition)
# mdadm --stop /dev/md1
# reboot

That's it. Your system is now restored

Fudge
12 June 2013