Coffee & Beer

Rantings and Ravings of the technical sort

Proper Kvm Xml Backups With Ruby

So, while kvm keeps the xml describing a running domain in /var/run/libvirt/qemu, it turns out this isn’t exactly the cleanest thing to backup. I HAD been doing this, with a simple rsync, but realizing this I decided we should do it propper, with libvirt runy bindings and all. So, here it is:

(dump_xmls.rb) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env ruby

require 'libvirt'
hostname=`hostname --short`.chomp
conn = Libvirt::open('qemu:///system')
vms = Hash.new
conn.list_domains.each do |domid|
        dom = conn.lookup_domain_by_id(domid)
        vms[dom.name] = dom.xml_desc
end

destination = "/n/kvm_stor01/xml/#{hostname}/"
vms.each do |dom,xml|
  file_dest = "#{destination}#{dom}" + ".xml"
 puts "writting xml for #{dom} to #{file_dest}"
 f=File.open(file_dest, "w")
 f.write(xml)
 f.close
end

Pretty simple. Figure out the hostname (this is deployed to multiple kvm hosts), get the list of domains running here, itterate on each one and get the name and xml, and populate a hash with these. Then, itterate that hash dumping the xml into a file on our shared storage, which is in turn checkpointed and backed up. Ta da.

Comments