Windows Server 2008 backup with on-board tools and 7z

A server-based data backup stores copies of important data, runs automatically, manages multiple file versions and ensures the integrity of the stored data. Microsoft Windows Server 2008 and Windows Server 2008 R2 already have a large number of these features on board. The missing parts are provided by the open source compression software 7z.

The key to a robust server backup is a copy of files in a consistent state. Since Windows Server 2003 and Windows Vista, Microsoft has built in the Volume Shadow Copy Service (VSS). This service creates a copy of files on a drive in spite of open files in a consistent state. As a result open files can be copied without stopping any service. This is achieved by providers for Microsoft SQL Server and Exchange which make sure that database files in shadow copies are consistent on database, Exchange and Small Business Servers.

Windows Server 2008 backup with on-board tools and 7z
Windows Server 2008 backup with on-board tools and 7z

Windows Server 2008 and 2008 R2 deliver a command-line tool called Diskshadow that can be used to manage shadow copies. Thanks to the commitment of
Igor Pavlov 7z is a free data compression program with a 30-70% better compression rate compared to Zip archives. Using these tools and some batch files you can build an automated backup solution for Windows servers.

The method requires the installation of 7z and Windows Server 2008 or later. In the example presented here, a daily backup is demonstrated. All batch files can be downloaded from the downloads section.

In the following the different parts of the batch scripts are explained in more detail. Essentially the backup consists of a backup batch file, the scripts for then DISKSHADOW command and a 7z batch file:

The backup batch file „Daily.cmd“

The batch file Daily.cmd is the starting point for data backup and can, for example, executed by the task scheduler of windows. At the beginning of the batch all variables for the backup are defined:

::Backup Parameter
set Folder=Daily
set ShadowVolume=D:
set ShadowAlias=Q:
set ShadowCommand=Daily7z.cmd
VariableDescription
%Folder%Directory where the archives of the backup will be stored. In the example a relative path is used but an absolute path works as well.
%ShadowVolume%Drive on which the data to be backuped is stored. Currently only one drive is used, since on a server all data resides on a single drive normally.
%ShadowAlias%Drive letter under which the shadow copy is made available. This has to be an unused drive letter in the server.
%ShadowCommand%This is the batch file that does the actual compressing of the data with 7z.

In the next step a simple log file is created in which the starting time and ending time of the backup is written:

::Backup Skript
echo Backup %1 started > %Folder%\Backup.log
date /t >> %Folder%\Backup.log
time /t >> %Folder%\Backup.log
echo. >> %Folder%\Backup.log

The actual backup happens in the next step:

::Create archives
DISKSHADOW /s ShadowRemove.dsh
DISKSHADOW /s Shadow.dsh >> %Folder%\Backup.log
del Backup.cab

Here the DISKSHADOW command is executed with two script files using the „/s“ parameter. The script files are simple text files having the extension „.dsh“ for identification. The first script „ShadowRemove.dsh“ removes a potentially remaining shadow copy. This can occur from an aborted backup and should be removed before the next backup. This method is necesary for a more robust and fault tolerant backup.

In the second DISKSHADOW command the „Shadow.dsh“ script creates a shadow copy of the %ShadowVolume% drive, mounts the shadow copy as drive %ShadowAlias% and copies files from that drive to the folder %Folder% with the batch file %ShadowCommand%. All outputs of the DISKSHADOW command are written to the log file.

At this time, the archive and log file have the name „Backup.*“. Thus, it makes sense to make an additional copy of the backup to another medium at this point. This optional copy is used to recover data after a simultaneous total failure of server and backup system (disaster recovery). In any case, this second backup copy should be stored separately from the daily backup at another location. In addition, the most recent backup minimizes the loss of data in the case of a disaster recovery.

In this example, the backup files are copied to a spatially separated NAS System:

:: Copy archives to network share (optional)
ren \\Server\Share\Backup\Backup.7z.* Backup.old.7z.*
xcopy %Folder%\Backup.7z.*\\Server\Share\Backup\Backup.7z.* /c /y >> %Folder%\Backup.log
del /q \\Server\Share\Backup\Backup.old.7z.*

Firstly, an existing backup is renamed to „.old.7z.„, then the new backup is copied, and finally the old backup is deleted. This method has the advantage of not losing a backup copy even in the event of an error.

In the next step, the archive files are renamed according to the given name. If the script has been started with no parameters this step is omitted:

