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)
So if I want to restore for example DC1 from Fri 01-20-2012 for example, I need to do the following steps:
- Copy or move the folder "DC1 - Fri 01-20-2012" back to WindowsImageBackup
- Rename the copy to read "DC1" by removing the date component
- Perform the restore
7 comments:
thanks a lot! about to try this and it looks like just what i wanted! I am going to find out what all these switches do first though as I want to have a regular schedule of system state and then a once weekly bare metal:
wbadmin start backup -allCritical -systemState -vssFull -backupTarget:%target% -user:myuser -password:mypassword -include:d: -quiet
You can use delage32.exe for the cleanup task
That's a nifty tool, but meantime I've evolved the batch file to remove older files on it's own...
for /f "skip=7 delims=" %%i in ('dir /b /ad /o-d %target%%computername%*') do rd /s /q "%target%%%i"
This will keep newest 7 backups (skip=7). However, I keep all the logs, i do not delete them, but few more similar lines could do this clearing...
Hi,
Great script - Where would I put the line above to keep only the last 7 backups?
Thanks
Here is what I currently use now:
@echo off
setlocal
set currentdate=%date:~-4,4%-%date:~4,2%-%date:~7,2%
set target=\\master\backups\
set user=username
set pass=password
set log=%target%backup log %computername% %currentdate%.txt
set backuptarget=%target%WindowsImageBackup\%computername%
set desiredtarget=%target%%computername% - %currentdate%
(
echo Started at %date% %time%.
echo.
wbadmin start backup -allCritical -systemState -vssFull -backupTarget:%target% -user:%user% -password:%pass% -include:d: -quiet
move "%backuptarget%" "%desiredtarget%"
echo.
for /f "skip=7 delims=" %%i in ('dir /b /ad /o-d %target%%computername%*') do rd /s /q "%target%%%i"
for /f "skip=21 delims=" %%i in ('dir /b /a-d /o-d "%target%backup log %computername%*"') do del "%target%%%i"
) >>"%log%"
echo Ended at %date% %time%.>>"%log%"
endlocal
This will keep the last 7 backups and the last 21 backup logs.
what does the option -include:d: does?
once i include the option i get the error
"Include in backup drive D:". If you do not have the drive D: you can skip it.
Post a Comment