Many DBA’s and developers are now routinely using Oracle Virtual Box virtual machines to simplify database research and even development. I am one of those people. I also use VMware’s Workstation too. Today I’m sharing my Windows FIX_VDI.BAT script for compacting and zipping up my Virtual Box VM’s. For example when I’ve got a VM exactly the way I like it and want to create a version to save, I run this script. While some steps like compacting the VM can be done from the Virtual Box GUI, I just want to do it all at once – with minimal interaction from me. Oracle’s Virtual Box provides some very useful command line capabilities – but most users don’t know they have them. So I wrote and am sharing this script. Let’s assume that you have your VM’s in the C:\VM directory and now want to compact and zip up as a RAR file the LINUX_ORACLE12 VM, then you simply issue the following command:

 

                C:\> fix_vdi.bat  c:\vm linux_oracle12  rar

 

@REM ===========================================

@REM SCRIPT:  FIX_VDI.BAT

@REM AUTHOR:  BERT SCALZO

@REM USAGE:   %0  VM-DIR VM-NAME  {7z rar both}

@REM ===========================================

 

:PARMCOUNT

@set argC=0

@for %%x in (%*) do @Set /A argC+=1

@if %argC% GTR 3 @goto USAGE

@goto PARM1

 

:PARM1

@if "%1"=="" @goto USAGE

@if EXIST %1        @goto PARM2

@goto USAGE

 

:PARM2

@if "%2"=="" @goto USAGE

@if EXIST %1\%2     @goto PARM3

@goto USAGE

 

:PARM3

@if "%3"=="" @goto CONTINUE

@if "%3"=="7z" @goto CONTINUE

@if "%3"=="rar" @goto CONTINUE

@if "%3"=="both" @goto CONTINUE

@goto USAGE

 

:USAGE

@echo.

@echo ====================================================

@echo USAGE:  %0  VM-DIR VM-NAME  {7z rar both}

@echo.

@echo ERROR:  Mistake made in parameter list

@echo ====================================================

@goto RETURN

 

:CONTINUE

@set vm_dir=%1

@set vm_name=%2

@set zipping=%3

@FOR /F %%G IN ('DIR %1\%2\*.vdi /B') DO @SET vm_file=%%G

@if "vm_file"=="" @goto USAGE

 

@echo.

@echo ##################################################

@echo #### %0:

@echo ####     VM Dir:   %vm_dir%

@echo ####     VM Name:  %vm_name%

@echo ####     VM VMDK:  %vm_file%

@echo ###      Zipping:  %zipping%

@echo ##################################################

 

REM @goto RETURN

REM @goto SKIP

"C:\Program Files\Oracle\VirtualBox\VBoxManage" modifyhd --compact %vm_dir%\%vm_name%\%vm_file%

:SKIP

 

@if EXIST %vm_dir%\%vm_name%\Logs @del /f /q /s %vm_dir%\%vm_name%\Logs\*.*

@if EXIST %vm_dir%\%vm_name%\Logs @rmdir /q /s %vm_dir%\%vm_name%\Logs

 

@if EXIST %vm_dir%\%vm_name%\*.* @attrib -a %vm_dir%\%vm_name%\*.*

 

@echo ##################################################

@echo.

 

@SET isodate=%date:~10,4%-%date:~4,2%-%date:~7,2%

 

@if "%zipping%"=="7z"   @goto ZIP7Z

@if "%zipping%"=="rar"  @goto ZIPRAR

@if "%zipping%"=="both" @goto ZIPRAR

@goto RETURN

 

:ZIPRAR

@if EXIST %vm_dir%\%vm_name%-%isodate%.rar @del /f /q %vm_dir%\%vm_name%-%isodate%.rar

@"C:\Program Files\WinRAR\rar"   a -ac -ai -cl -ed -m5 -mt4 -r -s -t           %vm_dir%\%vm_name%-%isodate%.rar %vm_dir%\%vm_name%\

@if "%zipping"=="both" @goto ZIP7Z

@goto RETURN

 

:ZIP7Z

@if EXIST %vm_dir%\%vm_name%-%isodate%.7z  @del /f /q %vm_dir%\%vm_name%-%isodate%.7z

@"C:\Program Files\7-Zip\7z.exe" a -t7z -r -mx=9 -ms=on -mmt=4 -m1=LZMA -slp %vm_dir%\%vm_name%-%isodate%.7z %vm_dir%\%vm_name%\

@goto RETURN

 

:RETURN