Robocopy backup to dynamically-lettered removable drive

I use a Seagate FreeAgent GoFlex USB drive to carry large install files to customer sites. I wanted an easy, portable way to keep my Flex drive in sync with the working files on my laptop’s D: drive. I use Robocopy for big backup jobs on the server application products we support so I put it to use here, too, and it worked fine. The only annoying part was editing the Robocopy statement whenever the drive letter of the Flex changed.

To get around this I named the Flex volume “flex” (original) and added a statement to find the driver letter on-the-fly.

This should work for any removable drive for which you know the volume name. Just change the text of the find "flex" statement in line 10.

  • You’ll need a copy of Robocopy. I grabbed it from the Windows Server 2003 Resource Kit and it runs fine on the XP & Win7 workstations I’m using. I think the new version might be delivered standard with Server 2008.
  • This example mirrors the entire D: drive, excluding two directories I don’t care about, to the Flex drive. If you haven’t used Robocopy, do some reading before using /MIR. It will delete anything on the destination that doesn’t exist on the source.

backup_flex.bat


@echo off

echo. > "%cd%\backup_flex.log"
echo %time% *** backup starting *** >> "%cd%\backup_flex.log"
echo. >> "%cd%\backup_flex.log"

rem Find the drive letter corresponding to the removable drive named "flex"
setLocal Enabledelayedexpansion
for %%i in (d e f g h i j k l m n o p q r s t u v w x y z) do (
	for /f "tokens=1,* delims=:" %%x in ('fsutil fsinfo volumeinfo %%i: ^|find /i "flex"') do set drive=%%i
)

REM Mirror the D: drive to the Flex drive, excluding some directories.
Robocopy D:\ %drive%:\ /MIR /XJ /R:0 /W:1 /NP /NDL /xd "D:\Tools\Outlook offline files" "D:\Tools\Windows Search Index" /LOG+:"%cd%\backup_flex.log" 2>>errors.txt

echo. >> "%cd%\backup_flex.log"
echo %time% *** flex backup done *** >> "%cd%\backup_flex.log"
echo. >> "%cd%\backup_flex.log"

p.s. If anyone comes up with a cool way to limit the drive letters the FOR statement needs to iterate through, I’d be happy to hear about it. I spent more than a few minutes trying to work the output from fsutil fsinfo drives into the loop but I’m missing something.

for /f "tokens=1,*" %%i in ('fsutil fsinfo drives') do (
  rem iterate through the ...volumeinfo command with %%j
)

Launching VMware remote console from a batch script

For a long time I’ve used this method to deal with starting and stopping the VMware Server 2 services on-demand. My habit was to log into Web Access, start/resume the guest, and launch the Remote Console from there. I recently started using VM shortcuts to launch the Remote Console directly, instead of logging into the VMI Web Access client first. When I’m not making configuation changes, the shortcuts get me to the VM desktop quicker. Naturally, once I saw how simple the shortcut syntax is, I wanted to streamline everything with a script to manage the services the same way I do with the Web Access, and cut out the Web Access piece completely.

The command to launch the Remote Console looks like this.
Linux:
vmware-vmrc -h [<hostname>] [-u <username> -p <password>] [-M <moid> | <datastore path>]

Windows:
vmware-vmrc.exe -h <hostname> [-u <username> -p <password>] -M <moid> | <datastore path>

The user/pass combo is optional. If it’s not passed from the command line you’ll be prompted each time.

You can identify the VM either by the Managed Object ID (MOID) or datastore path, you don’t need both. As I mentioned in that previous post, I can’t get the user/pass to work from the command line unless I use the datastore path. I saw some mention in the forums this might be caused by these guests having been created by the 1.x Server version. Try the MOID and see if it works for you.

