/build/static/layout/Breadcrumb_cap_w.png

Back Me Up, Scotty Back Up User Data to a VHD

As IT Pros we typically focus on backups at the server level. And while it is easy to say it is up to the user to put files in the "right spot" so they get backed up, this isn't always practical, or good customer service. Face it, you might want to get a quick backup of critical files before undertaking a data-threatening repair. Or perhaps you'd like a way for mobile users to not only have a quick backup, but easy access to their backup. One approach you might consider for Windows 7 is creating a small virtual hard disk (VHD) and copy the user's files to it.

Normally, you would use the disk management console to create the VHD. But it can also be created from the command line using the Diskpart.exe command line utility. Since you use the same steps to create a VHD, format it and mount it, these steps can be put into a text file and passed to Diskpart.exe as a "script". I've taken this a step further and created a PowerShell function called New-VHD.ps1. You can download the script here.

Open a PowerShell prompt and "dot source" the script.

PS C:\> . C:\scripts\new-vhd.ps1

I'm going to run everything from the C: drive but you could easily run from a UNC or removable USB drive. Here's the basic syntax:

New-VHD [-Path] [-Size] [-Drive] [-Expandable] [-Compressed [-WhatIf] [-Confirm]

To create a VHD specify the name of the VHD (-Path) and its size in megabytes (-Size). The only other required parameter is a drive letter to assign so the VHD can be easily mounted. All you need to specify is the letter, e.g., "Q". By default the function will create a fixed disk or you can use –Expandable to make it dynamic. Optionally, you can also enable disk compression within the VHD. Armed with this tool you can create a VHD in an elevated session.

PS C:\> new-vhd d:\mydisk.vhd 1024 Q

This will create a fixed 1GB vhd and mount it as drive Q. You will most likely get a popup window like Figure 1

Figure 1 Format Dialog

The function will handle the formatting so you can safely click cancel. When the function finishes you'll get another prompt to open the drive. Go ahead. Now you can drag and copy files and folders to the VHD.

When you are finished, open the Disk Management console, right-click the VHD drive and detach it.

Figure 2 Detach the VHD

The VHD file is completely portable. All you need to do is remount it to retrieve the files.

You might even want to put this in a wrapper script to create a VHD just large enough to backup the user's documents folder.

Param (
[string]$Path="$env:userprofile\documents",
[string]$VHDPath="\\chi-fp01\backup\$env:username.vhd",
[string]$Drive="Q")

#build a path to New-VHD.ps1
$scriptfolder=Split-Path ($myinvocation.invocationname)

#dot source the new-vhd script
. (Join-Path $scriptfolder new-vhd.ps1)

Write-Host "Getting files from $path" -foreground Green

$files=dir $path -recurse
$size=$files | measure -Property Length -Sum

#add a little extra room
[int]$vhdsize=($size.sum/1mb)*2
Write-Host "Creating a $vhdsize MB VHD $VHDPath" -foreground Green

New-Vhd -Path $VHDPath -Size $vhdsize -Drive $drive -expandable -compressed

#mount the VHD drive as a PSDrive
Try {
    new-psdrive -Name $drive filesystem -Root "$($Drive):\" -ea Stop |out-null

    write-Host "Copying $($files.count) files and folders to the VHD" -foreground Green

    copy $path -dest "$($drive):\" -Container -recurse -ea SilentlyContinue
    Write-host "Add any other files you want to the VHD and manually detach." -foreground Green
    
} #try
Catch {
    Write-Warning $_.exception.message
}

You would need to be running an elevated PowerShell session but your administrator account should still have access to the user's documents. You could then run a command like this, assuming the backup and new VHD scripts are in the same network share:

PS C:\> \\fp01\scripts\DocBackup-to-VHD.ps1 -Path C:\users\afredo\Documents -VHDPath "\\fp01\vhd\afredo.vhd" -Drive z

This short script will get all the files in the specified path and calculate how much space is needed. The script then creates an appropriate sized VHD and copies the files to it. I'm sure you can think of other interesting ways to use this VHD. In the meantime, check out the accompanying article for more backup options and ways to keep everyone happy.


Comments

This post is locked
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