virt-back: a python libvirt backup utility for kvm xen virtualbox

This entry is part 1 of 4 in the series Virt-back

Over the weekend I wrote virt-back, a backup utility for QEMU, KVM, XEN, or Virtualbox guests.

virt-back is a python application that uses the libvirt API to safely shutdown, gzip, and restart guests.

The backup process logs to syslog for auditing and virt-back works great with cron for scheduling outages. Virt-back is in active development so feel free to give suggestions or branch the source.

virt-back has been placed in the public domain and the latest version may be downloaded here: https://bitbucket.org/russellballestrini/virt-back

Installation:

The fastest way to install virt-back is to use pip or setuptools.

Try sudo pip install virt-back or sudo easy_install virt-back

Otherwise you may manually install virt-back
sudo wget https://bitbucket.org/russellballestrini/virt-back/raw/tip/virt-back -O  /usr/local/bin/virt-back
sudo chmod 755 /usr/local/bin/virt-back
Test installation:
virt-back --help
Example cronjob:
15  2  *  *  1  /usr/local/bin/virt-back --quiet --backup sagat
15  23 *  *  5  /usr/local/bin/virt-back --quiet --backup mbison
Manual:
russell@host:~$ virt-back --help
Usage: virt-back [options]
Options:
  -h, --help            show this help message and exit
  -q, --quiet           prevent output to stdout
  -d, --date            append date to tar filename [default: no date]
  -g, --no-gzip         do not gzip or tar the resulting files
  -a amount, --retention=amount
                        backups to retain [default: 3]
  -p 'PATH', --path='PATH'
                        backup path [default: '/KVMBACK']
  -u 'URI', --uri='URI'
                        optional hypervisor uri

  Actions for info testing:
    These options display info or test a list of guests.

    -i, --info          info/test a list of guests (space delimited dom names)
    --info-all          attempt to show info on ALL guests

  Actions for a list of dom names:
    WARNING:  These options WILL bring down guests!

    -b, --backup        backup a list of guests (space delimited dom names)
    -r, --reboot        reboot a list of guests (space delimited dom names)
    -s, --shutdown      shutdown a list of guests (space delimited dom names)
    -c, --create        start a list of guests (space delimited dom names)

  Actions for all doms:
    WARNING:  These options WILL bring down ALL guests!

    --backup-all        attempt to shutdown, backup, and start ALL guests
    --reboot-all        attempt to shutdown and then start ALL guests
    --shutdown-all      attempt to shutdown ALL guests
    --create-all        attempt to start ALL guests

virt-back: restoring from backups

This entry is part 2 of 4 in the series Virt-back

In a perfect world we should create backups but never need them. Although this statement holds truth, creating guest backups provides many more benefits.

The most common reasons system administrators restore from a virt-back guest backup:

  • recovering from data corruption
  • recovering deleted files
  • recovering from a virus infection
  • recovering from a compromised server
  • backing out a failed change
  • rolling back to a previous state
  • testing disaster recovery plans
  • cloning a server
  • building test environments

During this article we will cover how to restore a system from a virt-back guest backup. This article will not cover how to restore a VM host server.

Virt-back guest restore procedure

In this guide our guest mbison has failed with a major corruption and we would like to restore from our backups. We have our running production guest images in /KVMROOT and our virt-back guest backups in /KVMBACK.

Overview:

  1. Ensure the guest is shut off.
  2. move the bad image file out of the way
  3. untar the virt-back backup into place
  4. power up the guest

Detailed Procedure:
  1. Verify the guest is shut off by running:
    virt-back --info-all
        
  2. We noticed that mbison was still running so we invoked:
    virt-back --verbose --shutdown mbison
        
  3. Move the corrupted image file out of the way:
    mv /KVMROOT/mbison.img /KVMROOT/mbison.img.NFG
        
  4. Unzip and unarchive the backup using the following command:
    sudo tar -xzvf /KVMBACK/mbison.tar.gz -C /KVMROOT --strip 1
        
  5. When the untar completes, start the guest:
    virt-back --verbose --create mbison
        
  6. Connect to the guest over SSH and verify that all required services and applications start. Determine if the restore was successful.

virt-back’s Domfetcher class returns doms from libvirt API

This entry is part 4 of 4 in the series Virt-back
2013-03-02-scale-virt-back-domfetcher-scaled

I’m hooked…

I attended SCaLE 11x, my first technical conference, and had an amazing time. My favorite talk was Michael Day’s “Advancements with Open Virtualization & KVM” (link to slides). Michael’s presentation inspired me to continue my work on virt-back.

During my trip home I used the in-flight wifi to push this commit into the cloud from the clouds! This particular commit re-factored the dom object list generation into a simple-to-use class called Domfetcher. Domfetcher abstracts the libvirt API and grants access to the following helper methods:

get_all_doms( )
Return a list of all dom objects

get_doms_by_names( guest_names=[] )
Accept a list of guest_names, return a list of related dom objects

get_running_doms( )
Return a list of running dom objects

get_shutoff_doms( )
Return a list of shutoff but defined dom objects

This is an example of how to use Domfetcher:

import virtback

# optionally supply hypervisor uri
domfetcher = virtback.Domfetcher()

doms = domfetcher.get_running_doms()

for dom in doms:
    print dom.name()

for dom in doms:
    print dom.info()

for dom in doms:
    print dom.shutdown()

As always thanks for reading!