Wednesday, 14 April 2010

Date and Time Injection for Command Line Parameters

I like to backup files using tools such as robocopy, richcopy and 7-Zip, and sometimes I want to include the day or date and time in the backup path or file name. For example, I might want to copy some files to an external hard disk and have them go in to a Monday folder on a Monday, a Tuesday folder on a Tuesday etc.

After looking around I failed to find a satisfactory solution so being a software developer I decided to create my own solution - the result was DateParseCmd.exe.

This is a very simple command line tool written in Delphi 7. The parameters for this command are actually just another command with it's parameters. However, in the child command's parameters, if you want to specify the current date and/or time you can do so by enclosing a Delphi style DateTime Format String in {[ and ]}.

Suppose I want to use a single robocopy script to backup to a DayOfTheWeek folder, I can now do something like this:

DateParseCmd robocopy "C:\My Projects" "F:\Backups\{[dddd]}\My Projects" /MIR /ZB /R:3 /W:10

DateParseCmd first parses it's command line parameters and {[dddd]} will resolve to "Monday" on a Monday. It will then launch the robocopy command with it's newly evaluated parameters i.e.

robocopy "C:\My Projects" "F:\Backups\Monday\My Projects" /MIR /ZB /R:3 /W:10

If I want to archive a folder using the standalone 7-Zip command line tool and have the current date in the archive name so that I never overwrite an archive:

DateParseCmd 7za.exe a -r "D:\My Backups\Source Code {[yyyy-mm-dd]}.7z" "C:\Source Code\*.*"

This will create an archive named "Source Code 2010-04-14.7z". Simple stuff, but very useful.

Shadows in the dark

(or the elusive vshadow.exe)

vshadow is a very cool command line tool from Microsoft for working with the Volume ShadowCopy Service (VSS). If you don't know what VSS is then I'm not about to go in to the details now but it's what Windows uses for System Restore Points, retrieving Previous Versions of documents and allows you to make copies of files that are in use.

vshadow is much better and easier to use than vssadmin, especially on non-server operating systems on which vssadmin won't let you create a snapshot (or shadow volume). The trouble is that it's not the easiest tool to get hold of - it is actually a demo tool that is supplied with various Microsoft SDKs as an example of how to use the VSS API. As such, the tool is limited and there is plenty that it does not do, however if you are looking for a solution to backing up files that are in use then combined with dosdev.exe (another elusive Microsoft tool found in some SDKs) and robocopy it provides a very robust and scriptable solution.

The annoying part is that the VSS API appears to be different on all versions of Windows and so you need a copy of vshadow that was compiled for the version of Windows you want to use it on (i.e. XP, Vista, Win7, Server 2003 or Server 2008). No one seems willing to host the compiled copies of vshadow as there is some debate as to whether this tool is redistributable under the terms of the SDK - personally I think this tool deserves a place on sysinternals.

Ok, so how do you get hold of vshadow.exe?

XP and Server 2003
Download and install the Volume Shadow Copy Service SDK 7.2. Once installed, browse here: C:\Program Files\Microsoft\VSSSDK72\TestApps\vshadow\bin and you will find the release folders for both XP and Server (2003).

Vista, Server 2008 and Win7
Download and install the relevant Microsoft Windows Software Development Kit:

Server 2008: Windows SDK v6.1
Windows 7: Windows SDK v7.0

You can either install these by using the Web Installer or you can download the ISOs. If you just want the compiled versions of vshadow and don't care about the source code or anything else in the SDK then when installing just choose to only install the Win32 Development Tools.

All 3 SDKs install to C:\Program Files\Microsoft SDKs\Windows plus a folder for each SDK (v6.0, v6.1 and v7.0 respectively). Within each version folder head in to Bin\vsstools and you will find the prize.

In order to script a backup of in use files using vshadow, dosdev and robocopy, take a look at this blog entry which contains a section with a script example.

If this all seems a little too complicated and you just want to backup some in use files and you don't need all the cool options that robocopy has then take a look at Hobocopy. This is a tool inspired by robocopy but uses the VSS API to create a shadow volume from which the files are actually copied. Because it uses the VSS API you must make sure you use the correct build of Hobocopy for the OS you are running it on.