Quick and dirty script for making cold backups of KVM Virtual Machines

Note that this depends on “virsh”, the libvirt client, and “bzip2”.

Basically, what this script does is send a shutdown signal to each of the specified nodes, back up the disk images, and then bzip them in the background while it restarts the node. So having said this, you’ll want to make sure your services on your VM’s are set to shut down and start up gracefully when the OS is shut down / restarted.


# This is who we'll back up
machines="Dev Test Foobar"

# Keep the house clean

# Store backups here. NFS to another machine would make sense. 
# so that your backups are not local.

# Logs go here.

# End parameters

# Timestamp for the log file
right_now=`date '+%m%d%Y_%H%M%p'`

exec 1>/${log_dir}/backup_vms.${right_now}.log 2>&1

print_date() {
   date '+%m%d%Y_%H%M%p'

for machine in $machines
   if [[ ! -d ${backup_dir}/${machine} ]];
      mkdir -p ${backup_dir}/${machine}

   echo "Backing up VM configuration"
   virsh dumpxml $machine > ${backup_dir}/${machine}/${machine}.xml

   echo "Sending shutdown signal to $machine"
   virsh shutdown $machine
   echo "   Return code: $?"
   echo -n "Waiting for machine to shut down "
   for i in 1 2 3 4 5
      echo -n "."
      virsh list | grep -v "^$" | grep -v "^ Id" | grep -v "\-\-\-\-\-" | awk '{print $2" "$3}' | grep $machine | while read name state
         if [[ $state -eq "running" ]]
            sleep 60

   echo "Copying disk(s)"
   virsh domblklist $machine | grep -v "^$" | grep -v "^Target" | grep -v "\-\-\-\-\-" | awk '{print $2}' | while read disk
      echo "   $disk ..."
      copy_disk="${backup_dir}/${machine}/`basename ${disk}`.`print_date`"
      echo "   Copying $disk to $copy_disk"
      fuser $disk 1>/dev/null 2>&1
      if (( $? == 0 ))
         echo "   Disk $disk is still in use! "
         echo "   Copy started at `print_date`"
         cp $disk $copy_disk
         echo "   Return code: $?"
         echo "   Copy ended at `print_date`"
         echo "   Backgrounding bzip of $copy_disk"
         nohup bzip2 $copy_disk &

   echo "Starting machine $machine"
   virsh start $machine
   echo "   Return code: $?"


   echo "Removing old backups."
   find $backup_dir -type f -mtime +$days_to_keep -ls
   find $backup_dir -type f -mtime +$days_to_keep -exec rm -f {} \;

12 thoughts on “Quick and dirty script for making cold backups of KVM Virtual Machines

  1. Thanks for this script.
    I should mention that if you have an ISO attached to the VM image then these will get backed up as well. So unattach any ISO before running it.
    Also, to take backup of the VM configuration file add the following to your script:

    virsh dumpxml vm_name > /tmp/vm_name.xml


  2. Very nice script!! Exactly what i needed. 1 small remark that would make it perfect : if the “logs” directory doesn’t exist it would be nice to create it on the first run :)))

    For compression bzip works fine in the background but a full uncompressed copy of the disk image can be slow if you copy the image externally. I have several images with quite a bit of empty space in the image, I now use pigz (multi-threaded compression) and do copy and compression on the fly using tar.gz. With the VM in shutdown state i can use full cpu capacity to compress the images. The copy operation saturates my ethernet link, using on the fly compression doesn’t even drive cpu utllization of the Dual 6-core server to 100% but still saturates the ethernet link (so the compression is just extra speed).

    Your mileage may vary 🙂

    Thanks for this very,very useful script, i couldn’t have made it myself!

  3. Is it possible to add an exclusion option on the disk images? I have backup images named “vmname-BACKUP01” etc which are located on NAS mounts (these are normally used for backup routines inside the guest OS) Backing these up along with the cold host isn’t needed. Is adding an exclusion for disk images containing BACKUP in their names possible?
    Thanks for an excellent script, way above my ability for sure.

  4. I realize this has been here for about five years but still would like to say thank you. I am using your script to backup two KVM servers to each other.

    This is a home setup for learning. Each host has about five or six guests running that I wanted to backup in case one or the other dies.

    In setting this up I had to revisit setting up NFS on both sides (server and client)
    I had to learn about ACPI (because my Ubuntu desktop was not shutting down at first, hint edit the /etc/acpi/events/powerbtn script – change the action to ‘action=/sbin/poweroff’)
    I had to learn about setting up a CRON Job and I had to learn more about permissions.

    All of this is pretty simple stuff but at first linux can be ‘interesting’ shall we say 🙂

    Again – thanks

  5. Another thing I have noticed is always that for many people, below-average credit is the results of circumstances outside of their control. Such as they may be actually saddled by having an illness and because of this they have high bills for collections. It can be due to a job loss or maybe the inability to do the job. Sometimes separation and divorce can truly send the financial situation in the undesired direction. Thanks sharing your notions on this weblog.

Leave a Reply

Your email address will not be published.