Windows Server Backup Notes

Windows Server Backup is a free, optional tool to create backups of volumes, both OS and Data, as separate backups. With a bit of scripting, you could create a reasonable backup solution if dedicate third party solutions are out of reach.

By default, wbadmin will create a folder called 'WindowsImageBackup' on the target path. When you can rename this folder, you will need to rename it back to its original name to perform a restore.

Installing wbadmin for Windows Server:

Open PowerShell with elevated privileges and run the following:

Import-Module Servermanager
Install-WindowsFeature Windows-Server-Backup

Here are some examples on using Windows Server Backup, wbadmin:

Creating a backup of a Hyper-V guest:

wbadmin start backup -backupTarget:E: -hyperv:"MyVM" -quiet

List backup versions:


wbadmin get versions

Get specific details of a particular backup. Read more here.


wbadmin get items -version:12/26/2018-11:19

Performing a restore of a Hyper-V guest;


wbadmin start recovery -version:12/26/2018-12:00 -itemType:hyperv -items:"MyVM" -backupTarget:E:

Example: PowerShell script to automate a VM guest backup on Hyper-V:

#Declare variables, req. Install-Module Posh-SSH and mc-wol.exe or alternative WOL utility
$NAS = "192.168.1.10"
$NASUser ="nasuser"
$NASPwd = ConvertTo-SecureString "blablablablablabla" -AsPlainText -Force
$NASCred = New-Object System.Management.Automation.PSCredential($NASUser, $NASPwd)
$NewPSDriveCred  = New-Object System.Management.Automation.PsCredential("$NAS\$NASUser", $NASPwd)
$NASMAC = "11:22:33:aa:bb:cc"
$BackupPath = "X:\Backups\"
$env:Path += ";C:\Scripts\SystemBackup"
$DateFolder = "$((Get-Date).ToString('yyyyMMdd'))"
Clear-Host
Write-Host ""
Write-Host "Waking up NAS..." -foreground yellow
Write-Host ""
mc-wol.exe $NASMAC
Clear-Host
Write-Host ""
Write-Host "Waiting for NAS..." -foreground yellow
Write-Host ""
#Adjust sleep timer to suit your NAS appliance
Start-Sleep 180
$Ping = Test-Connection -ComputerName $NAS -Count 1 -Quiet
If ($Ping -eq $false)
{
Clear-Host
Write-Host ""
Write-Host "NAS is offline, exiting..." -foreground red
Write-Host ""
Exit
}
Else
{
Clear-Host
Write-Host ""
Write-Host "Commencing VM Backup" -foreground yellow
Write-Host ""
}
#Run VM backups
Remove-Item -Recurse "E:\WindowsImageBackup"
wbadmin start backup -backupTarget:E: -hyperv:"somevm" -quiet
Move-Item -Path "E:\WindowsImageBackup" -Destination "E:\$DateFolder"
New-PSDrive –Name "X" -PSProvider FileSystem -Root "\\$NAS\share" -Credential $NewPSDriveCred
#Retains 7 days of backups, purging the rest
Get-ChildItem "$BackupPath" | Where-Object {$_.CreationTime -le (Get-Date).AddDays(-7)} | Foreach-Object {Remove-Item $_.FullName -Recurse -Verbose -Force} 
Copy-Item "E:\$DateFolder\" -Destination "$BackupPath" -Recurse
Remove-PSDrive -name "X" -Force
#Shutting down NAS
New-SSHSession -ComputerName $NAS -Credential $NASCred
Invoke-SSHCommand -Index 0 -Command "shutdown -p now"
Remove-SSHSession -Index 0 -Verbose
#Tidy up local backup
Move-Item -Path "E:\$DateFolder" -Destination "E:\WindowsImageBackup"
Clear-Host
Write-Host ""
Write-Host "VM Backup is Complete!" -foreground green
Write-Host ""
Exit