Versioned Nightly 100% Virtual Machine Backup (VMWare Workstation)

vmware_logo3If you want to Backup your virtual machine every night and need to keep more than one version the following batch / script is what you’re looking for.

 

 

@echo off

:: Start Variables

set numberToKeep=5
set backupCmd=xcopy /s /c /d /e /h /i /r /y

set vmWareWorkstationCmd="C:\Program Files\VMware\VMware Workstation\vmrun"

set virtualMachineFolder=C:\VM\TenStepsAhead_Server\
set virtualMachineVmxFile="C:\VM\TenStepsAhead_Server\TenStepsAhead_Server.vmx"
set backupTarget=D:\Backup\

:: End Variables

:: Actual Script Starts Here!
echo+
echo STARTING BACKUP
echo %date% %time%

:: 1. Delete older backup set(s) beyond the numberToKeep
for /F "tokens=* skip=%numberToKeep%" %%I In ('dir "%backupTarget%" /AD /B /O-D /TW') do (
echo+
echo DELETING OLD BACKUP SET %backupTarget%%%~I
rd /s /q "%backupTarget%%%~I"
)

:: 2. Suspend Virtual Machine
echo+
echo Suspend Virtual Machine
%vmWareWorkstationCmd% suspend %virtualMachineVmxFile%

:: 3. Create new backup set
set bkuphour=%time:~0,2%
if "%bkuphour:~0,1%"==" " set bkuphour=0%time:~1,1%
set bkupfldr=%date:~10,4%_%date:~4,2%_%date:~7,2%_%bkuphour%_%time:~3,2%

echo+
echo CREATING FOLDER %backupTarget%%bkupfldr%\
if not exist "%backupTarget%%bkupfldr%\" mkdir "%backupTarget%%bkupfldr%\"

echo+
echo BACKING UP FILES...
%backupCmd% "%virtualMachineFolder%*.*" "%backupTarget%%bkupfldr%\"

:: 4. Start Virtual Machine
echo+
echo Start Virtual Machine
%vmWareWorkstationCmd% start %virtualMachineVmxFile%
echo+
echo BACKUP COMPLETED!
echo %date% %time%

Thanks to C. Peter Chen