How to Backup a Hypervisor: Difference between revisions

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


The second and third steps are self explainatory, so I am only going over step 1. :)
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.
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:
To determine the volume format type, go into the directory where the images are:
cd /var/lib/libvirt/images/
 
Then type "qemu-img info image_name"
    cd /var/lib/libvirt/images/
qemu-img info my_image
 
image: my_image
Then determine the volume format type using qemu-img info:
file format: qcow2 <==== This is the disk/image format
 
virtual size: 10G (10737418240 bytes)
    qemu-img info my_image
disk size: 8.9G
 
cluster_size: 65536
    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:
You take a snapshot like this:


virsh snapshot-create-as my_image my_image_snapshot1
    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.
Where "my_image" is the domain name and "my_image_snapshot1" is the name that I am giving the snapshot.
Line 29: Line 34:
All the snapshots are stored as an xml file here:
All the snapshots are stored as an xml file here:


/var/lib/libvirt/qemu/snapshot/
    /var/lib/libvirt/qemu/snapshot/


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


virsh snapshot-list my_image
    virsh snapshot-list my_image


Name                Creation Time            State
    Name                Creation Time            State
------------------------------------------------------------
    ------------------------------------------------------------
my_image_snapshot1    2014-09-30 16:36:51 -0700 running
    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):
To convert the image from another format type to qcow2 (using my_image as an example):


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


You have to power off the domain first:  
You have to power off the domain first:  


virsh shutdown my_image
    virsh shutdown my_image


Next, convert my_image from raw to qcow2:
Next, convert my_image from raw to qcow2:


cd /var/lib/libvirt/images/
    cd /var/lib/libvirt/images/


qemu-img convert -f raw my_image -O qcow2 my_image.qcow2
    qemu-img convert -f raw my_image -O qcow2 my_image.qcow2


virsh edit my_image
Then edit the xml file:


Change this:   
    virsh edit my_image


<driver name='qemu' type='raw'/>
    Change this:  
      <source file='/var/lib/libvirt/images/my_image'/>
To this:


<driver name='qemu' type='qcow2'/>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/my_image.qcow2'/>
        <source file='/var/lib/libvirt/images/my_image'/>
    To this:


      <driver name='qemu' type='qcow2'/>
        <source file='/var/lib/libvirt/images/my_image.qcow2'/>
   
    Save and quit.


virsh start my_image
    virsh start my_image


That's it!


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


#!/bin/bash
#!/bin/bash
IMAGEDIR=/var/lib/libvirt/images/bulk
IMAGEDIR=/var/lib/libvirt/images/
BACKUPDIR=/var/backup/home/data/
BACKUPDIR=/var/backup/home/data/
# Cleanup of backup directory
    X=`ls -l $BACKUPDIR | wc -l`
    if (($X > 1)); then
          rm -rf $BACKUPDIR/*
    fi


# Check to see if image format is qcow2 and if so create a snapshot:
# Check to see if image format is qcow2 and if so create a snapshot:
Line 104: Line 104:


References: http://fedoraproject.org/wiki/Features/Virt_Live_Snapshots
References: http://fedoraproject.org/wiki/Features/Virt_Live_Snapshots
http://kashyapc.com/2012/09/14/externaland-live-snapshots-with-libvirt/
http://kashyapc.com/2012/09/14/externaland-live-snapshots-with-libvirt/
http://serverfault.com/questions/567234/how-to-replace-qemu-binary-with-newer-version-for-libvirt-live-snapshots
http://serverfault.com/questions/567234/how-to-replace-qemu-binary-with-newer-version-for-libvirt-live-snapshots
http://linux.die.net/man/1/virsh
http://linux.die.net/man/1/virsh
http://www.bashrc.in/2014/01/snapshot-of-kvm-vm-rhel-6.html
http://www.bashrc.in/2014/01/snapshot-of-kvm-vm-rhel-6.html

Revision as of 21:49, 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 -l $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://fedoraproject.org/wiki/Features/Virt_Live_Snapshots

http://kashyapc.com/2012/09/14/externaland-live-snapshots-with-libvirt/

http://serverfault.com/questions/567234/how-to-replace-qemu-binary-with-newer-version-for-libvirt-live-snapshots

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

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