How to Backup and Restore MBR within Logical Volumes?

Mr. Teo En Ming (Zhang Enming) space.time.universe at gmail.com
Fri Nov 13 10:05:38 UTC 2009


On Fri, Nov 13, 2009 at 1:25 PM, Mr. Teo En Ming (Zhang Enming) <
space.time.universe at gmail.com> wrote:

>
>
> On Fri, Nov 13, 2009 at 11:17 AM, Mr. Teo En Ming (Zhang Enming) <
> space.time.universe at gmail.com> wrote:
>
>>
>>
>> On Fri, Nov 13, 2009 at 5:40 AM, Sergey Vlasov <vsu at altlinux.ru> wrote:
>>
>>> On Fri, Nov 13, 2009 at 12:57:22AM +0800, Mr. Teo En Ming (Zhang Enming)
>>> wrote:
>>> [...]
>>> > # VM 11: Rocks 5.1 x86_64 HPC Compute Cluster HVM domU
>>> >
>>> > kpartx -av /dev/virtualmachines/rocks0001
>>> >
>>> > dd if=/dev/virtualmachines/rocks0001 of=$DEST/rocks0001.mbr bs=512
>>> count=1
>>> >
>>> > partimage -d -M -b -z1 save /dev/mapper/virtualmachines-rocks0001p1
>>> > $DEST/virtualmachines-rocks0001p1.img
>>> >
>>> > partimage -d -M -b -z1 save /dev/mapper/virtualmachines-rocks0001p2
>>> > $DEST/virtualmachines-rocks0001p2.img
>>> >
>>> > partimage -d -M -b -z1 save /dev/mapper/virtualmachines-rocks0001p5
>>> > $DEST/virtualmachines-rocks0001p5.img
>>>
>>> Note that this VM (and some other VMs listed in your script) uses
>>> logical partitions.  In this case just saving a copy of MBR will not
>>> be enough to save partition layout - MBR describes only 4 primary
>>> partitions, and restoring just MBR will not restore extended
>>> partitions.
>>>
>>> One way to backup the complete partition layout is by saving also
>>> the output of "sfdisk -d $device"; the resulting file can be used as
>>> input to sfdisk to restore all partitions, including logical ones.
>>> Saving MBR is still needed together with sfdisk, because it saves
>>> the boot code (used for HVM) and CHS geometry information (which can
>>> be used during boot in some cases).
>>>
>>> -----BEGIN PGP SIGNATURE-----
>>> Version: GnuPG v1.4.10 (GNU/Linux)
>>>
>>> iEYEARECAAYFAkr8gM8ACgkQW82GfkQfsqLa+QCaAqpO5NWhYHtKVi3M5ytERw27
>>> eC0AnjsJuG34MAR1jZRejBiJCVybQvc1
>>> =RRGH
>>> -----END PGP SIGNATURE-----
>>>
>>>
>>> _______________________________________________
>>> Xen-users mailing list
>>> Xen-users at lists.xensource.com
>>> http://lists.xensource.com/xen-users
>>>
>>
>>
>>
>> Thank you for pointing this out. Otherwise I would made incomplete
>> backups.
>>
>> Would it be best practice to always backup both the MBR and the partition
>> geometry using sfdisk whenever cloning our harddisks on desktops and
>> servers?
>>
>> Besides sfdisk, there are also other partitioning tools like fdisk,
>> cfdisk, and parted.
>>
>> Could these other tools also be used for backing up the partition geometry
>> like sfdisk?
>>
>> Thank you.
>>
>>
>> --
>> Mr. Teo En Ming (Zhang Enming) Dip(Mechatronics) BEng(Hons)(Mechanical
>> Engineering)
>> Alma Maters:
>> (1) Singapore Polytechnic
>> (2) National University of Singapore
>> My Primary Blog: http://teo-en-ming-aka-zhang-enming.blogspot.com
>> My Secondary Blog: http://enmingteo.wordpress.com
>> My Youtube videos: http://www.youtube.com/user/enmingteo
>> Email: space.time.universe at gmail.com
>> Mobile Phone (Starhub Prepaid): +65-8369-2618
>> Street: Bedok Reservoir Road
>> Country: Singapore
>>
>
> Hi,
>
> I have made some improvements to the backup/cloning script. Please help me
> to vet it through for any mistakes. Thank you very much!
>
>
> <SCRIPT>
>
    ---SNIPPED---

