Backing up a vmware server can be difficult. We have implemented our solution, and it works for us. Please feel free to take and use any of the ideas and code found in this section.
I use DOS batch programming to accomplish this task.
Im my DOS batch file, I have a parameter that is passed in as: %1. The parameter will be the directory name that houses the vmware server I am going to back up. Also, in DOS batch programming, a line that starts with :: is a comment line. A line that starts with : is a label line (ie. a line that you can use GOTO with)
I append the passed in directory name to the directory that houses all of my VMWare servers (Y:\VMIMAGES)
SET VMIMAGEDIR=Y:\VMIMAGES\%1
I also set a variable equal to the full path and filename of the vmare-cmd executable which I will be using to pause and restart the servers.
set VMCMD=C:\Program Files (x86)\VMware\VMware Server\vmware-cmd
I also set up a few other variables that I will use for copying and backing up the vmware images.
set VMTEMPDIR=Y:\%1TEMPset VMBACKDIR=z:\vmbackup\%1
If the passed VMImage directory name does not exist, I bomb out of the program with an error:
:: if VMImage does not exist print I error and exitIF not exist %VMIMAGEDIR% ECHO. "%1" does not existIF not exist %VMIMAGEDIR% GOTO end
The last line of my batch file has an :end label. The above code will skip to that line if %VMIMAGEDIR% does not exist.
Next, I get a datestamp that I will use for naming the files, format it appropriately, and assign it to a variable called "timestr" that I will use for naming the backup file.
for /f "tokens=1,2" %%u in ('date /t') do set d=%%vfor /f "tokens=1" %%u in ('time /t') do set t=%%uif "%t:~1,1%"==":" set t=0%t%set timestr=%d:~6,4%-%d:~0,2%-%d:~3,2%
Now that everything has been set up, our first order of business is to determine whether the VMWare server is actually running. This code assumes that the network name of the VMWare server is the same as directory name that was passed in. I use a ping command, check the results of that ping command by using the %ERRORLEVEL% variable that is automatically populated whenever you attempt to run a command.
ping %1if %ERRORLEVEL%==0 set VMSTARTUP=1if not %ERRORLEVEL%==0 set VMSTARTUP=0:: If VM is up, For each VMware Image in directory, suspend itif %VMSTARTUP%==1 for %%F in (Y:\VMIMAGES\%1\*.vmx) do call "%VMCMD%" "%%F" suspend -Q
Now that the server is suspended, I can copy the image to a temporary directory to minimize the time that the server has to be down.
:: if directory already exists, delete all from directoryif exist "%VMTEMPDIR%" del /q "%VMTEMPDIR%"if not exist "%VMTEMPDIR%" mkdir "%VMTEMPDIR%":: Copy all files from our image directory to our temp directoryxcopy "%VMIMAGEDIR%\*.*" "%VMTEMPDIR%"
In the next step, I will resume all VMWare images that I have suspended.
:: If I suspended the VM, For each VMware Image in directory, re-start it if %VMSTARTUP%==1 for %%F in (%VMIMAGEDIR%\*.vmx) do call "%VMCMD%" "%%F" start
Next, I use a compression program to compress the image and put it in another directory. I use WinRAR and it works for me. PKZip, Gzip, and other compression programs will work as well.
if not exist "%VMBACKDIR%" mkdir "%VMBACKDIR%"call "C:\Program Files (x86)\WinRAR\rar.exe" a -r -df -ep1 -m1 "%VMBACKDIR%\%1_%timestr%.rar" "%VMTEMPDIR%\*.*"
Lastly, I clean up the temporary directory and delete it.
:: clean upif exist "%VMTEMPDIR%" del /q "%VMTEMPDIR%"if exist "%VMTEMPDIR%" rmdir "%VMTEMPDIR%"
Here is the entire batch file:
:: **************************************************************************@ECHO OFF:: if missing parameters print error and exitIF "%1"=="" ECHO. must enter a VMImage to back upIF "%1"=="" GOTO endset VMDIRECTORY=Y:\VMImages\SET VMIMAGEDIR=Y:\VMIMAGES\%1set VMTEMPDIR=Y:\%1TEMPset VMBACKDIR=z:\vmbackup\%1set VMCMD=C:\Program Files (x86)\VMware\VMware Server\vmware-cmd:: if VMImage does not exist print error and exitIF not exist %VMIMAGEDIR% ECHO. "%1" does not existIF not exist %VMIMAGEDIR% GOTO end:: Get DateStampfor /f "tokens=1,2" %%u in ('date /t') do set d=%%vfor /f "tokens=1" %%u in ('time /t') do set t=%%uif "%t:~1,1%"==":" set t=0%t%set timestr=%d:~6,4%-%d:~0,2%-%d:~3,2%ping %1if %ERRORLEVEL%==0 set VMSTARTUP=1if not %ERRORLEVEL%==0 set VMSTARTUP=0:: If VM is up (use errorlevel from ping to check this) For each VMware Image in directory, suspend itif %VMSTARTUP%==1 for %%F in (Y:\VMIMAGES\%1\*.vmx) do call "%VMCMD%" "%%F" suspend -Q:: if directory already exists, delete all from directoryif exist "%VMTEMPDIR%" del /q "%VMTEMPDIR%"if not exist "%VMTEMPDIR%" mkdir "%VMTEMPDIR%":: Copy all files from our image directory to our temp directoryxcopy "%VMIMAGEDIR%\*.*" "%VMTEMPDIR%"REM If we suspended the VM, For each VMware Image in directory, re-start itif %VMSTARTUP%==1 for %%F in (%VMIMAGEDIR%\*.vmx) do call "%VMCMD%" "%%F" startif not exist "%VMBACKDIR%" mkdir "%VMBACKDIR%"call "C:\Program Files (x86)\WinRAR\rar.exe" a -r -df -ep1 -m1 "%VMBACKDIR%\%1_%timestr%.rar" "%VMTEMPDIR%\*.*":: clean upif exist "%VMTEMPDIR%" del /q "%VMTEMPDIR%"if exist "%VMTEMPDIR%" rmdir "%VMTEMPDIR%":end:: **************************************************************************