Jump to content

vmWare Backup Script (Server 2, Workstation, possibly ESX)


Recommended Posts

Posted (edited)

I was using vmWare Server 1.x to host our intranet site, scripted backup using vmCOM API, all worked well etc.

I had to reinstall the host OS so decided to move to Server 2.0 while i was at it, but i didnt check whether the currect backup script would work; it wont (no vmCOM in server2)

 

After looking around on google and not finding anything particulaly good i decided to wack one together myself using the vmWare VIX 1.6 API. The script is to be run from a Windows PC/host and afaik should work with both versions of server and all versions of workstation from 6+, and also ESX.

 

VIX API compatibility.

http://blogs.vmware.com/photos/uncategorized/2008/07/07/matrix_3.png

As you can see VIX is the only API which is fully supported accross all platforms, hense my choice. It does not currently work with ESXi as the only API for ESXi is VI remote.

 

This script is by no means finished, nor does it log errors very well, but it does work. Just thought id get it posted up on here so people who have came accros the same problem atleast have something working by means of backup. Oh and people can tidy up my mess too! (im by no means a scripter)

 

What it does:

Suspends the VM

Copies its files to a temp folder suing robocopy.

Starts the VM

Used 7z CLI to make a compressed archive. (Only on level 1 compression atm due to my server being slow, will add ability to change in ini file)

Appends steps to datestamped log file.

 

What everything does;

backup.bat - the main script.

run.bat - launches and recorded the output of backup.bat to the log file defined in the ini.

*.ini - defines the parameters.

 

Prerequisites:

VIX API installed on the PC you will be running the script from. - You will need to register on the vmware site to get this. It is located on the main download page for Server 2.0

Robocopy - http://www.microsoft.com/downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en

 

 

 

To use;

Unzip files.

Create/Edit the ini file to your liking (i have included a couple to give you examples)

Edit "Run.bat" and chang the first line to point to your ini file.

Run "Run.bat"

 

What ive tested working;

Script run localy on windows server 2003 host with vmserver2