> </SCRIPT>
>
> --
> Mr. Teo En Ming (Zhang Enming) Dip(Mechatronics) BEng(Hons)(Mechanical
> Engineering)
> Alma Maters:
> (1) Singapore Polytechnic
> (2) National University of Singapore
> My Primary Blog: http://teo-en-ming-aka-zhang-enming.blogspot.com
> My Secondary Blog: http://enmingteo.wordpress.com
> My Youtube videos: http://www.youtube.com/user/enmingteo
> Email: space.time.universe at gmail.com
> Mobile Phone (Starhub Prepaid): +65-8369-2618
> Street: Bedok Reservoir Road
> Country: Singapore
>

Hi All,

This is the final version of my backup script. I have gone through the
script once to make sure there are no mistakes. If I want to capture any
errors during the execution of the backup/cloning script, which of the
following commands do I run?

# ./backup-script.sh > backup.log

OR

# ./backup-script.sh 2> backup.log

After completing the backup script, I will proceed to write the restore
script.

If there are any errors, please let me know because I do not want to make
any mistakes with the cloning process. Thank you.

<SCRIPT>
#!/bin/sh

###############################################################################################################
###############################################################################################################
# Script to Backup/Clone Xen Host/Dom0 and all DomUs which are using Logical
Volumes as Virtual Hard Disks
###############################################################################################################
###############################################################################################################

# Written by:

# Mr. Teo En Ming (Zhang Enming) Dip(Mechatronics) BEng(Hons)(Mechanical
Engineering)
# Alma Maters:
# (1) Singapore Polytechnic
# (2) National University of Singapore
# Primary Blog: http://teo-en-ming-aka-zhang-enming.blogspot.com
# Secondary Blog: http://enmingteo.wordpress.com
# Youtube Videos: http://www.youtube.com/user/enmingteo
# Xen Tutorials and Video Demos: http://www.xen.org/support/tutorial.html
# Email: space.time.universe at gmail.com
# MSN: teoenming at hotmail.com
# Mobile Phone (Starhub Prepaid): +65-8369-2618
# Street: Bedok Reservoir Road
# Country: Singapore

# First written: 13 November 2009 Friday 12:38 A.M. Singapore time
# Last updated: 13 November 2009 Friday 12:38 A.M. Singapore time
# Last updated: 13 November 2009 Friday 1:20 P.M. Singapore time
# Last updated: 13 November 2009 Friday 5:23 P.M. Singapore time


# REFERNCE: "Geek Sheet: Bare-metal backup and recovery", May 7th, 2008,
Jason Perlow
# URL: http://blogs.zdnet.com/BTL/?p=8759

# Usage Instructions:

# Boot up your desktop/server with System Rescue CD version 1.3.2 for
i386/amd64.
# Then execute this backup script. You need to adapt this script to work for
your environment.
# Download System Rescue CD from http://www.sysresccd.org/Main_Page

# FAQ
# Q: Why do I want to write this cloning script when there are many open
source cloning software available?
# A: I am previously using Clonezilla. It is a very good cloning software.
However, it could not detect all the
#    virtual machines in my LVM2 logical volumes and had to fall back on
using dd to image all my domUs,
#    which resulted in an enormous 300 GB (!) backup image set and an
extremely lengthy cloning process of
#    7 hours. Clonezilla is very good for LVs which only contain
filesystems, but not for LVs which contain
#    virtual machines with MBR, partition geometries and full fledged
operating systems. It is very diffcult
#    to image nested logical volumes, i.e. logical volumes within logical
volumes. And the PV within a LV is
#    the barrier. So it is not Clonezilla's fault. Anybody who knows how to
activate all the logical volumes
#    of all the volume groups within LVM2 physical volumes inside a logical
volume hosting a virtual machine,
#    please share your knowledge with me. Thank you very much.

# No. of QC Checks Performed on this backup script: 1

###############################################################################################################
# Declare Variables
###############################################################################################################

HARDDISK=/dev/sda
DEST=/media/hitachi/test
FSA_COMPLEVEL=9
PROCESSOR_CORES=2
GZIP_COMPLEVEL=fast
PARTIMAGE_COMPLEVEL=1

###############################################################################################################
# Create Storage Directory for Backup Images
###############################################################################################################

if [ ! -d $DEST ]
then
    mkdir -p $DEST
fi

###############################################################################################################
###############################################################################################################
# Cloning Xen Host/Dom0
###############################################################################################################
###############################################################################################################