::Rename archives
IF "%1" == "" GOTO NORENAME
del /q %Folder%\%1.7z.*
ren %Folder%\Backup.7z.* %1.7z.*
:NORENAME

Finally, the end time of the backup is written to the log file, and the log file is also renamed according to the given name:

echo. >> %Folder%\Backup.log
date /t >> %Folder%\Backup.log
time /t >> %Folder%\Backup.log
echo Backup %1 finished >> %Folder%\Backup.log

::Rename log
IF "%1" == "" GOTO NORENAMELOG
del /q %Folder%\%1.log
ren %Folder%\Backup.log %1.log
:NORENAMELOG

The DISKSHADOW script „Shadow.dsh“

The DISKSHADOW script „Shadow.dsh“ creates with the DISKSHADOW tool a shadow copy, makes the shadow copy as a drive available, executes the backup batch and removes the shadow copy afterwards:

#DiskShadow script file
set context persistent
set metadata Backup.cab
set verbose on
begin backup
add volume %ShadowVolume% alias DataVolumeShadow
create
expose %DataVolumeShadow% %ShadowAlias%
exec %ShadowCommand%
delete shadows id %DataVolumeShadow%
end backup
#End of script

The command „begin backup“ starts the backup process, with „add volume“ the volume %ShadowVolume% to shadow is specified and „create“ creates the shadow copy. During this, all providers like SQL Server and Exchange are instructed to write their files in a consistent state to the shadow copy. The ShadowID of the shadow copy is temporarily stored in the variable „DataVolumeShadow“ for later use.

By „expose“ the shadow copy is mounted to the drive letter %ShadowAlias%.

The next step DISKSHADOW script calls another batch file %ShadowCommand% with the „exec“ command. This compresses and copies all files from the shadow copy in a 7z archive.

The remaining commands in the script are for cleaning up by deleting the shadow copy and finishing the backup. A detailed description of the DISKSHADOW tool can be found in Microsoft’s Technet.

The DISKSHADOW script „ShadowRemove.dsh“

Das Skript entfernt eine mögliche noch vorhandene Schattenkopie und besteht aus dem Aufräum-Schritt des „Shadow.dsh“ Skriptes:

The script removes a possible remaining shadow copy and consists of the clean-up step of the „Shadow.dsh“ script:

#DiskShadow script file
delete shadows exposed %ShadowAlias%
#End of script

In %ShadowAlias% the drive letter of the shadow copy is defined.

Archiving with 7z in „Daily7z.cmd“

7z archives the files from the shadow copy with the „Daily7z.cmd“ batch file from %ShadowCommand% :

@echo off
"C:\Program Files\7-Zip\7z.exe" a -v2g %Folder%\Backup.7z @Daily.lst
exit 0

In the use command line all directories from the „@Daily.lst“ file are saved in the multi-part archive „Backup.7z“. The size of the archive parts is limited to two gigabyte. Thus, the individual parts of the archive can be easily stored even on FAT drives. The file „Daily.lst“ is a simple text file containing all directories to be saved separated by line breaks:

Q:\Folder1
Q:\Folder2
Q:\Folder3
Q:\Folder...

Calling „Daily.cmd“ manually

The batch file can be invoked manually from the command line and takes as a parameter the name of the backup like the weekday at which the backup is executed:

Daily "Monday"

If no parameters are passed, a backup with the file name „Backup.*“ is created.

Automation of the backup with the task scheduler

Windows Server 2008 provides with the task scheduler an efficient mechanism for periodic execution of processes:

Task Scheduler
Task Scheduler
Action of the Task Scheduler
Action of the Task Scheduler

In this example for each day of the week a task is created with an action that starts the batch file „Daily.cmd“ with the corresponding weekday as parameter at a given time.

You should be aware that depending on the amount of data and processor power, the backup can take a considerable time and utilizes the server cpu strongly. Therefore, the backup should be made during times when the server is not used like during night times.

Discussion

The presented method has been proven for a long time in practice. Nevertheless, there are some points that can be improved: Firstly, incremental backups are difficult to realize with this method. One possibility would be to use Robocopy instead of 7z.

Secondly, the backup method does not check whether the backup files are identical to the original files. This is not a large drawback as the intregrity of the 7z archive ensures a correct backup.

Downloads

Downloads You can download all batch files as a zip archive here:

Backup7z.zip1,4 KB All batch files  und scripts.

Further information