Qcow2 snapshots

I’m using internal shapshots (which is a feature of qcow2 disk files )to back up my virtual machines.
This feature is available only from rhel 6.0 release. The best thing about it that it gives us fast way to backup our machines, without powering the off- snapshot will freeze our guest machine and unfreeze it when it’s done. It is very useful before any major upgrades. We are making a snapshot, then upgrade and if there is any problem we can revert back virtual machine to the state when snapshot was made.

First of all we have to ensure that out disk images are in qcow2 format. We can check this with command

[root@virthost ~]# qemu-img info /images/Guetst1.img
image: /images/Guest1.img
file format: qcow2
virtual size: 146G (157286400000 bytes)
disk size: 14G
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK

As we can see the “file format” shows qcow2, so we can perform a snapshot of this image.
We are using libvirt command virsh to manage snapshots. To create snapshot of a whole guest (that means all his disk images will be snapshoted) we are using:
[root@virthost ~]# virsh snapshot-create Guest
then we can see if the snapshot was made with command:

[root@virthost ~]# virsh snapshot-list Guest

The outpoot should be :
[root@virthost ~]# virsh snapshot-list Guest
Name Created State
------------------------------------------------------------
1351123665 2012-10-25 02:07:45 +0200 running

If we would check state of our guests during snapshot we would notice that it’s state is “paused” during snapshot and running after it’s completed.
Now if we want to revert out virtual machine to any specific snapshot that we made, we use command:
[root@virthost ~]# virsh snapshot-revert Guest 1351123665
where 1351123665 is the name of our snapshot. After this command whole virtual machine is reverted to this snapshot (including VM state, not only disks). If we want to revert only one disk image we have to use qemu-img commands:

[root@virthost ~]# qemu-img snapshot -a 1351123665 /images/Guest1.img

Now our VM state isn’t modified, but disk image Guest1.img was reverted to point of time when we made shapshot.
We can easily rename our snapshots to make more familiar names using:
[root@virthost ~]# virsh snapshot-edit Guest 1351123665
Below is a cron script which makes snapshot backup of all my VM’s, and keeps only last five snapshot:

#!/bin/bash

for i in `virsh list | tail -n+3 | awk ‘{print $2}’| head -n-1`; do

howmany=$(( `virsh snapshot-list $i | awk ‘ { print $1} ‘ | tail -n+3 | wc -l` -1))

if [ $howmany -gt 4 ]; then

virsh snapshot-list $i | awk ‘ { print $1} ‘ | tail -n+3 | head -1 |xargs virsh snapshot-delete $i

fi;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s