KVM (Kernel-based Virtual Machine) is the leading open source complete virtualization solution and it supports all major operating systems including Linux and Windows. KVM uses LVM-based guests so we can take KVM virtual machine backup using the “dd” utility.  To generate the  backup of a virtual machine we have to take the entire backup of the virtual disk attached to it. “DD” is a low level utility used for this purpose, it is low level byte to byte copy tool.

As KVM using LVM-based guests the LVM logical volume will be mapped as “/dev/volume group/virtual machine” and we will get the available logical volumes using “lvs” command.

In the below example, each virtual machine is stored in volume group “vg002” root@localhost ~]# lvs vg000

LV         VG    Attr       LSize   Pool Origin Data%  Move Log Cpy%Sync Convert

kvm101_img vg002 -wi-ao—- 20.00g

kvm102_img vg002 -wi-ao—-  32.00g

kvm103_img vg002 -wi-a—– 70.00g

The virtual disk of VPS kvm101 is available at /dev/vg002/kvm101_img and we have to take the backup of the corresponding virtual disk.

To take the backup of the Logical volume kvm101_img run command:

 ddif=/dev/vg002/kvm101_img of=<backup file> bs=512K

This could create the copy of the virtual disk having the same size of original one.

“if” represents the source disk and “of” represents the backup file of virtual disk;

bsis the block size and it depends on the  hardware and software on your system. Hereihave used 512K.

To restore the virtual machine:

You shouldn’t restore the backup when the VM is running. You should shut down the VM before proceeding with restoration.

To restore the Virtual machine using  the backup we have created, just run the  below command:

Here also we have used the DD utility, the only change is in the source and destination port.

ddif=<backup file name> of=/dev/volume group/<Destination logical volume> bs=512K

The above approach is fast, but takes a lot of space because the size of the backup file is sames as the source lvm device. So make sure you should have enough free space before proceeding.

To generate a backup in compressed format, run command:

If you want to save the space, then use below command to take the backup. This will generate backup in compressed format.

VMscompress from 10-80%, this will depend on the data in the virtual disk.

ddif=/dev/vg002/kvm101_img bs=512K |gzip-9 > <backup file name.gz>

To generate a backup to remote server, run command:

If you are using a remote backup server, then use ssh command to store the backupinremote server.

dd if=/dev/vg002/kvm101_img | gzip -c | ssh user@remoteserver “dd of=/remotel_location/backup.gz”

or

dd if=/dev/vg002/kvm101_img | ssh user@remote.machine “gzip -c | dd of=/remote_location/backup.gz”

The first command compress the backup on source sever and second command on  the destination server. If your source machine has high CPU usage and you don’t want to overload, then it is better to compress the backup on destination serve.(second option)

To restore the virtual machine from compressed backup,run command:

unzip<backup file.gz> – |ddof=/dev/mapper/<Destination device

or

dd if=/<backup_file_name.gz> | gzip -c -d | dd of=/dev/volume group/<Destination logical_volume>

Backup using the LVMsanpshotmethod

The other backup  methods will work well if the virtual machine is shut down. If any update being made during the backup process might be invalid and restore an unstable VM. To overcome we can use LVM snapshot method. In this method we will use snapshots of entire VM. Snapshots is a copy of the virtual machine at the moment of taking it. So we can use this method to backup a live VM, by simply backing up the snapshot rather than the main disk.We have to adjust the size of the snapshot to be sure that it won’t overflow during backup. So before proceeding, we should have an idea about the snapshots size otherwise it overflows and discarded.

The first step is creating the snapshot using the command:

lvcreate -s -n /dev/volume_group_ name/<snapsho_name> -L <size_of_snapshot> /dev/volume_group_ name/logical_volume_name

This will create snapshots of same size and name mentioned in the command. You can verify this using “lvs” command.

eg:

I have created  a snapshot of logical volume kvm101_img usingcommand:

lvcreate -s -n /dev/vg002/kvm101/kvm101_snap -L 10G /dev/vg002/kvm101_img

You can verify  the snapshot with lvs command:

LV         VG    Attr       LSize   Pool Origin Data%  Move Log Cpy%Sync Convert

  kvm101_img vg002 -wi-ao—- 20.00g

  kvm102_img vg002 -wi-ao—-  32.00g

  kvm103_img vg002 -wi-a—– 70.00g

  kvm101_snap  vg002 -wi-a—– 10.00g   kvm101_img

Here a new snap short logical volume “kvm101_snap” has been  created having origins kvm101_img.The performance implications should suggest that LVM snapshots should not be used as a backup strategy. A good approach is to temporarily create a snapshot of a volume, make a backup copy using the snapshot, and discard the snapshot upon completion of the backup. This way, if the backup is quick and there aren’t many changes being made to the original volume in the meantime, the performance penalty of the snapshot can be very small for a short time.

Once you have created a snapshot you can generate the backup using following command.

ddif=/dev/vg002/kvm101_snap of=/<backup_location>/vm_backup.dd.

kvm101_snap: snapshot_name

To generate a backup in compressed format,run command:

dd if=/dev/vg002/kvm101_snap | gzip -c | dd of=/<backup_location>/vm_backup..gz

To generate a backup to remote server,run command:

If you are using a remote backup server, then use ssh command to store the backupon remote server.

dd if=/dev/vg002/kvm101_snap | gzip -c | ssh user@remote.machine “dd of=/<backup_location>/vm_backup.gz

or

dd if=/dev/vg002/kvm101_snap | ssh user@remote.machine “gzip -c | dd of=/<backup_location>/vm_backup.gz”

To restore backup.

ddif=/<backup_location>/vm_backup.ddof=/dev/vg_name/destination_logical_volume

To restore the virtual machine from compressed backup,run command:

dd if=/<backup_location>/vm_backup.gz | gzip -c -d | dd of=/dev/vg_name/destination_logical_volume

Once the backup process is  completed, delete the snapshot using the command.

lvremove /dev/vg002/kvm101_snap

You shouldn’t restore the backup when the VM is running. But you can snapshot and take backup of running VMs in this method.

This method works both with Xen and KVM based VMs.