Windows Server Backup with PowerShell
February 3rd, 2011 by charlie and tagged Backup, PowerShell, Server
Windows Server 2008 R2 added some useful enhancements to the PowerShell snapin that was in Windows Server 2008. But it’s still a pain to use, and a bit hard to get your head around. Here’s a basic PowerShell script to do a backup. There are enough comments in there that it should help you get started.
# —————————————————————–
# Script to set a Windows Server Backup policy for a server
#
# Assumes: Volumes C: and D: to backup.
# : Target – \storageserverserverbackup
# : Exclusions – D:temp, D:Users*.MP3, D:Users*.WMA
# : VSS Mode – Full Backup
# : System State – True
# : Bare Metal Recovery – True
# : Schedule – 12:30 PM, 11:00 PM
# Requires: Elevated PowerShell
# : (Must be run As Administrator)
#
# Planned Improvements: [minor] Check for elevated priv, error politely if not
# : [minor] Check for Server Backup PSSnapin before trying to load
# : [major] Accept cmdline parameters
#
# ModHist: 1/31/11 – initial (Charlie Russel)
# :
#
# With profound thanks to Richard Siddaway, Windows PowerShell MVP
# —————————————————————–
# By default, the PSSnapin isn”t loaded automatically, so first load it.
Add-PSSnapin Windows.ServerBackup # will error if already loaded, but continue
# First, create a new empty policy
# Alternately, you can open the existing policy in Edit mode with
# $BackupPolicy = Get-WBPolicy -edit
$BackupPolicy = New-WBPolicy
# Now,define the parts of it.
# First,let”s do the “source” volumes that will be part of the backup.
# This requires us to first get a list of them, and then parse that list
# to add the ones we want (C: and D:)
# We don”t actually need C:, since we”ll get that as part of Bare Metal Restore,
# but we include it anyway for completeness
$volC = Get-WBVolume -AllVolumes | Where {$_.MountPath -eq “C:”}
$volD = Get-WBVolume -AllVolumes | Where {$_.MountPath -eq “D:”}
$Volumes = $volC,$volD
# Now, add those volumes to the blank policy.
Add-WBVolume -policy $BackupPolicy -volume $Volumes
# If you want to do the entire volume, you don”t need to define any exclusions,
# but if you want to exclude any files or folders, you need to define that exclusion
# and add that to the backup policy you”re building. First, define it.
$excTEMP = New-WBFileSpec -Filespec D:Temp -exclude
$excWMA = New-WBFileSpec -Filespec “D:Users*.wma” -exclude # Recursive unless
$excMP3 = New-WBFileSpec -Filespec “D:Users*.mp3” -exclude # -NonRecursive specified
$FileExclusions = $excTEMP,$excWMA,$excMP3
# and then add that to the policy we”re building
Add-WBFileSpec -policy $BackupPolicy -filespec $FileExclusions
# Define the backup target, this time as a network share.
# For backup to share, you need to create a credential to connect to the remote share.
# This wouldn”t be required for specifying a removable disk.
# You can specify the username here (DOMAINUser) but will be prompted for password
$Cred = Get-Credential exampleuser
# Now, define the target
$Target = New-WBBackupTarget -NetworkPath \StorageServerServerBackup -Credential $Cred
# Add the target to the policy
Add-WBBackupTarget -policy $BackupPolicy -target $Target
# Define the schedule
$sch1 = [datetime]”01/31/2011 12:30:00″
$sch2 = [datetime]”01/31/2011 21:00:00″
Set-WBSchedule -policy $BackupPolicy -schedule $sch1,$sch2
# Set for system state and for bare metal recovery
Add-WBSystemState -policy $BackupPolicy
Add-WBBareMetalRecovery -policy $BackupPolicy
# Finally, set for full VSS Backup
Set-WBVssBackupOptions -policy $BackupPolicy -VssFullBackup
# Finally, we need to SET the policy before it actually takes control
Set-WBPolicy -force -policy $BackupPolicy
# This completes the configuration of the SBS server backup policy
$Server = (hostname).tolower()
” The server $Server now has the following backup configuration: “
“”
Get-WBPolicy
Posted in PowerShell, Windows Server | 4 Comments »
May 4th, 2011 at 7:21 pm
I am trying to update a single element of a scheduled backup. Do I have to rebuild the entire thing just to change one element?
This does not work:
Add-PSSnapin Windows.ServerBackup
$BackupPolicy = Get-WBPolicy -Editable
$Target = New-WBBackupTarget -VolumePath V:
Add-WBBackupTarget -policy $BackupPolicy -target $Target
Set-WBPolicy -policy $BackupPolicy
If it makes any difference, V: is a virtual hard drive that I rebuild each week.
May 9th, 2011 at 2:12 pm
You need to Get-WBBackupTarget, then remove the old one, create a new one, and add it. Finally, if you don’t need to change anything else, then you should be able to Set the new policy with the changed WBBackupTarget as you’ve done.
May 25th, 2011 at 7:22 pm
Hello
I would like to create a script that triggers “Windows Server Backup” to run according to parameters. I am currently doing this in Windows 2003 with VBScript and a BKS file on the server and using NTBackup. Do you think there is a way to do this in 2008 (not R2) using powershell / VBScript ?
June 21st, 2011 at 9:10 am
Probably, but with limited options. Backup in SBS 2003 is not PowerShell aware.