PowerShell for Windows Server Backup

Windows Server Backup is the Windows 2008 replacement for NTBackup.  It enables the creation of backups of whole volumes only.  It is not intended as an Enterprise level backup system being targeted more at single server scenarios

It uses VSS and does backup system state.  It is an optional feature installed through Server Manager.  When installing there is an option to install the command line tools which includes a PowerShell snapin.  The command line tools install insists on the presence of PowerShell V1 installed through the Windows 2008 features.  It will not install if PowerShell V2 is installed.

The snapin has to be enabled by

Add-Pssnapin Windows.serverbackup

This adds a number of cmdlets

Name
—-
Add-WBBackupTarget
Add-WBVolume
Get-WBBackupTarget
Get-WBDisk
Get-WBPolicy
Get-WBSchedule
Get-WBSummary
Get-WBVolume
New-WBBackupTarget
New-WBPolicy
Remove-WBBackupTarget
Remove-WBPolicy
Remove-WBVolume
Set-WBPolicy
Set-WBSchedule

A backup schedule seems to be referred to as a policy.  Sadly apart from the cmdlet help files I couldn’t find any documentation so I created a policy using the GUI and then investigated what had been created.

PS C:Scripts> get-wbpolicy | fl

Schedule        : {11/01/2008 22:00:00}
BackupTargets   : {Microsoft.Windows.ServerBackup.Commands.WBBackupTarget}
VolumesToBackup : {Microsoft.Windows.ServerBackup.Commands.WBVolume}

PS C:Scripts> (get-wbpolicy).schedule

11 January 2008 22:00:00

 

PS C:Scripts> (get-wbpolicy).backuptargets | fl

Label      : SQLTest 2008_01_11 19:01 DISK_01
WBDisk     :
Path       : \?Volume{dae9090c-c06f-11dc-a79a-0003ff69ad11}
TargetType : Volume

 

PS C:Scripts> (get-wbpolicy).volumestobackup | fl

VolumeLabel :
MountPath   : C:
MountPoint  : \?Volume{cb6fae01-a330-11dc-8e93-806e6f6e6963}
FileSystem  : NTFS
Property    : Critical, ValidSource
FreeSpace   : 58093953024
TotalSpace  : 68716331008

So we need to start by creating a new backup policy

PS C:Scripts> $newpol = New-WBPolicy
New-WBPolicy : Can’t create a new policy before removing the old one
At line:1 char:22
+ $newpol = New-WBPolicy <<<<

OK so if we can only have one policy — there can only be one – where have we heard that before?

PS C:Scripts> Remove-WBPolicy -Policy ( Get-WBPolicy ) -WhatIf
What if: Performing operation "You are about to remove your scheduled backup." on Target "Windows Server Backup".

Try it without the -whatif  and we don’t seem to be able to remove the current policy so need to do that in the GUI

To create new policy

PS C:Scripts> $pol = New-WBPolicy
PS C:Scripts> $pol

Schedule                                BackupTargets                           VolumesToBackup
——–                                ————-                           —————

We need to populate the policy.  First we create a backup target which means telling it which disk – the backup system takes the whole disk.

PS C:Scripts> get-wbdisk

DiskName   : Virtual HD ATA Device
DiskNumber : 0
DiskId     : ad0076a7-0000-0000-0000-000000000000
TotalSpace : 68718428160
FreeSpace  : 58094235648
Volumes    : {Microsoft.Windows.ServerBackup.Commands.WBVolume}
Properties : None

DiskName   : Virtual HD ATA Device
DiskNumber : 1
DiskId     : d9983a3d-0000-0000-0000-000000000000
TotalSpace : 17179803648
FreeSpace  : 17084346368
Volumes    : {Microsoft.Windows.ServerBackup.Commands.WBVolume}
Properties : ValidTarget

Create a backup disk


PS C:Scripts> $disk = Get-WBDisk | Where{$_.DiskNumber -eq 1}
PS C:Scripts> $disk

DiskName   : Virtual HD ATA Device
DiskNumber : 1
DiskId     : d9983a3d-0000-0000-0000-000000000000
TotalSpace : 17179803648
FreeSpace  : 17084346368
Volumes    : {Microsoft.Windows.ServerBackup.Commands.WBVolume}
Properties : ValidTarget

create a backup target

PS C:Scripts> $tgt = New-WBBackupTarget -Disk $disk -Label "NewBackup"
PS C:Scripts> $tgt

Label                         WBDisk                        Path                                             TargetType
—–                         ——                        —-                                             ———-
NewBackup                     Microsoft.Windows.ServerBa…                                                      Volume

PS C:Scripts> $tgt | fl

