KVM Backup Procedure

1. Determine the running KVM instances

Run the command:

virsh list

This will show a list of instances that are running:

 Id Name State
----------------------------------------------------
 2 dev.hypnoray.com running
 3 Hypnoray.com running

 

2. Shutdown the instance that is going to be backed up

Run the command:

virsh shutdown yourKVMName 

e.g.

virsh shutdown Hypnoray.com

3. Wait until the KVM has shutdown instance

It may take a short while to shutdown the instance. The virsh list command will no longer show the name of the instance when it is complely shutdown.

Note.  Can this check be automated?

4. Save the instance’s XML definitions

Use the virsh dumpxml command to save your instance’s XML definitions

virsh dumpxml yourKVMName > yourbackupXMLDefinitionFile

e.g.

virsh dumpxml Hypnoray.com > ~/backups/Hypnoray.xml

5. Take a copy of the instance’s disk

The disk images are located under /var/lib/libvirt/images.  The name of the image should correlate with the name of the KVM instance.  Copy the disk image to your backup directory.  e.g.

cp /var/lib/libvirt/images/Hypnoray.com.qcow2 ~/backups/Hypnoray.com.qcow2

6. Restart the KVM instance

run virsh start yourKVMName to restart the instance.  e.g.

virsh start Hypnoray.com

7. (Optional) Shrink the disk image

qemu-img convert -O qcow2 ~/backups/Hypnoray.com.qcow2 ~/backups/HypnorayShrunk.com.qcow2
mv ~/backups/HypnorayShrunk.com.qcow2 ~/backups/Hypnoray.com.qcow2

8. Create a tgz archive of the backup files

8.1 Create a backup directly on a remote host

c | ssh USERNAME@HOST “dd of=BACKUPFILENAME.tgz”

e.g.

tar cvzf - ~/backups/ | ssh [email protected] "dd of=/backup/Hypnoray.com.tgz"

8.2 (or create a local Backup)

tar cvzf BACKUPFILENAME.tgz BACKUPDIRECTORY

Restoring Saved Backups

1. Extract the Backup archive

tar xvfz BACKUPFILENAME.tgz

2. Restore the KVM XML definitions

virsh define –file nameofvm.xml

3. Copy the extracted qcow2 file to /var/lib/libvirt/images

cp InstanceName.qcow2 /var/lib/libvirt/images

4. Restart the KVM Instance

virsh start InstanceName