06 September, 2010

SQL Express (MSDE) automatic backup script

Hello all,
You may have instances of SQL Express or in it's previus version MSDE due to small applications or just to save some moeny.
The main problem is to back them up in a central location and get alerts if something goes wrong.

My script is using the following external tools:
1. osql.exe - just install studio express
2. mailit.exe - a command line mailing program, part of NUTS suite.

The things you need to do before:
1. make sure that the sql instance supports integrated security (http://msdn.microsoft.com/en-us/library/ms188670.aspx)
2. document the server names and instance name
3. create a shared central location for the backup destination (security of the directory should include the computer account that you are backing up with write permissions)
4. create a sql backup script for every DB you want to backup (replace the bold with your data):
==============================
BACKUP DATABASE [DBNAME] TO  DISK = N'\\server\share$\Filename.bak' WITH NOFORMAT, NOINIT, NAME = N'BESMGMT-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO
==============================


The script:
in this example I backup 3 DB's on 3 deferent servers.
replace the BOLD with your data
==============================
set backupexec=yes
set epo=yes
set bes=yes

osql -S servername\bkupexec -E -i D:\Work\DB_Backup\Scripts\BackupexecDB.sql -o D:\Work\DB_Backup\Logs\backupexec.log

find "BACKUP DATABASE successfully processed" D:\Work\DB_Backup\Logs\backupexec.log
if %errorlevel% NEQ 0 (set Backupexec=no)

osql -S servername\eposerver -E -i D:\Work\DB_Backup\Scripts\ePO.sql -o D:\Work\DB_Backup\Logs\ePO.log

find "BACKUP DATABASE successfully processed" D:\Work\DB_Backup\Logs\ePO.log
if %errorlevel% NEQ 0 (set epo=no)

osql -S servername\besmgmt -E -i D:\Work\DB_Backup\Scripts\BES.sql -o D:\Work\DB_Backup\Logs\BES.log

find "BACKUP DATABASE successfully processed" D:\Work\DB_Backup\Logs\BES.log
if %errorlevel% NEQ 0 (set bes=no)

set body=
if "%backupexec%"=="no" (set body=BackupExec)
if "%epo%"=="no" (set body=%body%, ePO)
if "%bes%"=="no" (set body=%body%, BES)
set att=
if "%backupexec%"=="no" (set att=D:\Work\DB_Backup\Logs\backupexec.log)
if "%epo%"=="no" (set att=%att%,D:\Work\DB_Backup\Logs\ePO.log)
if "%bes%"=="no" (set att=%att%,D:\Work\DB_Backup\Logs\BES.log)
if "%body%"=="" goto end

rem === mailit parameters ===
SET MAILIT_SERVER=mailserver
SET MAILIT_FROM=db_bk@yourdomain.local
SET MAILIT_TO=your-email@yourdomain.local
SET MAILIT_SUBJ=Error in DB Backup (%body%)
SET MAILIT_MSGTEXT=There are errors in DB backup of: %body%
SET MAILIT_ATTACH=%att%
D:\Work\DB_Backup\MAILIT.exe

:end
=============================

Enjoy

No comments:

Post a Comment