How to Backup a Hypervisor: Difference between revisions

From DISI
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 83: Line 83:


     <nowiki>#!/bin/bash
     <nowiki>#!/bin/bash
     IMAGEDIR=/var/lib/libvirt/images/
     IMAGEDIR=/var/lib/libvirt/images/
     BACKUPDIR=/var/backup/home/data/
     BACKUPDIR=/var/backup/home/data/

Latest revision as of 23:05, 8 October 2014

There are essentially three steps to fully backing up a hypervisor:

1) Create snapshots of all images

2) Create a tarball of the /var/lib/libvirt directory

3) Create a tarball of the /etc/libvirt directory

The second and third steps are self explainatory, so I am only going over step 1. :)


In order to create a snapshot of an image it needs to have a qcow2 image format.

To determine the volume format type, go into the directory where the images are:

   cd /var/lib/libvirt/images/

Then determine the volume format type using qemu-img info:

   qemu-img info my_image
   image: my_image
   file format: qcow2			<==== This is the disk/image format
   virtual size: 10G (10737418240 bytes)
   disk size: 8.9G
   cluster_size: 65536

You take a snapshot like this:

   virsh snapshot-create-as my_image my_image_snapshot1

Where "my_image" is the domain name and "my_image_snapshot1" is the name that I am giving the snapshot.

All the snapshots are stored as an xml file here:

   /var/lib/libvirt/qemu/snapshot/

You can also see a list of snapshots for a particular domain by typing:

   virsh snapshot-list my_image
    Name                 Creation Time             State
   ------------------------------------------------------------
    my_image_snapshot1    2014-09-30 16:36:51 -0700 running


To convert the image from another format type to qcow2 (using my_image as an example):

   qemu-img info my_image
   image: my_image
   file format: raw
   virtual size: 10G (10737418240 bytes)
   disk size: 8.9G
   cluster_size: 65536

You have to power off the domain first:

   virsh shutdown my_image

Next, convert my_image from raw to qcow2:

   cd /var/lib/libvirt/images/
   qemu-img convert -f raw my_image -O qcow2 my_image.qcow2

Then edit the xml file:

   virsh edit my_image
   Change this:    
     <driver name='qemu' type='raw'/>
   To this:
     <driver name='qemu' type='qcow2'/>
   Save and quit.
   virsh start my_image


I have a cron job that runs this script I wrote to back up the hypervisor monthly:

   #!/bin/bash

    IMAGEDIR=/var/lib/libvirt/images/
    BACKUPDIR=/var/backup/home/data/
    
    # Check to see if image format is qcow2 and if so create a snapshot:	
    for HOST in `ls $IMAGEDIR`;do 
       FORMAT=`qemu-img info $HOST | grep format | cut -d' ' -f3`
       if ["$FORMAT" == "qcow2"]; then
         virsh snapshot-create-as $HOST $HOST_1
       fi
    done

    tar -czvf $BACKUPDIR/var_libvirt.tar.gz /var/lib/libvirt
    tar -czvf $BACKUPDIR/etc_libvirt.tar.gz /etc/libvirt


References:

http://linux.die.net/man/1/virsh

http://www.bashrc.in/2014/01/snapshot-of-kvm-vm-rhel-6.html