Script successfully backed up a Windows XP test machine, a Debian4.0 and JeOS 8.10 (Ubuntu's cut down vm)/

 

 

Not tested but should work;

Script run remotely on windows workstation

vmServer 1.x vmserver 2.0, workstation 6.x+, ESX

 

 

As ive said im not a scripter so would appreciate it if someone can take a look at this. How would i tidy up the "for /f "tokens=2 delims==" %%? in ('find..." bit. Is there a way to read multiple variables from an ini file without repeating commands? (Obviously i could set a variable for this but ill wait and see if theres a "proper" way to do it first)

 

Main Script:

@echo off
for /f "tokens=1,2,3 delims=/- " %%x in ("%date%") do set DATESTAMP=%%z%%y%%x

setlocal enableextensions

for /f "tokens=2 delims==" %%? in ('find /i "HOST" ^< %INI%') do set HOST=%%?
if "%HOST:~0,1%" equ " " set HOST=%HOST:~1%

for /f "tokens=2 delims==" %%? in ('find /i "VMNAME" ^< %INI%') do set VMNAME=%%?
if "%VMNAME:~0,1%" equ " " set VMNAME=%VMNAME:~1%

for /f "tokens=2 delims==" %%? in ('find /i "VMDIR" ^< %INI%') do set VMDIR=%%?
if "%VMDIR:~0,1%" equ " " set VMDIR=%VMDIR:~1%

for /f "tokens=2 delims==" %%? in ('find /i "VMROOTPATH" ^< %INI%') do set VMROOTPATH=%%?
if "%VMROOTPATH:~0,1%" equ " " set VMROOTPATH=%VMROOTPATH:~1%

for /f "tokens=2 delims==" %%? in ('find /i "USER" ^< %INI%') do set USER=%%?
if "%USER:~0,1%" equ " " set USER=%USER:~1%

for /f "tokens=2 delims==" %%? in ('find /i "PASS" ^< %INI%') do set PASS=%%?
if "%PASS:~0,1%" equ " " set PASS=%PASS:~1%

for /f "tokens=2 delims==" %%? in ('find /i "DATASTORE" ^< %INI%') do set DATASTORE=%%?
if "%DATASTORE:~0,1%" equ " " set DATASTORE=%DATASTORE:~1%

for /f "tokens=2 delims==" %%? in ('find /i "TEMPDIR" ^< %INI%') do set TEMPDIR=%%?
if "%TEMPDIR:~0,1%" equ " " set TEMPDIR=%TEMPDIR:~1%

for /f "tokens=2 delims==" %%? in ('find /i "BACKUPDIR" ^< %INI%') do set BACKUPDIR=%%?
if "%BACKUPDIR:~0,1%" equ " " set BACKUPDIR=%BACKUPDIR:~1%

for /f "tokens=2 delims==" %%? in ('find /i "VIX" ^< %INI%') do set VIX=%%?
if "%VIX:~0,1%" equ " " set VIX=%VIX:~1%

for /f "tokens=2 delims==" %%? in ('find /i "ROBOCOPY" ^< %INI%') do set ROBOCOPY=%%?
if "%ROBOCOPY:~0,1%" equ " " set ROBOCOPY=%ROBOCOPY:~1%

for /f "tokens=2 delims==" %%? in ('find /i "COMPRESS" ^< %INI%') do set COMPRESS=%%?
if "%COMPRESS:~0,1%" equ " " set COMPRESS=%COMPRESS:~1%

for /f "tokens=2 delims==" %%? in ('find /i "LOGS" ^< %INI%') do set LOGS=%%?
if "%LOGS:~0,1%" equ " " set LOGS=%LOGS:~1%
echo.
echo ------------------------------------------------------------------------------------------------
echo %TIME% Starting backup %VMNAME%
echo ------------------------------------------------------------------------------------------------
echo.
echo %TIME% Deleting temp files in %TEMPDIR%\%VMNAME% ...
IF EXIST "%TEMPDIR%\%VMNAME%" rd "%TEMPDIR%\%VMNAME%" /S /Q
md "%TEMPDIR%\%VMNAME%"

echo %TIME% Suspending %VMNAME% for snapshot backup ...
"%VIX%\vmrun" -T server -h %HOST% -u %USER% -p %PASS% suspend "%DATASTORE% %VMDIR%/%VMNAME%.vmx"

echo %TIME% Suspended %VMNAME%, copying files to temp dir ...
%robocopy% "%VMROOTPATH%\%VMDIR%" "%TEMPDIR%\%VMNAME%" /e /NP

echo.
echo %TIME% Copying completed. Starting %VMNAME% ...
"%VIX%\vmrun" -T server -h %HOST% -u %USER% -p %PASS% start "%DATASTORE% %VMDIR%/%VMNAME%.vmx"

echo %TIME% Startup complete. Compressing backup files for %VMNAME% and transfering to %BACKUPDIR%
%COMPRESS% a -t7z -mx1 -mmt "%BACKUPDIR%\%VMNAME%_%DATESTAMP%.7z" "%TEMPDIR%\%VMNAME%\*.*"

echo %TIME% Finished compressing. Deleting temp files in %TEMPDIR%\%VMNAME% ...
rem rd "%TEMPDIR%\%VMNAME%" /S /Q
echo %TIME% Completed! The archieve is located in %BACKUPDIR%
echo ================================================================================================

 

Also i will be making the "Run.bat" able to recieve the ini file via CL like so; "run.bat" -ini=server1

Will do this ASAP.

 

But in the mean time here it is, please feel free to edit, copy and improve. But please do post back and hopefully with abit of work we will have a finished script to go in the wiki which should work for the forseeable future atleast.

 

 

//Looking further into it; if you want to use vmWare server 1.x you will need to edit the line starting "%VIX%\vmrun.exe ....." and add a "1" after "server" :- ""%VIX%\vmrun" -T server1 -h..."

If you want to use Workstation then please have a read through the VIX API manual and correct the command.

vmbackup.zip

Edited by Guest
Posted (edited)

I would be very interested in any development on this. We have just started looking at moving some servers onto the free ESXi and I am wondering about backups. I nearly died when I was given the backup exec agent price so I am looking for alternatives.

Discussing it with Ric earlier it did sound like reasonable enough to treat them as a normal machine and have a template server vm to restore to but I would like to see other methods in action.

 

If I get time tommorow I will have a look at the script and see if I can get my head round it. One thing I would like to do is convert it to VBScript as I find that easier to read, even if it might end up bulkier

Edited by ChrisH
Posted
I have been looking at this today and couldnt find the VIX 1.6 API. There doesnt seem to be a download link and topics on their forums are no help. There doesnt seem to be much positivity about backing up ESX either but I want to see that for myself :p .
Posted (edited)

I was looking for the 1.6 you mentioned. That links to 1.1.5 is this correct?

 

 

Looks like I definately need 1.6 as there is no vmrun in this version.

 

 

edit 2!

Now I have read your post carefully and other documentation this isnt going to do what I wanted as I will mostly be working with ESXi. Serves me right for not reading carefully enough but I will keep my eye on it for future developments.

Edited by ChrisH
Posted (edited)
I was looking for the 1.6 you mentioned. That links to 1.1.5 is this correct?

 

 

Looks like I definately need 1.6 as there is no vmrun in this version.

 

 

edit 2!

Now I have read your post carefully and other documentation this isnt going to do what I wanted as I will mostly be working with ESXi. Serves me right for not reading carefully enough but I will keep my eye on it for future developments.

 

Sorry, was in a rush so saw VIX and didnt look at the version number.

 

You will have to register to get the VIX API. Its on the main download page for Server 2.

 

 

From reading about i believe the only API you can use with ESXi is VI. (VI perl, VI python? etc). I would be interested to hear if you find anything on ESXi as i was planning on using that but have settled (atleast until i get more time) on server 2 on a windows host :(

Edited by Guest
Posted
I have it but I didn't read your original post about ESXi which is what I have been experimenting with. I am currently playing with the backup script on the VMware forums for backing up using VI Remote.
  • 6 months later...
Posted

 

To use;

Unzip files.

Create/Edit the ini file to your liking (i have included a couple to give you examples)

Edit "Run.bat" and chang the first line to point to your ini file.

Run "Run.bat"

 

 

Hi there,

 

I'm testing vmware 2.0.1 on both Linux and Windows platform. Om Linux there is no problems for me with backup, but on Windows 2003 R2 server I'm having difficulties.

 

My ini file:

 

;
; DO NOT include trailing \ (slash)


; IP of host, if the script is running on same PC as vmserver put https://127.0.0.1:8333/sdk
HOST=https://127.0.0.1:8333/sdk

; Name of VM (vmx)
VMNAME=SBS_2003_server

; Path to VM root
VMROOTPATH=C:\Virtual Machines

; VM dir name
VMDIR=SBS_2003_Server

; Username with permissions over vmServer
USER=Administrator

; Password
PASS=PASSWORD

; Name of datastore enclosed in [ ] ie [Virtual Machines in d]
DATASTORE=[standard]

; Temp working dir for files
TEMPDIR=C:\backuptemp

; Dir to copy compressed archive to
BACKUPDIR=C:\backups\Virtual Machines

; Path to vmWare VIX API folder, normally "C:\Program Files\VMware\VMware VIX"
VIX=C:\Program Files\VMware\VMware VIX

; Path to robocopy
ROBOCOPY=C:\Program Files\Windows Resource Kits\robocopy.exe

; Path to 7Z CLI
COMPRESS=C:\Program Files\7-Zip\7z.exe

; Path to log files
LOGS=C:\vmpub\backup_log

 

My run.bat:

 

@echo off
SET INI=SBS_2003_Server.ini

setlocal enableextensions

for /f "tokens=2 delims==" %%? in ('find /i "VMNAME" ^< %INI%') do set VMNAME=%%?
if "%VMNAME:~0,1%" equ " " set VMNAME=%VMNAME:~1%

for /f "tokens=2 delims==" %%? in ('find /i "LOGS" ^< %INI%') do set LOGS=%%?
if "%LOGS:~0,1%" equ " " set LOGS=%LOGS:~1%

echo Backup of %VMNAME% started @ %TIME% on %DATE%

for /f "tokens=1,2,3 delims=/- " %%x in ("%date%") do set DATESTAMP=%%z%%y%%x

backup.bat >> "%LOGS%\%VMNAME%\%VMNAME%_%DATESTAMP%.txt"
Echo Completed @ %TIME% on %DATE%
pause

 

Can you see where I'm making the error :confused:

  • 3 weeks later...
Guest Guest
Posted
Hi there,

 

I'm testing vmware 2.0.1 on both Linux and Windows platform. Om Linux there is no problems for me with backup, but on Windows 2003 R2 server I'm having difficulties.

 

 

 

First thing which jumps out at me is there is a space in your folder path. Give that a shot first.

 

 

Unfortunatly we have moved to a full SAN-Xen solution so i wont be doing any more with this script.

  • 1 month later...
Guest Guest
Posted
Did you find out if the space in your folder path was the problem?

I have a space in mine too and it won't work...

 

Im fairly sure this is what the problem is, i tend not to use spaces for this reason. The script certainly works, infact its still in use now; been running for over 6 months and hasnt failed yet.

Posted

Ok, did you change the names of the VMs/folders or did you put som kind of symbol like " or ' inside the paths?

 

Thanks for your reply.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...