# Backup MBR

dd if=$HARDDISK of=$DEST/f11-xen-dom0-sda.mbr bs=512 count=1

# Backup Partition Geometry
sfdisk -d $HARDDISK > $DEST/f11-xen-dom0-partition-geometry-sda.sfdisk

# Activate all logical volumes in all volume groups

vgchange -ay

# Backup UUIDs of PVs

pvdisplay > $DEST/f11-xen-dom0-pvdisplay.txt

# Backup LVM2 Metadata for All Volume Groups


# Backup the configuration of dedicated volume group for dom0

vgcfgbackup -d -v vg_fedora11_host -f
$DEST/vg_fedora11_host-vgcfg.lvm2.metadata

# Backup the configuration of dedicated volume group for domUs

vgcfgbackup -d -v virtualmachines -f
$DEST/virtualmachines-vgcfg.lvm2.metadata

# Backup /boot partition and all logical volumes of dedicated volume group
for host/dom0

fsarchiver savefs -v -z $FSA_COMPLEVEL -j $PROCESSOR_CORES
$DEST/f11-xen-dom0-filesystems.fsa /dev/sda1 \
/dev/vg_fedora11_host/lv_home /dev/vg_fedora11_host/lv_root
/dev/vg_fedora11_host/lv_var

###############################################################################################################
###############################################################################################################
# Cloning Xen-based Virtual Machines/DomUs/Guest Operating Systems/VMs
###############################################################################################################
###############################################################################################################

# The following 8 virtual machines have LVM2 physical volumes, so it is not
possible to image using partimage
# or fsarchiver directly, as both do not support PVs.  The only convenient
way is to image the logical volume
# for the VM using dd, ie. sector by sector copy, and then piping it to gzip
for a smaller backup image.
# Other methods would have been more involved and tedious.

# VM 1: Fedora 11 x86_64 PV domU (MPICH2 Virtual Supercomputer Center)

###############################################################################################################

# Sector by sector copy
dd if=/dev/virtualmachines/f11-pv-hpc-node0001 | gzip --$GZIP_COMPLEVEL > \
$DEST/virtualmachines-f11-pv-hpc-node0001.img.gz

# VM 2: Fedora 11 x86_64 PV domU (MPICH2 Virtual Supercomputer Center)

###############################################################################################################

# Sector by sector copy
dd if=/dev/virtualmachines/f11-pv-hpc-node0002 | gzip --$GZIP_COMPLEVEL > \
$DEST/virtualmachines-f11-pv-hpc-node0002.img.gz

# VM 3: Fedora 11 x86_64 PV domU (MPICH2 Virtual Supercomputer Center)

###############################################################################################################

# Sector by sector copy
dd if=/dev/virtualmachines/f11-pv-hpc-node0003 | gzip --$GZIP_COMPLEVEL > \
$DEST/virtualmachines-f11-pv-hpc-node0003.img.gz

# VM 4: Fedora 11 x86_64 PV domU (MPICH2 Virtual Supercomputer Center)

###############################################################################################################

# Sector by sector copy
dd if=/dev/virtualmachines/f11-pv-hpc-node0004 | gzip --$GZIP_COMPLEVEL > \
$DEST/virtualmachines-f11-pv-hpc-node0004.img.gz

# VM 5: Fedora 11 x86_64 PV domU (MPICH2 Virtual Supercomputer Center)

###############################################################################################################

# Sector by sector copy
dd if=/dev/virtualmachines/f11-pv-hpc-node0005 | gzip --$GZIP_COMPLEVEL > \
$DEST/virtualmachines-f11-pv-hpc-node0005.img.gz

# VM 6: Fedora 11 x86_64 PV domU (MPICH2 Virtual Supercomputer Center)

###############################################################################################################

# Sector by sector copy
dd if=/dev/virtualmachines/f11-pv-hpc-node0006 | gzip --$GZIP_COMPLEVEL > \
$DEST/virtualmachines-f11-pv-hpc-node0006.img.gz

# VM 7: Fedora 11 x86_64 PV domU (MPICH2 Virtual Supercomputer Center)

###############################################################################################################

# Sector by sector copy
dd if=/dev/virtualmachines/f11-pv-hpc-node0007 | gzip --$GZIP_COMPLEVEL > \
$DEST/virtualmachines-f11-pv-hpc-node0007.img.gz