You’ll need two bits of info to modify my script to work for you: the path to vmware-vmrc.exe and either the MOID or datastore location.

  • vmware-vmrc.exe: Create a shortcut using the Generate Virtual Machine Shortcut command, then look at the Properties for the shortcut. This is an easy way to confirm the path to vmware-vmrc.exe and find the MOID. You can chuck this shortcut after copying the path.
  • MOID: You can also find the MOID for a VM by looking at vmInventory.xml in /etc/vmware/hostd/ under Linux or %ALLUSERSPROFILE%\Application Data\VMware\VMware Server\hostd under Windows.
  • Datastore path: In the VMware Infrastructure Web Access client, select the VM and click Configure VM. On the General tab, the datastore path will look something like this:
    [standard] <folder>/<name>.vmx

    datastore

Optional

I added a special user to my system and use that user/pass combo on the command line. You don’t have to do this. You can either omit the -u and -p switches from the command line (and enter them at the prompt each time) or use the existing user/pass on the command line. This assumes you’ve been logging into the web client successfully.

I chose to do it this way because I wanted the ease and speed of passing credentials on the command line but did not want to expose my normal account’s password in plaintext.

Add a non-priveleged user to the windows system. This is a Home Premium workstation so I have to make do with the simplified user management and have not added it to a group.
Add that user as an Administrator in VMware:

  1. Select the host object | Permissions tab
  2. Select the User
  3. Change the Role to Administrator
  4. datastore

It makes sense that some reduced permissions level would allow you to launch the remote console but I haven’t experimented yet.

Once you make sure you can log into VMI Web Access with this account, put your info in the script and give it a spin.

My habit, when I’m done with a vm, is to select VMware Remote Console | Troubleshoot | Suspend and Exit (or Power Off and Exit). This tells the vm what to do and exits the console in one step. (Once the console exits, the rest of the script gets evaluated.)

I’ve had no problems using these scripts on the two main systems I work with, both running VMware Server 2.0.2:

  • Win7 Home Prem SP1 x64
  • WinXP Pro SP3 x86

Special bonus, no charge – I added a quick test to the end of the script that simply leaves the VMware server services running if there was more than one guest VM running. This way the services only get stopped when they’re not needed. Be aware it’s not yet smart enough to know if the web client is running.

vmware_merlin.bat

@echo off

REM **** Check if Tomcat is running. If so, assume all VMware server services are running. Otherwise,
REM ** start them.
tasklist /FI "IMAGENAME eq tomcat6.exe" 2>NUL | find /I /N "tomcat6.exe">NUL
if %ERRORLEVEL%  equ 0  ( 
    REM ** Tomcat is running, just get on with it.
    echo Tomcat is running, getting on with it.
	) else (  
		REM **** Start VMware services ****
		net start VMwareServerWebAccess
		net start vmauthdservice
		net start VMwareHostd
		net start vmnetdhcp
		net start "vmware nat service"
		
		REM ** Let's wait a couple--ok, alot--of jiffys to let Tomcat get ready
		REM ** x * 1000 is Number of miliseconds to wait
		> "%Temp%.\sleep.vbs" ECHO WScript.Sleep 2 * 1000
		CSCRIPT //NoLogo "%Temp%.\sleep.vbs"
		DEL "%Temp%.\sleep.vbs"  
		)

REM **** Start Remote Console ****
REM ** This passes the username/password of a user with login permission in VMI and calls
REM ** the guest by the datastore path. I couldn't get the user/pass to work when calling
REM ** the guest by the Managed Object ID.

"C:\Program Files (x86)\Common Files\VMware\VMware Remote Console Plug-in\vmware-vmrc.exe" -u vm -p test1234 -h zaphod:8333 "[standard] merlin/merlin.vmx"

REM **** Check if any guests are running. If so, leave the VMware server services alone. Otherwise,
REM ** stop them.
tasklist /FI "IMAGENAME eq vmware-vmx.exe" 2>NUL | find /I /N "vmware-vmx.exe">NUL
if %ERRORLEVEL%  equ 0  ( 
	REM ** Leave VMware services running and exit **
	echo Another guest is running 
	) else (  
		REM ** Stop VMware services **
		net stop VMwareHostd
		net stop vmauthdservice
		net stop vmnetdhcp
		net stop "vmware nat service"
		net stop VMwareServerWebAccess  
		)

