Friday, January 20, 2012

Backup Windows Server 2008 R2 to a Network Share and Retain Previous Backups

If you ever tried to configure the new Windows Server Backup (WSB) to make backups of your server on a network share, you'll be notified that only the last backup will be retained and the previous one will be deleted. Period. There is nothing you can do about this... or is it?

While everyone is shouting at Microsoft that they can't create a proper backup system, I have to disagree. The new WBS which replaced NTBackup is far more powerful than its predecessor. I admit, it has its own limitations and flaws, nothing is perfect though. There are indeed technical restrictions with the current design that prevent the new system to keep multiple backups. Maybe a future version will improve this.

Let's us go back to our problem... WSB is doing a nice backup job to a network share and it is able to restore it using the installation DVD in case of a major failure directly from the network share, if you backed up the system state of course. So here we have to be creative in order to keep multiple backups o a regular schedule... Let's unleash the power of the command line interface to WSB...

Create a batch file with the following content:

@echo off
setlocal
set currentdate=%date:/=-%
set target=\\backupserver\backups\
set log=%target%backup log %computername% %currentdate%.txt
set errorlog=%target%backup error log %computername% %currentdate%.txt
set backuptarget=%target%WindowsImageBackup\%computername%
set desiredtarget=%target%%computername% - %currentdate%
(
 wbadmin start backup -allCritical -systemState -vssFull -backupTarget:%target% -user:myuser -password:mypassword -include:d: -quiet
 move "%backuptarget%" "%desiredtarget%"
) 1>>"%log%" 2>>"%errorlog%"
endlocal

So what is this code do? First of all it sets up the variables needed then invokes the backup operation. After the backup is completed the backup folder is taken out of the original location and renamed to contain the backup date.

You will have to adjust the highlighted values to suit your own environment. Schedule this batch file as you wish using Windows Task Scheduler and you're done. Since you do the scheduling, you will also bypass the limitation of the WBS scheduling which is only once per day, every day. Note though that since I do not added the time to the folder name this batch file cannot be scheduled to run more than one time per day. However, you can schedule it to run only on weekends if you wish...

All you have to do now is to watch the backup server for disk space and delete old, unneeded backups.

To restore a given version, you will have to copy the day backup back into the WindowsImageBackup folder and remove the date. I did not renamed the WindowsImageBackup folder itself because to this location I backup multiple servers.

So here is the file/folder structure after a backup:

DC1 - Fri 01-20-2012\
DC2 - Fri 01-20-2012\
WindowsImageBackup\


backup error log DC1 Fri 01-20-2012.txt
backup error log DC2 Fri 01-20-2012.txt
backup log DC1 Fri 01-20-2012.txt
backup log DC2 Fri 01-20-2012.txt

As you can see the following are created for each server (names are DC1 and DC2 here):

  • The backup folder for each server with the added date
  • An error log file (all errors will be dumped here)
  • A log file of the backup (all non-error messages will be dumped here)
(I've listed one day only for two servers, but the full picture will contain multiple days of course)

So if I want to restore for example DC1 from Fri 01-20-2012 for example, I need to do the following steps:
  1. Copy or move the folder "DC1 - Fri 01-20-2012" back to WindowsImageBackup
  2. Rename the copy to read "DC1" by removing the date component
  3. Perform the restore
Hope this helps you.