Creating
a Bootable Backup of a Linux Operating System
Newton Hammet
I have an AMD Athlon-XP PC running Linux Red Hat 8.0 that I built
for personal use and software development projects. For this system,
I wanted an archival and recovery process that ensured a quick recovery
from a hard drive crash or other loss of data, so I built my PC
with two inClose cold-swappable docking bays. Each docking bay holds
a removable docking tray containing one 60-GB IBM ATA hard drive.
The result is a full tower PC that uses the hard drive in the bottom
docking bay as my current operating system, and the hard drive in
the top docking bay as my backup medium.
To perform the backup, I wrote a script called mkbkup.pl, which
is available for download from the Sys Admin Web site. Its
function is to create a bootable backup of the operating system
and all mounted filesystems from the current OS onto the backup
medium. It was developed on a Linux Red Hat 8.0 platform, using
Perl (v5.8.0), grub (Gnu Grub v0.92), and the Linux kernel (/boot/vmlinuz-2.4.18-14).
Mkbkup.pl uses the command-line interface.
Mkbkup.pl has various options that provide flexibility in the
kinds of backups you can create. For example, you can create an
exact backup of your operating system that preserves partition size
and filesystem structure, or you can create a backup that has a
different number of partitions with a new filesystem structure.
Two examples follow:
1. Creation of a bootable backup with partition and filesystem
structure identical to the current operating system is accomplished
by the command:
mkbkup.pl <drive-address>
where <drive-address> is any valid hard drive device
address, other than the device address of the current operating system
(e.g., /dev/hdb, if the current operating system is located at /dev/hda).
2. Bootable backups can be created with different partition sizes
and/or different filesystem structures. For example, a system with
the following cramped filesystem structure:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hde2 5178460 4565608 612852 88% /
/dev/hde3 4142768 2081304 2061464 50% /home
could be backed up with the mkbkup.pl command line:
mkbkup.pl --newpart swap=1G,/=1G,/home=8G,/tmp=2G,/usr=8G,/var=2G /dev/hdg
to create the following, more roomy, filesystem structure:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hde2 1035692 358420 624660 37% /
/dev/hde3 8262036 2081304 5761036 27% /home
/dev/hde5 2071416 34488 1931704 2% /tmp
/dev/hde6 8262036 4006796 3835544 52% /usr
/dev/hde7 2071384 165904 1800256 9% /var
Bootable backups can also be created on hard drives with different
geometries and/or capacities than the hard drive that contains the
current system.
Recovering the data and operating system on the backup drive is
simplified by the use of the removable docking trays. I typically
boot up from the bottom docking bay, which holds my current system,
and back up to the drive in the top docking bay. To use the backup
drive, I simply remove the current drive from the bottom bay and
move the backup drive into the bottom bay; the backup drive becomes
the current drive, and 0I am ready to boot up.
There may be situations where you don't want to be limited to
one particular docking bay as your designated boot bay. You may
want to be able to boot from any docking bay or drive location in
your system. To allow for this flexibility, mkbkup.pl creates the
bootable backup using the labeled filesystems feature of Linux.
Linux allows filesystems to be created with labels and allows
the mounting of those filesystems to be done by specifying the filesystem's
label instead of specifying the filesystem's device address. For
example:
/sbin/mkfs -t ext3 -L root1a9WXQt5V /dev/hdb2
The above command creates a filesystem labeled "root1a9WXQt5V" on
/dev/hdb2 (the second partition of the drive located at /dev/hdb).
If the drive at /dev/hdb were moved to another device location
(e.g., to /dev/hdd, then the following command could be used to
mount that filesystem without prior knowledge that the filesystem
had changed its device address:
mount LABEL=root1a9WXQt5V /mnt/root
The command will find the filesystem labeled "root1a9WXQt5V", which
happens to be at /dev/hdd2, and mount it on /mnt/root.
By default, mkbkup.pl creates a label for each filesystem that
will be created on the backup medium. Using the -L option
of the mkfs command, mkbkup.pl will create labeled filesystems
on the backup medium, labeling each filesystem with the label created
for it. A disk partition containing a labeled filesystem is called
a labeled partition.
Since the operating system will use particular labels to find
particular filesystems, it is imperative that these labels be unique
with respect to all labeled partitions seen by the operating system.
A special algorithm uses the partition number and mount-point path
name of the filesystem to be created, the number of seconds since
January 1, 1970, and the process ID of the current invocation of
mkbkup.pl to produce these filesystem labels. This virtually assures
unique labels.
Although there is no comparable labeling process for swap partitions,
a simple script, swapfind, has been included with the download.
Swapfind runs when the operating system is booting and looks for
a swap partition that is on the same drive as the OS. If it finds
a swap partition on that drive, it activates it.
If you are using a swap partition and want the bootable backup
drive to be able to boot from any location, then do the following:
1. Install swapfind (included in the download) by the following
method:
cp swapfind /sbin/swapfind
2. Make swapfind executable:
chmod +x /sbin/swapfind
3. Then modify the /etc/rc.d/rc.local in the following manner. Add
the following line to /etc/rc.d/rc.local:
[ -x /sbin/swapfind ] && /sbin/swapfind boot-only
ensuring that it occurs before the following line, if present:
touch /var/lock/subsys/local
Some Linux kernels may not support labeled mounting and/or labeled
mounting on bootup, for some filesystem types. In that case, use the
--nolabels option on the mkbkup.pl command and a designated
drive for bootup of the backup medium. For example, the current operating
system is on /dev/hda, and the backup medium is on /dev/hdb:
mkbkup.pl --newpart /:ext3=2G,/usr:ext3,/home:jfs --nolabels all /dev/hdb
The above command line creates the backup with "/" and "/usr" as "ext3"
filesystems, "/home" as a "jfs" type filesystem, and all filesystems
will use device address mounting.
To boot from this drive, its address needs to be /dev/hda. To
accomplish this with docking bays, simply move the backup drive
from the /dev/hdb location to the /dev/hda location. If you are
not using docking bays, you can simply open the cabinet and re-cable
the backup drive from /dev/hdb to /dev/hda. On my PC, I use ext3
filesystems for the operating system and data, and for this situation
the Red Hat 2.4.18-14 kernel supports labeled mounting.
Mkbkup.pl accomplishes bootable backup creation by a six-step
process:
1. The designated backup drive and new partitions, if any, are
determined by command-line parameters.
2. Filesystem labels are created to allow mounting of labeled
filesystems via fstab on boot up. A special algorithm is employed
to make the labels unique.
3. The designated backup drive is partitioned, either according
to parameters, or according to the original partitions.
4. All data on the original drive is copied in tree structure
order starting from "/". Filesystems on the backup drive are created
and mounted as needed.
5. A master boot record is created on the designated backup drive
via grub (Grand Unified Boot Loader).
6. /boot/grub/grub.conf and /etc/fstab on the designated backup
drive are modified to reflect any partition changes from current
to backup.
When mkbkup.pl has completed its operation and there are no errors,
the backup hard drive is ready for booting as if it were the current
operating system. No rescue disk(s) or other additional media are
needed.
Mkbkup.pl has been run on Linux Red Hat 8.0, SUSE Linux 7.3, and
SUSE Linux 8.2. Mkbkup.pl is available as an open source project,
governed by an OpenBSD-style license template. It is available for
download at http://www.samag.com/code/. The download is a gzipped
tar file that contains the following:
mkbkup.pl -- A Perl script that creates a bootable backup.
mkbkup.1 -- Manpages in troff/preprocessor input text format.
swapfind -- A BASH shell script executable that is called from
/etc/rc.d/rc.local.
INSTALL -- A BASH shell script executable that installs mkbkup.pl
in /usr/local/bin, and mkbkup.1 in /usr/local/man, as default locations.
README -- A flat text file that is the documentation on installing
mkbkup.pl and the manpages.
This script has served me well, giving me peace of mind knowing
that my data, programs, and operating system do not depend on the
well being of a single hard drive. It has evolved from a Korn shell
script performing only verbatim backups of an OS that consisted
of the "/", /boot, and /home partitions, to a Perl script that creates
bootable backups from any mix of partitions and filesystems to the
same or to a new mix of partitions and filesystems.
I have a list of enhancements I want to make to mkbkup.pl. One
of these is to provide a graphical user interface. Another is to
add a Logical Volume Manager capability and possibly incorporate
the use of snapshot filesystems so that creation of a backup does
not interrupt workflow. I would also like to provide a lilo
option (i.e., incorporate lilo as an alternative to grub
as a boot loader.
Newton Hammet has more than 12 years experience working with
various flavors of Unix, including AIX, Tandem, Solaris, and Linux.
He has a bachelor's degree in Mathematics from Rice University.
He would like to thank Dr. Phil Carinas, President of Fortuitous
Technologies, Inc., for providing a review of his Perl source code,
and Margie Hammet for editing suggestions dealing with the manpages
and the Web site.
|