As with the Server script, I call it through this snippet to get rid of the command window.

vmware_merlin_launch.js

var WindowStyle_Hidden = 0
var objShell = WScript.CreateObject("WScript.Shell")
var result = objShell.Run("cmd.exe /c vmware_merlin.bat", WindowStyle_Hidden)

Start and stop VMware Server 2 services automatically

I use the free VMware Server for a few test and dev purposes on my laptop, but I don’t have any VMs that run all the time. So of course it annoys me to have the VMware Server services running the rest of the time, just sitting around twiddling their thumbs. I’m absolutely certain it takes some of my system resources for them to shoot virtual spitballs at each other in their boredom.

At first I simply scripted the starting and stopping of those services so I could manage them easily when I wanted to. As is usually my way, what originated as two separate scripts (one of net start commands and one of net stop commands, run manually), eventually morphed into a thing with little bits of logic that controls everything from the start of my server use through to the end.

A mis-named couple points of interest:

  • When I added the browser launch statement after starting the services, I found I needed to delay a couple seconds to reliably get the login screen. I’ve only used this script on a couple laptop hosts so try it without this delay on your system, in case it’s something weird with mine.
  • The script will sit at the IE launch statement until you close the last instance of the browser. Since I only use IE for the VMware Infrastructure Web Access, and so it’s unlikely there are multiple instances, the logic in the script is simple and reliable. If you use IE for normal browsing you’ll likely need to come up with a cleverer way of telling the script to shove on and stop the services.
  • Once this all worked reliably I eventually wished to get rid of the annoying command window that stays open the whole time the script is active. I found, through the wisdom of the internet, the attached .js launch method to solve that.

Now I have a quick launch shortcut pointed at vmware2_launch.js to keep things simple and clean. This works well on the two systems I use, both running VMware Server 2.0.2:

  • Win7 Home Prem SP1 x64
  • WinXP Pro SP3 x86

Here are the code segments. (I need to find a simpler, less aggravating way to present cleaner code in WordPress posts.)

Edit: updated after installing SyntaxHighlighter Evolved. Very nice.

vmware2.bat

@echo off

REM **** Check if Tomcat is running. If so, assume all VMware server services are running. Otherwise,
REM ** start them.
tasklist /FI "IMAGENAME eq tomcat6.exe" 2>NUL | find /I /N "tomcat6.exe">NUL
if %ERRORLEVEL%  equ 0  ( 
	echo Tomcat is running, just get on with it.
	) else (  
		REM **** Start VMware services ****
		net start VMwareServerWebAccess
		net start vmauthdservice
		net start VMwareHostd
		net start vmnetdhcp
		net start "vmware nat service"
		
		REM echo Let's wait a couple--ok alot--of jiffys to let Tomcat get ready

		REM x is Number of miliseconds to delay
		> "%Temp%.\sleep.vbs" ECHO WScript.Sleep 2 * 1000
		CSCRIPT //NoLogo "%Temp%.\sleep.vbs"
		DEL "%Temp%.\sleep.vbs"  )

REM **** Start web browser ****
REM **Using the <hostname> instead of "127.0.0.1" or "localhost"
REM **avoids the annoying certificte warnings

"C:\Program Files (x86)\Internet Explorer\iexplore.exe" http://zaphod:8308/ui/#


REM ** Stop VMware services **
net stop VMwareHostd
net stop vmauthdservice
net stop vmnetdhcp
net stop "vmware nat service"
net stop VMwareServerWebAccess

vmware2_launch.js

var WindowStyle_Hidden = 0
var objShell = WScript.CreateObject("WScript.Shell")
var result = objShell.Run("cmd.exe /c vmware2.bat", WindowStyle_Hidden)