eslam-gomaa / virt-backup

Fully backup your KVM Virtual Machines

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

virt-backup ๐Ÿš€

Fully backup & restore your KVM Virtual Machines


virt-backup Features:

  • Ability to backup all the VM's disks or only system disk
  • compress the backup directly in a ZIP file, which decreases backup size
  • validate the restore process with the checksum of the original VM (taken on backup)
  • Ability to backup and restore all the VM's snapshot - [ Internal Snapshots ]

Install

At least Ruby Version 2.4 is needed, Here is how to install Ruby 2.5 on different distributions

Just install the gems used and you're good to go (Assuming that you have KVM installed)

gem install rubysl-tempfile
gem install rubysl-optparse
gem install open4
gem uninstall zip # Need to be removed if installed
gem install rubyzip
git clone https://github.com/eslam-gomaa/virt-backup.git
cd virt-backup
ruby virt-backup.rb --help
Prefered way to install

cd /var
git clone https://github.com/eslam-gomaa/virt-backup.git
alias virt-backup="ruby /var/virt-backup/virt-backup.rb"
# Put it in ~/.bashrc for persistence.
# echo 'alias virt-backup="ruby /var/virt-backup/virt-backup.rb"' >> ~/.bashrc
virt-backup -h

Test

๐Ÿ“Œ The tests are done by an automated Jenkins pipeline

Distro Test Result
Ubuntu 16.04
Ubuntu 18.04
Ubuntu 20.04
CentOS 7
CentOS 8
fedora-34
Debian 10
Debian 11

Note for Debian 11 Users (Consider that issue)

It works fine on my tests, but if you'll use the --with-snapshots or -s option make sure to test to restore your VM



Examples

  • Backup a VM with all its disks

Note To backup the Snapshots of the VM, use the option --with-snapshots

ruby virt-backup.rb --backup \
 --with-snapshots \
 --original-vm kubernetes-master \
 --save-dir /var/lib/libvirt/images/backup/
  • Backup a VM with only system disk
ruby virt-backup.rb --backup \
 --with-snapshots \
 --original-vm kubernetes-master \
 --system-disk-only \
 --save-dir /var/lib/libvirt/images/backup/
  • Restore a VM from backup

Note no options needed when restoring a backup with only system disk, the script detects and handles that.

Note To restore the Snapshots of the VM, use the option --with-snapshots

ruby virt-backup.rb --restore \
 --with-snapshots \
 --backup-file /var/lib/libvirt/images/backup/kubernetes-master.zip \
 --restore-dir /var/lib/libvirt/images/backup/
  • Backup without compression

supported (--compression) values are: default, none, best Default: best

ruby virt-backup.rb --backup \
 --with-snapshots \
 --original-vm kubernetes-master \
 --system-disk-only \
 --compression none
 --save-dir /var/lib/libvirt/images/backup/

Example screenshots

Assuming you have installed this way

๐Ÿšฉ Backup

mkdir /var/lib/libvirt/images/backup/

virt-backup --backup \
  --with-snapshots \
  --original-vm cirros \
  --save-dir /var/lib/libvirt/images/backup/

image

๐Ÿšฉ Restore

virt-backup --restore \
  --with-snapshots \
  --backup-file /var/lib/libvirt/images/backup/cirros.zip \
  --restore-dir /var/lib/libvirt/images/

image



Updates & Fixes

  • If md5 mismatch found, print where is the difference โœ”๏ธ
  • Pause the VM before collecting the checksum โœ”๏ธ
  • Fix: Error if snapshot name has a space โœ”๏ธ
  • Fix: Restore snapshot --> stable now โœ”๏ธ
  • Update: Zip 64 bit is added (the support to backup & restore large disk files) โœ”๏ธ
  • Add command-line control to the compression level โœ”๏ธ


Note
The script does the job perfectly, However to add more features easily the code needs to be refactored,

And since that would take a few weeks, at least 3 features/enhancements should be requested first.



Thank you

Eslam Gomaa

.

About

Fully backup your KVM Virtual Machines


Languages

Language:Ruby 89.9%Language:Shell 10.1%