sabato 30 giugno 2012

Oracle VM Backup Running Guests VMs


In this post I provide a way to perform backup of running VMs for disaster recovery purpouse. Basically this procedure transfers VM Disks from production Repository to a different backup Repository placed on another SAN storage.
Please bear in mind that this backup does NOT guarantee data consistency so if your VMs runs apps such as DBs consider a backup strategy using utility tools such as rman.

In case you need to recover a VM you need first to delete the VM and corresponding disks from VM Manager then import backed up disk image and recreate the VM using imported disk image as virtual disk.



I use a dedicated Oracle Linux 6 U2 server to perform backups; it's basically a VM using a virtual disk mapped to a Repository created on an ISCSI separated storage.

Let's start step-by-step:

First I've created a dedicated Backup VM in VM Manager.

NOTE: Consider carefully the VirtualDisk size since in this machine you will store backup VirtualDisks. For example if you need to backup 5 VMs each one with a 20GB hard disk you need to have AT LEAST 20*5=100GB available disk space after system installation.

Second step is to install OS. I use Oracle Linux 6 U2 server.

Please refer to this guide to install oracle Linux: Oracle Linux installation & configuration guide

When OS is installed I created a dedicated user to perform backups:

[root@orcl ~]$ adduser VMBackup

Then I need to install two additional packages to allow backup script to be correctly executed:

[root@orcl ~]$ mkdir /mnt/cdrom
[root@orcl ~]$ mount /dev/cdrom /mnt/cdrom
[root@orcl ~]$ cd /mnt/cdrom/Packages
[root@orcl Packages]# rpm -Uvh tcl-8.5.7-6.el6.x86_64.rpm
[root@orcl Packages]# rpm -Uvh expect-5.44.1.15-2.el6.x86_64.rpm

Switch to VMBackup user:

[root@orcl ~]$ su - VMBackup

and create VMBackup.sh script

[VMBackup@orcl ~]$ nano /home/VMBackup/VMBackup.sh

NOTE: This code is inspired by "Automate SCP command using Shell Script" so credits for part of this script goes to Santhosh Kumar T.

Copy/Paste this code.

I know it's far from an elegant (and clean) code but it does the job so...

#!/bin/bash

##EDIT THIS VALUE
#Insert IP Address of your VM Server
YOUR_SERVER_IP_ADDRESS=10.0.0.12

##EDIT THIS VALUE
#Insert root password of your VM Server
YOUR_SERVER_PASSWORD=password

if [ $# -eq 0 ] ; then
    echo 'Usage: VMBackup [backup | restore] [Repository ID] [VM VirtualDisk ID]'
    exit 0
fi

case "$1" in
        backup)
           
            cat > /home/VMBackup/backup.sh <<EOF
#!/usr/bin/expect -f

spawn scp "root@$YOUR_SERVER_IP_ADDRESS:/OVS/Repositories/$2/VirtualDisks/$3.img" /home/VMBackup/$3.img

expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "$YOUR_SERVER_PASSWORD\r"
}
}
interact
EOF

        chmod a+x backup.sh
       
        /home/VMBackup/backup.sh
        rm /home/VMBackup/backup.sh
       
        ;;

        restore)

            cat > /home/VMBackup/restore.sh <<EOF
#!/usr/bin/expect -f

spawn scp /home/VMBackup/$3.img "root@$YOUR_SERVER_IP_ADDRESS:/OVS/Repositories/$2/VirtualDisks/$3.img"

expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "$YOUR_SERVER_PASSWORD\r"
}
}
interact
EOF

        chmod a+x restore.sh
       
        /home/VMBackup/restore.sh
        rm /home/VMBackup/restore.sh
       
        ;;

        *) echo 'Usage: VMBackup [backup | restore] [Repository ID] [VM VirtualDisk ID]' ;;
esac

Then...

[VMBackup@orcl ~]$ chmod a+x /home/VMBackup/VMBackup.sh

and this is the command you use to perform a backup:

[VMBackup@orcl ~]$ /home/VMBackup/VMBackup.sh backup REPOSITORY_ID VIRTUAL_DISK_ID

or a restore:

[VMBackup@orcl ~]$ /home/VMBackup/VMBackup.sh restore REPOSITORY_ID VIRTUAL_DISK_ID

For example if your VirtualDisk location is:

/OVS/Repositories/0004fb0000030000ad1f4d7286b3abcd/VirtualDisks/0004fb00001200005ab2a78b4b71abcd.img

REPOSITORY_ID=0004fb0000030000ad1f4d7286b3abcd

VIRTUAL_DISK_ID=0004fb00001200005ab2a78b4b71abcd


the command to backup VirtualDisk 0004fb00001200005ab2a78b4b71abcd is

[VMBackup@orcl ~]$ /home/VMBackup/VMBackup.sh backup 0004fb0000030000ad1f4d7286b3abcd 0004fb00001200005ab2a78b4b71abcd


Reasonably you'll need to add this script to crontab so it will perform automated backups at certain time.

That's all!!

Nessun commento:

Posta un commento