Saturday, February 6, 2010

Backup outlook using Volume Shadow Copy

Shadow Copy (a.k.a Volume Snapshot Service or VSS for short), is a Windows feature that can access files even when they are locked. It has many great uses, and this time I want to talk about backing up outlook. When outlook is running, it locks the PST files it uses, making them impossible to backup. One workaround is to close outlook before the backup, but that has several disadvantages, and may be hard to automate. Many backup utilities use shadow copy to help, but what if you don’t want to spend money? As usual, I’m here to help.

To use Shadow Copy, you would need a little utility called VSHADOW. I wish I could offer it as a direct download from here, but unfortunately, you’ll have to download the Windows SDK for that. It’s quite a hefty download – an ISO image that is over 1 GB long and takes FOREVER to install. Once you’re done with that, go to c:\program files\Microsoft SDKs\Windows\v6.1\Bin\vsstools and find VSHADOW.EXE there. It would be a 226K file – run it once to make sure it displays the right version – 3.0:

clip_image002

Copy this file to somewhere safe, and you may now uninstall the SDK to save on some disk space (approx 1.3 GB to be exact). If you had some virtual machine to do this install on, even better.

To actually make a backup, you need to perform 4 steps:

1. Use Vshadow to create a “snapshot” – this captures a drive the way it is at a certain point of time.

2. Mount the snapshot as another drive

3. Copy the files from the newly mounted drive to somewhere else (the backup destination).

4. Erase the snapshot.

Erasing the snapshot at the end is not really critical – you can leave it there for eternity, but since it’s frozen in time, there isn’t much point in making another backup from it in the future. You can mount multiple snapshots, but you’ll run out of drive-letters pretty fast.

To be more technical, here’s what you do:

1. Open a CMD prompt, which must have Administrative privileges.

2. Go to where you saved VSHADOW.EXE, and run the following command. If the files you want to backup are on a drive other than C, substitute it’s letter with c:

Vshadow –nw –p c:

3. On the output of this command, note the line that says SNAPSHOT ID:

clip_image004

If you get an error message like “Access denied”, that means you are not an administrator, or that you have ran your CMD not as an administrator.

4. Run the following command to map the drive letter, using the proper snapshot ID. You can use any letter you want, if J is already in use:

vshadow.exe -el={57b7248c-e086-46fc-848d-f449fb4c0ae3},j:

5. Now, use your favorite file copy method to copy any files you want from that temporary drive to anywhere else. I use ROBOCOPY and copy to my external USB drive, but you can do anything you want, really.

6. Once done with the file copy, use this command to un-mount the temporary drive and delete the snapshot. This does NOT do anything to the original files – just gets rid of the shadow copy and frees up the drive letter and system resources. Note that we are using the same snapshot ID as before:

vshadow.exe -ds={57b7248c-e086-46fc-848d-f449fb4c0ae3}

7. That’s it! Now you are ready to do this again tomorrow.

As you may or may not know, I don’t like to do so many things manually, so I wrote a VBScript to do the same as above, automatically. I use the WScript.Shell object and its RUN method to execute the various commands, and VBScript’s INSTR to find the snapshot ID. Other than that, the script is pretty straightforward:

'Create the Objects

set oWshell = CreateObject("WScript.Shell")

set oFSO = CreateObject("Scripting.FileSystemObject")

'Create the snapshot

oWshell.run "%COMSPEC% /C c:\vshadow -nw -p d: >c:\vshadow.txt", 0, TRUE

'Read the output and find the Snapshot ID

set oFile = oFSO.openTextFile("c:\vshadow.txt", 1, TRUE)

sOutput=oFile.readall

oFile.close

sSnapshotID=mid(sOutput,instr(sOutput,"SNAPSHOT ID")+14,38)

'Mount the Drive

wshell.run "%COMSPEC% /C c:\vshadow -el=" & sSnapshotID & ",j:", 0, TRUE

'Copy the files. Note that I'm using double-quotes because the path has a space in it

wshell.run "%COMSPEC% /C c:\Robocopy ""j:\My Documents"" ""x:\My Documents"" *.pst", 0, TRUE

'Destroy the snapshot

wshell.run "%COMSPEC% /C c:\vshadow -ds=" & sSnapshotID, 0, TRUE

You may adjust the script for different drive letters and folders. That’s it! Now, just use the Windows Task Scheduler to run this daily, and you’re done!

1 comment:

  1. I can't believe there are no comments on such a great post!

    Ben, I'm working on incorporating this solution to backup .pst files to a Netgear ReadyNAS NV+ running rsync. I'm using DeltaCopy to serve as my rsync server and I'm successfully connecting to it using the ReadyNAS.

    My thought is to create the shadow copy and mount it (say as z:) and then assign the path (z:\users\myuser\appdata\local\microsoft\outlook) to a virtual directory in the DeltaCopy server.

    My concern is with the destruction of the snapshot and the timing of it. Since I'm using the ReadyNAS to schedule and execute the rsync backup job, there's no way to know when to destroy it.

    Any ideas on what I could do to resolve this? I think if we could incorporate rsync with this it'd be the holy grail!

    Thanks for this post!!!

    ReplyDelete