Backing up virtual machines in KVM.

In this article, we will consider several options for backing up virtual machines on a KVM hypervisor, as well as recovery scenarios from backups. I would like to note right away that there are no convenient tools for backup under KVM as such, and each administrator uses his own options, scripts and crutches. There are 2 scenarios for backing up a VM in KVM: with stopping the VM (the easiest, but rarely used) and without stopping the virtual machine.

First of all, I would like to note that the features of backup in your KVM are highly dependent on the type of virtual disks used: LVM, RAW (IMG) or qcow2. On my KVM servers, virtual machine disks are formatted as qcow2. I believe that this format outperforms the rest for two reasons:

  • The size of the disk is always sized according to the space occupied inside the machine, it is enough just to increase and decrease it;
  • This format supports snapshots.

Therefore, virtual disks in qcow2 format are the easiest for me to back up.

Creating backups in KVM with stopping the virtual machine.

If your project allows for a temporary shutdown of the virtual machine, then you can use the easiest way to create a backup. In order for you to always have up-to-date backups at hand, you need to copy the disk file and the configuration file of the virtual machine itself (in case the configuration changes).

Using virsh, we will display a list of virtual machines in KVM:

# virsh list

Id Name State ---------------------------------------------------- 1 test-centos running 4 generic running

I also have a directory where I plan to save backup copies of virtual machines:

# df -h | grep backup

/dev/sda1 1.8T 77M 1.7T 1% /backup
The configuration file of the virtual machine can be copied with the following command:

# virsh dumpxml VM > /backup/VM.xml
Where VM is the name of your virtual machine.

Now, to back up the virtual machine's disk, we need to stop the virtual machine and copy the disk image to the target directory:

# virsh shutdown test-centos
# cp /vz/disk/test-centos.img /backup/

After the disk is completely copied, you need to start the virtual machine:

# virsh start test-centos


For each virtual machine, you can create a separate directory and automate the process of creating copies by adding script commands and setting up jobs in cron. You can use the scripts to suit your needs by adapting them for KVM.

Or you can use a small and simple script:

 #!/bin/bash
data=`date +%Y-%m-%d`
backup_dir=/backup
vm=`virsh list | grep . | awk '{print $2}'| sed 1,2d | tr -s '\n' ' '`
for activevm in $vm
do
mkdir -p $backup_dir/$activevm
# Backing up the XML configuration for the virtual machine
virsh dumpxml $activevm > $backup_dir/$activevm/$activevm-$data.xml
# Virtual machine disk address
disk_path=`virsh domblklist $activevm | grep vd | awk '{print $2}'`
# Stopping a working machine
virsh shutdown $activevm
sleep 2
for path in $disk_path
do
# Removing the filename from the path
filename=`basename $path`
# Create a disk backup
gzip -c $path > $backup_dir/$activevm/$filename-$data.gz
sleep 2
virsh start $activevm
sleep 2
done
done
/usr/bin/find /backup/ -type f -mtime +7 -exec rm -rf {} \;
Add this script to your cron and run it as often as you need. The script automatically stops the virtual machine, backs up the disk file and configuration file, and then automatically starts the virtual machine.

As a result of the script execution, directories will be created for each virtual machine and the disk file and configuration dump will be placed in them, as well as old backups will be deleted (specify the number of days yourself):


# ls -la test-centos

-rw-r--r-- 1 root root 5440 Feb 19 12:17 test-centos-2020-02-19.xml
-rw-r--r-- 1 root root 38609784 Feb 19 12:18 test-centos.img.gz

KVM: backup without stopping the virtual machine.

Naturally, in most cases, administrators want to use the option of "live" backup of KVM virtual machines without stopping. It is a little more complicated than the first option and requires additional steps. This option uses the creation of a snapshot and its subsequent merging with the disk file of the virtual machine. At the very beginning of the article, I wrote that I use the qcow2 disk format and this is exactly what allows you to create a live backup. In order for you to correctly create a copy of the virtual machine, a Channel Device with the name org.qemu.guest_agent.0 must be added to the VM (can be added via the configuration file or virt-manager).

Advice. If you do not set the configure agent for guest VMs, an error will appear when creating a snapshot:

error: argument unsupported: QEMU guest agent is not configured

Don't forget to add the following block to the VM configuration XML file:<channel type='unix'> <target type='virtio' name='org.qemu.guest_agent.0'/> </channel>

It is added to the “Device” section, after which you need to save the configuration and reboot the machine.

Then, in the guest OS, you need to install the qemu-guest-agent package (install it via yum / dnf):

# yum install qemu-guest-agent -y

To create snapshots of Windows VMs, you need to install virtio-win.

To create a snapshot of a VM, use the following command:
# virsh snapshot-create-as --domain VM snapshot --disk-only --atomic --quiesce –no-metadata

Where VM is the name of the virtual machine. Next, you need to create a backup of the disk file:

# gzip -c VM > /backup/VM/VM.gz

After the disk of the virtual machine is copied, you need to merge it with the snapshot:

# virsh blockcommit VM vda --active --verbose --pivot
Where vda is the output of the command:
# virsh domblklist VM | grep vd | awk '{print $1}'
I've redone the previous backup script to stop virtual machines. Now the backup is started by cron, creating snapshots and merging it with the virtual machine disk: This script will perform the backup like the first option, only without stopping the virtual machine (works correctly, tested on virtual servers with postgresql, mariadb and nginx with php-fpm). The end result will be exactly the same, you will have a backup of the configuration file and the disk file.
#!/bin/bash
data=`date +%Y-%m-%d`
backup_dir=/backup
vm=`virsh list | grep . | awk '{print $2}'| sed 1,2d | grep -vf /root/bin/exclude | tr -s '\n' ' '`
for activevm in $vm
do
mkdir -p $backup_dir/$activevm
# Backing up the XML configuration for the virtual machine
virsh dumpxml $activevm > $backup_dir/$activevm/$activevm-$data.xml
# List of virtual machine disks
disk_list=`virsh domblklist $activevm | grep vd | awk '{print $1}'`
# Virtual machine disk address
disk_path=`virsh domblklist $activevm | grep vd | awk '{print $2}'`
# Create a disk snapshot
virsh snapshot-create-as --domain $activevm snapshot --disk-only --atomic --quiesce --no-metadata
sleep 3
for path in $disk_path
do
# Removing the filename from the path
filename=`basename $path`
# Create a disk backup
gzip -c $path > $backup_dir/$activevm/$filename-$data.gz
sleep 3
done
for disk in $disk_list
do
# Determine the path to the snapshot
snapshot=`virsh domblklist $activevm | grep $disk | awk '{print $2}'`
# Merge snapshot with disk
virsh blockcommit $activevm $disk --active --verbose --pivot
sleep 2
# Delete Snapshot
rm -rf $snapshot
done
done
/usr/bin/find /backup/ -type f -mtime +7 -exec rm -rf {} \;

To store backups, you can use remote servers (you can copy data via Rsync) or storage. This way you can save disk space on your server.

Отправить комментарий

Добавлять новые комментарии запрещено.*

Новые Старые