# VM 8: Fedora 11 x86_64 PV domU (MPICH2 Virtual Supercomputer Center)

###############################################################################################################

# Sector by sector copy
dd if=/dev/virtualmachines/f11-pv-hpc-node0008 | gzip --$GZIP_COMPLEVEL > \
$DEST/virtualmachines-f11-pv-hpc-node0008.img.gz


# VM 9: FreeBSD 8.0 RC2 UNIX amd64 HVM domU
###############################################################################################################

# Sector by sector copy
dd if=/dev/virtualmachines/freebsd | gzip --$GZIP_COMPLEVEL > \
$DEST/virtualmachines-freebsd.img.gz


# VM 10: OpenSolaris 2009.06 UNIX amd64 PV domU
###############################################################################################################

# Sector by sector copy
dd if=/dev/virtualmachines/opensolaris | gzip --$GZIP_COMPLEVEL > \
$DEST/virtualmachines-opensolaris.img.gz

# VM 11: Rocks 5.1 x86_64 HPC Compute Cluster HVM domU (based on CentOS 5.2)

###############################################################################################################

# Add partition mappings in /dev/mapper
kpartx -av /dev/virtualmachines/rocks0001

# Backup MBR
dd if=/dev/virtualmachines/rocks0001 of=$DEST/virtualmachines-rocks0001.mbr
bs=512 count=1

# Backup Partition Geometry
sfdisk -d /dev/virtualmachines/rocks0001 >
$DEST/virtualmachines-rocks0001.sfdisk

# Backup Filesystems
partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-rocks0001p1 \
$DEST/virtualmachines-rocks0001p1.img

partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-rocks0001p2 \
$DEST/virtualmachines-rocks0001p2.img

partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-rocks0001p5 \
$DEST/virtualmachines-rocks0001p5.img

# Delete partition mappings in /dev/mapper
kpartx -dv /dev/virtualmachines/rocks0001

# VM 12: Rocks 5.1 x86_64 HPC Compute Cluster HVM domU (based on CentOS 5.2)

###############################################################################################################

# Add partition mappings in /dev/mapper
kpartx -av /dev/virtualmachines/rocks0002

# Backup MBR
dd if=/dev/virtualmachines/rocks0002 of=$DEST/virtualmachines-rocks0002.mbr
bs=512 count=1

# Backup Partition Geometry
sfdisk -d /dev/virtualmachines/rocks0002 >
$DEST/virtualmachines-rocks0002.sfdisk

# Backup Filesystems
partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-rocks0002p1 \
$DEST/virtualmachines-rocks0002p1.img

partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-rocks0002p2 \
$DEST/virtualmachines-rocks0002p2.img

partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-rocks0002p5 \
$DEST/virtualmachines-rocks0002p5.img

# Delete partition mappings in /dev/mapper
kpartx -dv /dev/virtualmachines/rocks0002

# VM 13: Rocks 5.1 x86_64 HPC Compute Cluster HVM domU (based on CentOS 5.2)

###############################################################################################################

# Add partition mappings in /dev/mapper
kpartx -av /dev/virtualmachines/rocks0003

# Backup MBR
dd if=/dev/virtualmachines/rocks0003 of=$DEST/virtualmachines-rocks0003.mbr
bs=512 count=1

# Backup Partition Geometry
sfdisk -d /dev/virtualmachines/rocks0003 >
$DEST/virtualmachines-rocks0003.sfdisk

# Backup Filesystems
partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-rocks0003p1 \
$DEST/virtualmachines-rocks0003p1.img

partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-rocks0003p2 \
$DEST/virtualmachines-rocks0003p2.img

partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-rocks0003p5 \
$DEST/virtualmachines-rocks0003p5.img

# Delete partition mappings in /dev/mapper
kpartx -dv /dev/virtualmachines/rocks0003

# VM 14: Rocks 5.1 x86_64 HPC Compute Cluster HVM domU (based on CentOS 5.2)

###############################################################################################################

# Add partition mappings in /dev/mapper
kpartx -av /dev/virtualmachines/rocks0004

# Backup MBR
dd if=/dev/virtualmachines/rocks0004 of=$DEST/virtualmachines-rocks0004.mbr
bs=512 count=1

# Backup Partition Geometry
sfdisk -d /dev/virtualmachines/rocks0004 >
$DEST/virtualmachines-rocks0004.sfdisk

# Backup Filesystems
partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-rocks0004p1 \
$DEST/virtualmachines-rocks0004p1.img

partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-rocks0004p2 \
$DEST/virtualmachines-rocks0004p2.img

partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-rocks0004p5 \
$DEST/virtualmachines-rocks0004p5.img

# Delete partition mappings in /dev/mapper
kpartx -dv /dev/virtualmachines/rocks0004

# VM 15: Rocks 5.1 x86_64 HPC Compute Cluster HVM domU (based on CentOS 5.2)

###############################################################################################################

# Add partition mappings in /dev/mapper
kpartx -av /dev/virtualmachines/rocks0005

# Backup MBR
dd if=/dev/virtualmachines/rocks0005 of=$DEST/virtualmachines-rocks0005.mbr
bs=512 count=1

# Backup Partition Geometry
sfdisk -d /dev/virtualmachines/rocks0005 >
$DEST/virtualmachines-rocks0005.sfdisk

# Backup Filesystems
partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-rocks0005p1 \
$DEST/virtualmachines-rocks0005p1.img

partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-rocks0005p2 \
$DEST/virtualmachines-rocks0005p2.img

partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-rocks0005p5 \
$DEST/virtualmachines-rocks0005p5.img

# Delete partition mappings in /dev/mapper
kpartx -dv /dev/virtualmachines/rocks0005

# VM 16: Slackware64 13.0 amd64 HVM domU
###############################################################################################################

# Add partition mappings in /dev/mapper
kpartx -av /dev/virtualmachines/slackware64

# Backup MBR
dd if=/dev/virtualmachines/slackware64
of=$DEST/virtualmachines-slackware64.mbr bs=512 count=1

# Backup Partition Geometry
sfdisk -d /dev/virtualmachines/slackware64 >
$DEST/virtualmachines-slackware64.sfdisk

# Backup Filesystems
partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-slackware64p1 \
$DEST/virtualmachines-slackware64p1.img

partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-slackware64p2 \
$DEST/virtualmachines-slackware64p2.img

# Delete partition mappings in /dev/mapper
kpartx -dv /dev/virtualmachines/slackware64

# VM 17: Ubuntu 9.10 Karmic Koala Linux HVM domU
###############################################################################################################

# Add partition mappings in /dev/mapper
kpartx -av /dev/virtualmachines/ubuntu910

# Backup MBR
dd if=/dev/virtualmachines/ubuntu910 of=$DEST/virtualmachines-ubuntu910.mbr
bs=512 count=1

# Backup Partition Geometry
sfdisk -d /dev/virtualmachines/ubuntu910 >
$DEST/virtualmachines-ubuntu910.sfdisk

# Backup Filesystems
partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-ubuntu910p1 \
$DEST/virtualmachines-ubuntu910p1.img

# Delete partition mappings in /dev/mapper
kpartx -dv /dev/virtualmachines/ubuntu910

# VM 18: Windows XP Home Edition SP3 32-bit HVM domU with VGA passthrough
(requires Intel VT-d)
###############################################################################################################

# Add partition mappings in /dev/mapper
kpartx -av /dev/virtualmachines/winxphome32

# Backup MBR
dd if=/dev/virtualmachines/winxphome32
of=$DEST/virtualmachines-winxphome32.mbr bs=512 count=1

# Backup Partition Geometry
sfdisk -d /dev/virtualmachines/winxphome32 >
$DEST/virtualmachines-winxphome32.sfdisk

# Backup Filesystems
partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save
/dev/mapper/virtualmachines-winxphome32p1 \
$DEST/virtualmachines-winxphome32p1.img

# Delete partition mappings in /dev/mapper
kpartx -dv /dev/virtualmachines/winxphome32

###############################################################################################################
# EOF
###############################################################################################################

</SCRIPT>

-- 
Mr. Teo En Ming (Zhang Enming) Dip(Mechatronics) BEng(Hons)(Mechanical
Engineering)
Alma Maters:
(1) Singapore Polytechnic
(2) National University of Singapore
My Primary Blog: http://teo-en-ming-aka-zhang-enming.blogspot.com
My Secondary Blog: http://enmingteo.wordpress.com
My Youtube videos: http://www.youtube.com/user/enmingteo
Email: space.time.universe at gmail.com
Mobile Phone (Starhub Prepaid): +65-8369-2618
Street: Bedok Reservoir Road
Country: Singapore
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/fedora-list/attachments/20091113/45a9a9fa/attachment-0001.htm>


More information about the fedora-list mailing list