Sunday, March 2, 2008

Scheduling database backup in SQL 2005 Express Edition

I have been recently visited client who uses SQL Server 2005 Express Edition for his business. As you know that edition is supplied without SQL Server Agent (which was in MSDE). So,the question is how to schedule backup of the database in that case? I would like to thank to Jasper Smith, who wrote these greate articles

Automating Database maintenance in SQL 2005 Express Edition Part I
http://www.sqldbatips.com/showarticle.asp?ID=27

Automating Database maintenance in SQL 2005 Express Edition Part II
http://www.sqldbatips.com/showarticle.asp?ID=29


CREATE PROCEDURE usp_BackupDatabases
@dbname SYSNAME,
@backup_path VARCHAR(100)
AS
DECLARE @dt VARCHAR(20) ---date of db backup
DECLARE @FileName VARCHAR(50) ---build path and dbname
DECLARE @sql VARCHAR(100) ---build final sql statement

SET @dt =CONVERT(VARCHAR(15),GETDATE(),112)
SELECT @FileName = @backup_path+@dbname + @dt+'.bak'

SET @sql = N'BACKUP DATABASE [' + @dbname + '] TO DISK = ''' + @FileName + ''''
EXEC(@sql)


The above is simple stored procedure that accepts two parameters and performs BACKUP of the database.Now that, we have created stored procedure lets put the call of the sp into a text file ( I use userdbfullbackup.sql) and place the file on filesystem.
usp_BackupDatabases @dbname='$(DB)',@backup_path='$(BACKUPFOLDER)'


SQLCMD utility 'knows' to accept parameters (-v),so we can easily provide parameters.

--Usage
EXEC master..xp_cmdshell 'SQLCMD -S URID\SQLSERVERDEV2005 -i"c:\userdbfullbackup.sql" -v DB="Northwind" -v BACKUPFOLDER="c:\"'


But wait , till now we just written a stored procedure that backups our databases and tested it , so what is about scheduling?

Windows Servers/XP have Scheduled Tasks Wizard
You can refernce to below KB
http://support.microsoft.com/default.aspx?scid=kb;en-us;308569&sd=tech

Please see Jasper's explanation ( do not want repetition).
http://www.sqldbatips.com/showarticle.asp?ID=27

1 comment:

vedro said...

Hello

I am a student at Faculty of Electrical Engineering and Computer Science in Maribor (Slovenia).
I am studying Automation
I am working on a project based on SQL 2005 Express Edition with SCADA as foreground control system.
I am new at SQL programing so I would ask for your help. I found your blog post for Scheduling database backup in SQL 2005 Express Edition and helped me a little to understand the procedure.
But I am still quite "in the dark" with my issues.
What I have to do is:
I have one database in which are 2 tables: Data and Alarms. Now, another database (backup) has to be created and table Data needs to be copied (data) into it at the first day of the every month.
For example, at May 01, at 0:00 table Data will be copied into the backup database and will be named Data_april_09. So this would be a monthly backup of the table Data.
Table Data from the original database has to be deleted after the backup proces.
The same procedure has to be with the table Alarms but every new year (so, once yearly). So the backuped Alarms database would be called Alarms_09.
Here also table Alarms from the original database has to be deleted after the backup proces.
Now, since those two pocedures will be runned from SCADA (actual schedule will be maintained) I need a script (maybe 2 - one for monthly and one for a yearly backup), that will be runned from it.

I have found SQL commads (not all) that I think are apropriate for my problem, but I do not not know how to put them into one usefull piece of code:

To copy a table from one database to another I need the command:

SELECT * INTO backUP_database_name.dbo.BackUP_table_name FROM Database.dbo.Original_table_name

Then to delete data from the original table when the data is copied to backup:

TRUNCATE table Original_table_name

The backup database has to be created manualy or it can be created sheduled every year?

I would be very grateful if you would help me with my issue. I have found SQL as very usefull tool in my future business and career.

Have a nice day,
Vedran Budinski