How to Backup a Hypervisor: Difference between revisions
(Created page with "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 tarbal...") |
No edit summary |
||
(6 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
1) Create snapshots of all images | 1) Create snapshots of all images | ||
2) Create a tarball of the /var/lib/libvirt directory | 2) Create a tarball of the /var/lib/libvirt directory | ||
3) Create a tarball of the /etc/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. :) | 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 | 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 27: | 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 | |||
------------------------------------------------------------ | |||
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 | |||
image: my_image | file format: raw | ||
file format: raw | virtual size: 10G (10737418240 bytes) | ||
virtual size: 10G (10737418240 bytes) | disk size: 8.9G | ||
disk size: 8.9G | cluster_size: 65536 | ||
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 | |||
Then edit the xml file: | |||
virsh edit my_image | virsh edit my_image | ||
Change this: | Change this: | ||
<driver name='qemu' type='raw'/> | |||
<source file='/var/lib/libvirt/images/my_image'/> | |||
<driver name='qemu' type=' | To this: | ||
<driver name='qemu' type='qcow2'/> | |||
<source file='/var/lib/libvirt/images/my_image.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: | |||
<nowiki>#!/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</nowiki> | |||
References: | |||
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 |
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: