Backup and Restore VirtualBox machine with LVM Snapshots and dd

#
# Install host using lvm partitions system (my case: linuxmint 17.3 xfce)
#
# From liveCD
# create partition 3 partitons with fdisk
# boot, swap, root

sudo su
fdisk -l
fdisk /dev/sda
Command (m for help) 
# boot
n
p
1
+500M
a
1
# swap and swap partion type
n
p
2
+5G
t
82
# root lvm partition type
n
3
p
50G
t
8e
# save setup
w

#
# Install virtualbox
# https://www.virtualbox.org/wiki/Linux_Downloads

deb http://download.virtualbox.org/virtualbox/debian vivid contrib
wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | sudo apt-key add -
sudo apt-get update
sudo apt-get install virtualbox-5.0
sudo apt-get install dkms

#
# Open virtualBox and change default virtual machines directory

sudo mkdir /home/vboxes
VBoxManage setproperty machinefolder /home/vboxes
VBoxManage list systemproperties

#
# Create logical volume to store a new virtual machine

sudo lvcreate -L10G -n ubuntu14 mint-vg
mkdir /home/vboxes/ubuntu14
sudo mount /dev/mint-vg/ubuntu14 /home/vboxes/ubuntu14

#
# Create a VM
# https://leemendelowitz.github.io/blog/ubuntu-server-virtualbox.html

vboxmanage createvm --name ubuntu14 --ostype Ubuntu_64 --register

# add more some ...

vboxmanage startvm ubuntu14

#
# Do backup

sudo lvcreate -L10G -s -n ubuntu14_snap /dev/mint-vg/ubuntu14
sudo dd if=/dev/mint-vg/ubuntu14_snap bs=64k | gzip -c > ubuntu14.img.gz
sudo lvremove -f /dev/mint-vg/ubuntu14_snap

#
# Restore

sudo lvcreate -L10G -n ubuntu14_restored mint-vg
sudo gunzip -c ubuntu14.img.gz | dd of=/dev/mint-vg/ubuntu14_restored bs=64k
mkdir /home/vboxes/ubuntu14_restored
sudo mount /dev/mint-vg/ubuntu14_restored /home/vboxes/ubuntu14_restored

#
# Change machine and disk uuid
# get current uuid

vboxmanage internalcommands dumphdinfo /home/vboxes/ubuntu14-disk.vdi

# set new uuid

vboxmanage internalcommands sethduuid /home/vboxes/ubuntu14_restored/ubuntu14-disk.vdi

# find and replace machine uuid and disk uuid by previous result

perl -i -pe "s/uuid="(.*?)"/uuid="{$newuuid}"/g" ${machinefolder}/${machinename}/${vboxfilename}.vbox

#
# Register, Check and Start VM

vboxmanage registervm /home/vboxes/ubuntu14_restored/ubuntu14.vbox
vboxmanage list vms
vboxmanage startvm <uuid>

# backup.sh

#!/bin/sh
#
# 2016-02-23 fribeiro
# 
# usecase: sudo ./backup.sh machinename
#

data=`date +%Y-%m-%d`
machinefolder="/home/fribeiro/vboxes"
machinename=$1
vg="mint-vg"

if [ -z "$machinename" ]; then
  echo "insert machine name"
  exit
fi

if [ ! -d ${machinefolder}/${machinename} ]; then
  echo "machine ${machinename}  doesn't exist"
  exit
fi


lvsize=$(df -H /dev/${vg}/${machinename} | grep -vE '^Filesystem' | awk '{ print $2 }')

lvcreate -L${lvsize}G -s -n ${machinename}_snap /dev/${vg}/${machinename}
dd if=/dev/${vg}/${machinename}_snap bs=64k | gzip -c > ${machinename}-${data}.img.gz
lvremove -f /dev/${vg}/${machinename}_snap

#
# restore.sh

#!/bin/sh
#
# 2016-02-23 by fribeiro
#
# usecase: sudo ./restore.sh fluxbuntu-2016-02-22.img.gz restoremachinename 10G
# 
# register machine: vboxmanage registervm /home/fribeiro/vboxes/restoremachinename
# 

data=`date +%Y-%m-%d`
machinefolder="/home/fribeiro/vboxes"
backupfile=$1
backupfolder="/home/fribeiro/vboxes"
vg="mint-vg"
machinename=$2
lvsize=$3

if [ -z "$backupfile" ]; then
  echo "insert backup file"
  exit
fi

if [ -z "$machinename" ]; then
  echo "insert machine name to build restore"
  exit
fi

if [ -z "$lvsize" ]; then
  echo "insert size in G of machine"
  exit
fi


if [ -d ${machinefolder}/${machinename} ]; then
  echo "machine ${machinefolder}/${machinename} already exists"
  exit
fi

if [ ! -f ${backupfolder}/${backupfile} ]; then
  echo "backupfile ${machinebackup}/${backupfile}  doesn't exist"
  exit
fi

lvcreate -L${lvsize}G -n ${machinename} ${vg}
gunzip -c ${backupfolder}/${backupfile} | dd of=/dev/${vg}/${machinename} bs=64k
mkdir ${machinefolder}/${machinename}
mount /dev/${vg}/${machinename} ${machinefolder}/${machinename}
vboxfilename=$(basename -s .vbox ${machinefolder}/${machinename}/*.vbox)
newuuid=$(vboxmanage internalcommands sethduuid ${machinefolder}/${machinename}/${vboxfilename}-disk.vmdk | awk ' { print $4 } ')
echo $newuuid
perl -i -pe "s/uuid=\"(.*?)\"/uuid=\"{$newuuid}\"/g" ${machinefolder}/${machinename}/${vboxfilename}.vbox

#
# References
http://www.tecmint.com/extend-and-reduce-lvms-in-linux/
http://www.linuxwizard.org/linux/creating-backups-clones-running-virtual...
# https://leemendelowitz.github.io/blog/ubuntu-server-virtualbox.html
# http://nakkaya.com/2012/08/30/create-manage-virtualBox-vms-from-the-comm...