#!/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@xxxxxxxxx # MSN: teoenming@xxxxxxxxxxx # 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 # Last updated: 14 November 2009 Saturday 3:19 A.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 # VM 19: openSUSE 11.2 x86_64 Linux PV domU ############################################################################################################### # Add partition mappings in /dev/mapper kpartx -av /dev/virtualmachines/opensuse # Backup MBR dd if=/dev/virtualmachines/opensuse of=$DEST/virtualmachines-opensuse.mbr bs=512 count=1 # Backup Partition Geometry sfdisk -d /dev/virtualmachines/opensuse > $DEST/virtualmachines-opensuse.sfdisk # Backup Filesystems partimage -d -M -b -z$PARTIMAGE_COMPLEVEL save /dev/mapper/virtualmachines-opensuse2 \ $DEST/virtualmachines-opensuse2.img # Delete partition mappings in /dev/mapper kpartx -dv /dev/virtualmachines/opensuse ############################################################################################################### # EOF ###############################################################################################################