Label      : NewBackup
WBDisk     : Microsoft.Windows.ServerBackup.Commands.WBDisk
Path       :
TargetType : Volume

Add it to the policy


PS C:Scripts> Add-WBBackupTarget -Policy $pol -Target $tgt

Label                         WBDisk                        Path                                             TargetType
—–                         ——                        —-                                             ———-
NewBackup                     Microsoft.Windows.ServerBa…                                                      Volume

get the volumes to backup


PS C:Scripts> Get-WBVolume -AllVolumes

VolumeLabel :
MountPath   : C:
MountPoint  : \?Volume{cb6fae01-a330-11dc-8e93-806e6f6e6963}
FileSystem  : NTFS
Property    : Critical, ValidSource
FreeSpace   : 58092306432
TotalSpace  : 68716331008

VolumeLabel : Backup
MountPath   : E:
MountPoint  : \?Volume{dae9090c-c06f-11dc-a79a-0003ff69ad11}
FileSystem  : NTFS
Property    : ValidSource
FreeSpace   : 17064423424
TotalSpace  : 17158832128

Add the source volume to the policy


PS C:Scripts> $vol = Get-WBVolume -AllVolumes | Where {$_.Mountpath -eq "C:"}
PS C:Scripts> Add-WBVolume -Policy $pol -Volume $vol

VolumeLabel :
MountPath   : C:
MountPoint  : \?Volume{cb6fae01-a330-11dc-8e93-806e6f6e6963}
FileSystem  : NTFS
Property    : Critical, ValidSource
FreeSpace   : 58092261376
TotalSpace  : 68716331008

set the backup schedule ie when it runs.  Remember that  PowerShell expects dates in US format

PS C:Scripts> $sched = [datetime]"01/11/2008 21:30:00"
PS C:Scripts> $sched

11 January 2008 21:30:00

PS C:Scripts> Set-WBSchedule -Policy $pol -Schedule $sched

11 January 2008 21:30:00

PS C:Scripts> $pol | fl

Schedule        : {11/01/2008 21:30:00}
BackupTargets   : {Microsoft.Windows.ServerBackup.Commands.WBBackupTarget}
VolumesToBackup : {Microsoft.Windows.ServerBackup.Commands.WBVolume}

make the policy active


PS C:Scripts> Set-WBPolicy -Policy $pol

Warning!
Do you want to format your target disk(s)?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y

Note that use of -Force will override the warning

The commands I have used here could be put into a script rather than being run as separate commands.

It works.  The backup starts as scheduled

PS C:Scripts> Get-WBSummary

NextBackupTime                  : 12/01/2008 21:30:00
NumberOfVersions                : 0
LastSuccessfulBackupTime        : 01/01/0001 00:00:00
LastSuccessfulBackupTargetPath  :
LastSuccessfulBackupTargetLabel :
LastBackupTime                  : 01/01/0001 00:00:00
LastBackupTarget                :
LastBackupResultHR              : 0
LastBackupResultDetailedHR      : 0
CurrentOperationStatus          : BackupInProgress

And when it is complete

PS C:Scripts> Get-WBSummary

NextBackupTime                  : 12/01/2008 21:30:00
NumberOfVersions                : 1
LastSuccessfulBackupTime        : 11/01/2008 21:30:04
LastSuccessfulBackupTargetPath  : \?Volume{dae90928-c06f-11dc-a79a-0003ff69ad11}
LastSuccessfulBackupTargetLabel : NewBackup
LastBackupTime                  : 11/01/2008 21:30:04
LastBackupTarget                : NewBackup
LastBackupResultHR              : 0
LastBackupResultDetailedHR      : 0
CurrentOperationStatus          : NoOperationInProgress

Sadly there doesn’t seem to be any way to start a backup from PowerShell.  It can be done from the GUI and wbadmin..  After the first backup only differentials are taken.  Backups are stored in vhd files.  It is not possible to use this to backup to tape.

Another possible use for this is to provide for a bare metal recovery system for servers using the Windows Recovery environment.

Good to see an additional piece of PowerShell functionality.  Pity it is so hidden and isn’t quite fully featured.

About these ads
This entry was posted in Powershell. Bookmark the permalink.

4 Responses to PowerShell for Windows Server Backup

  1. Chirag says:

    You can use all backup functionality using Powershell in 2008 R2.You can use Start-WBBackup to run AdhocBackup for a configured schedule .Also you can create a temporary policy and pass it to Start-WBBackup for a backup.

    • Tyson says:

      I am running into a problem where I have created WSB scripts using PowerShell on Server 2008 R2 that do not work on Plain Server 2008. Found this post in an effort to get around the problem but am losing hope that powershell can work as well as it does on R2 for this application.

  2. Pingback: Surprise | Richard Siddaway's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s