Snapshot backup of linux system

The task at hand is provisioning a FC6 server, taking a snapshot backup, then restoring it.

The idea is to create a FC6 instance from CD install, just use all defaults, next, next, next, and you’ll and up with a partitioning scheme that looks like this:

/dev/sda1 /boot boot partition, ext3
/dev/sda2 VolGroup00
              LogVol00 / root partition, ext3
              LogVol01   swap partition

When you have it ready to capture, you shutdown, and boot with linux rescue. You’ll need another machine to provide NFS disk mounts. The idea is capture the backup to another server, over nfs, then restore it later.

Assuming the backup is created, and you are about to restore, with the nfs dir already mounted at /configs/

#!/bin/sh -x
# This script will create the following partition scheme:
# /dev/sda1 /boot boot partition, ext3
# /dev/sda2 VolGroup00
#               LogVol00 / root partition, ext3
#               LogVol01   swap partition
# It creates the partitions, then mounts the following:
#                   /dev/sda1 /tmp/sda1
#    /dev/VolGroup00/LogVol00 /tmp/sda2
# It then untars the backup files for sda1 and sda2.
# Note about the backup files:
#    The backup files were captured after installing FC6 on from CD.
#    We use “linux text” to install, take all defaults.  Imagine we captured from a
#    rescue CD boot, with the following:
#         mkdir /tmp/tmp
#         mount <nfs_server_ip>:/tmp/tmp /tmp/tmp
#         mkdir /tmp/sda1
#         mount -o ro /dev/sda1 /tmp/sda1
#         cd /tmp/sda1
#         tar -czf /tmp/tmp/dk_fc6_ch3_sda1.tar.gz .
#         mkdir /tmp/sda2
#         vgscan
#         vgchange -ay VolGroup00
#         mount -o ro /dev/VolGroup00/LogVol00 /tmp/sda2
#         cd /tmp/sda2
#         tar -czf /tmp/tmp/dk_fc6_ch3_sda2.tar.gz .
#         sfdisk -d /dev/sda > /tmp/tmp/

# remove existing partitions:
lvremove -f /dev/VolGroup00/LogVol00
lvremove -f /dev/VolGroup00/LogVol01
vgremove VolGroup00
pvremove /dev/sda2
# This wipes the partition table:
dd if=/dev/zero of=/dev/sda count=1 bs=512
# if you want to really wipe everything, uncommend the following:
# dd if=/dev/zero of=/dev/sda bs=512
echo “Put the partitioning schema back on the sda drive”
/sbin/sfdisk /dev/sda < /configs/

# NOTE: no line for mbr, we don’t need it, we’ll install the mbr with grub-install near the end of this script!

pvcreate /dev/sda2
vgcreate VolGroup00 /dev/sda2
lvcreate –size 2G –name LogVol01 VolGroup00
echo “Make LogVol00 the remainder of the /dev/sda2 disk”
EXTENTS=`vgdisplay VolGroup00 | egrep “Free *PE.*Size” | sed -e ‘s@.*Size *@@’ -e ‘s@ .*@@’`
lvcreate –extents $EXTENTS –name LogVol00 VolGroup00

vgchange -ay VolGroup00

mkfs.ext3 /dev/VolGroup00/LogVol00
mkswap /dev/VolGroup00/LogVol01
mkfs.ext3 /dev/sda1
mkdir /tmp/sda1
mkdir /tmp/sda2

mount /dev/sda1 /tmp/sda1
mount /dev/VolGroup00/LogVol00 /tmp/sda2

cd /tmp/sda1 && tar -xzf /configs/dk_fc6_ch3_sda1.tar.gz
cd /tmp/sda2 && tar -xzf /configs/dk_fc6_ch3_sda2.tar.gz

cd /tmp
umount /tmp/sda1
umount /tmp/sda2
e2label /dev/sda1 /boot

# remount, this time with boot/ under root / partition:
mount /dev/VolGroup00/LogVol00 /tmp/sda2
mount /dev/sda1 /tmp/sda2/boot

echo “Disable Root Login”
sed -ie ‘s/^#*PermitRootLogin.*/PermitRootLogin no/’ /tmp/sda2/etc/ssh/sshd_config

echo “Install MBR with grub-install:”
grub-install –root-directory=/tmp/sda2 /dev/sda

cd /

umount /tmp/sda2/boot
umount /tmp/sda2

echo “Finished”

Any questions? I realize there are probably some holes in the logic here, but his script describes how to restore a FC6 system that was backed up while booted in a cold state.


Comments are closed.