#!/bin/sh
ScriptName="VMware VM backup for LINUX"
ScriptVersion="v0.2"
ScriptDate="02/03/2008"
ScriptAuthor="jmarior - www.virtualizados.com"
ScriptLicense="Creative Commons Attribution-Non-Commercial 2.0 Generic at http://creativecommons.org/licenses/by-nc/2.0/deed.es"

# Improvements for next versions
# - convert backup proccess in a function
# - encapsulate echo messages in a function
# - pack and compress the last backup
# - maintain a ring of packed backups
# - chrono start time - Init time
# - log to file
# - copy result into another computer
# - report by mail

# start user config
BackupDir="/disk-bkp/automatic-backups"
VMdir="/home/user/VirtualMachines/linux-server"
VMfile="linux-server.vmx"
# end user config

# general config
VMwareCmdPath="/usr/bin/vmware-cmd"

# resource strings
rsStart="Inicio"
rsStateBeforeStop="Estado antes de detener..."
rsStoppingVM="Deteniendo VM..."
rsStateAfterStop="Estado tras detener:"
rsStartingVM="Iniciando VM"
rsStateAfterStart="Estado tras iniciar:"

#compute source and destination
VMpath="$VMdir/$VMfile"
Today=$(date +'%Y%m%d%H%M')
BackupDestinationDir="$BackupDir/$Today"

echo $ScriptName "|" $ScriptVersion "|" $ScriptDate "|" $ScriptAuthor
echo $ScriptLicense
echo $VMfile
 
# stops the VM (requires VMWareTools installed)
echo $rsStart $(date +'%Y-%m-%d %H:%M')
echo $rsStateBeforeStop; $VMwareCmdPath $VMpath getstate
echo $rsStoppingVM; $VMwareCmdPath $VMpath stop
echo $rsStateAfterStop; $VMwareCmdPath $VMpath getstate

# create dest dir
mkdir "$BackupDestinationDir"

# cp the entire dir of the VM
echo Copiando...
cp -r "$VMdir" "$BackupDestinationDir"

# start VM
$VMwareCmdPath $VMpath start
echo $rsStateAfterStart; $VMwareCmdPath $VMpath getstate

# compress the backuped VM

# maintain a ring

echo $rsEnd $(date +'%Y-%m-%d %H:%M')
