Automate systemstate backup using Windows Server Backup feature

I was recently tasked with automating the backup of the systemstate of  several Windows 2008 R2 domain controllers using the Windows Server Backup feature.

Firstly, the Windows Server Backup feature is required to be installed;

1) Run Windows Powershell

2) From the command line type the below to import the Server Manager module into the current session;

Import-Module ServerManager

3) From the command line type the below to install the Windows Server Backup Features

Add-WindowsFeature Backup-Features -IncludeAllSubFeature

The backup of the systemstate can be invoked manually from the command line (with elevated privelages) as follows;

wbadmin start systemstatebackup -BackupTarget:<backup volume>

However, this process was then required to be automated, one of the limitations of the Windows Server Backup feature is that the backup target is required to be a local volume, the automation process requires the backup file to be archived to a destination folder based on the geographical location of the domain controller and retain the backup file for a period of seven days.

So firstly, the script will need to set the destination location for the backup file to be created, the requirement is based on geographical location, this was relatively easy for me to obtain due to the server naming scheme uses the fourth character to reference the geographical location so the substring method will allow me to set the destination folder,also  in this step any previous system state backups are deleted from the local volume;

# Retrieves the computername of the local host.
$Computer = gc env:computername

# Deletes previous system state backup.
Get-Item (“B:\WindowsImageBackup\” + $Computer + “\Backup*”) | Remove-Item

# Extracts the fourth character of the computername to determine the geograhpical location. 

$Location = $Computer.Substring(3,1)

# Sets the destination location to archive the systemstate backup based on the geographical location of the server
If ($Location -eq “x”){$Destination = “\\<server>\<folder>\SystemState\” + $Computer + “\”}
ElseIf ($Location -eq “y”) {$Destination = “\\<server>\<folder>\SystemState\” + $Computer + “\”}

Now we will need to invoke the systemstate backup using the following command;

# Performs system state backup in quiet mode.
wbadmin start systemstatebackup -BackupTarget:<backup volume> -quiet

Once the systemstate backup is complete we will now need to archive the local backup file to the destination folder which is set based on the geographical location of the domain controller;

# Copies the backup folder to destination location specified to archvice the systemstate backup.
$Source = Get-Item (“B:\WindowsImageBackup\” + $Computer + “\Backup*”)
Copy-Item $Source.fullname -Destination ($Destination + “\” + $Source.name) -Recurse

Finally we need to apply the retention period on the destination folder, where in this case is 7 days

#Deletes backup folder items older than 7 days.
Get-ChildItem $Destination | where {$_.Lastwritetime -lt (date).adddays(-7)} | Remove-Item

The powershell script may then be invoked from Task Scheduler to automate the process.

The powershell script  may be downloaded from the below:

http://www.box.com/embed/tzdznohy3px1t63.swf


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 )

Connecting to %s