SQL Server Nation
For all of your SQL Server needs.
How to Automate SQL Server Express Backups

SQL Server Express is great for small business with small data requirements.  It is essentially a scaled down version of SQL Server that is free.  However, because it is throttled down there are some features that are missing...one such feature is the SQL Server Agent.  This service is responsible for automating SQL Server tasks, such as automatically backing up your databases.  While there are plenty of tools out there that you can use to automate the backup of your SQL Server database....why pay for them?  Today I am going to show you how to do it with the tools that are available with your windows operating system.

The first step in this process is the TSQL code to actually do the database backup. In my example I want to take a full backup everyday with a different file name appended to the end of the file path.  In this example, my database is named Express and I am backing the files up to a folder path on the E drive of a server named db1.

 

Here's the code from the above screenshot.  You can copy and paste this code into a new query editor window in SQL Server Management Studio.

--FullBackup.sql

DECLARE @Path NVARCHAR(1000), @FileName NVARCHAR(255), @FullPath NVARCHAR(1255)
SET @Path = '\\db1\E$\SQLServer\2008\Backups\'
SET @FileName = 'Express_' + REPLACE(CONVERT(VARCHAR(10), GETDATE(), 101), '/', '') + '.bak'
SET @FullPath = @Path + @FileName

BACKUP DATABASE Express
TO DISK = @FullPath
WITH INIT

 

I'll need to save this file because I will reference it later when I create a Windows Scheduled Task.  To save the file go to the File menu in SQL Server Management Studio and choose Save SQLQuery1.sql As...

For this example, I want to save the file to the E:\SQLExpressBackup\FullBackup.sql.  This file is stored on a server named db2.

 

Now I need a way to automate the running of the above script that I created.  If I had the SQL Server Agent service available, I could create a new Job and post the code in it.  Since I do not have this luxury, I'll need an alternate method.  This is where SQLCMD comes into play. 

SQLCMD is a command line utility that you can use to connect to SQL Server instances and issue commands against the service.  You can do pretty much everything in it as you can in SQL Server Management Studio, but without all of the bells and whistles.  In the screenshot and script below, I am creating a batch file on the db2 machine which connects to my db2\SQL2008 instance using Windows Authentication and issues the backup file from above against the server.

Here's the code snippet from the batch file.

sqlcmd -S db2\SQL2008 -u -i E:\SQLExpressBackup\FullBackup.sql

Now I just need to save my batch file.

 

OK, I am done w/ the portion that does the work of backing up the SQL Server database.  All that is left to do is to setup a Windows Scheduled task that will execute the batch file once a night to run the script which will create the backup file. 

You can find the Scheduled Tasks folder under the Accessories/SystemTools folder in your Start Menu.  Create a new task and give it a name.

 

The next step is to set when you want the task to run.  I want my full backs to run Daily.

 

I then need to set when I want the task to run.  Since there is very little load on my system around midnight every night, this seems like the optimal time to create the backup.

I'll want to start a new program when it runs, in this case a batch file.

 

I can then select the location of the batch file that I want to execute.  Remember, this batch file contains the SQLCMD code that connects to my SQL Server instance  and runs the script which will backup my database.

 

When I save the task, it will ask me for a user name and password for which I want the task to run under.  I just need to enter my credentials and I am set.

 

All Done

Well, that is really all there is to it.  Even though SQL Server Express doesn't come with a native tool to automate the backup of SQL Server databases, it is easily accomplished with some simple TSQL code and a Windows Scheduled Task.  I hope you find this SQL Express tip handy!

Tim

 


Posted 23 Feb 2009 8:09 PM by TimChapman

Comments

Frink wrote re: How to Automate SQL Server Express Backups
on 24 Aug 2009 10:12 AM

Cheers for that Tim.

Do you know what files don't need backing up if I do this?

I use Backup Exec to backup my hard drives, but it was failing as a number of files were in use (eg. .mdf, .ldf etc.) Presumably your solution above backs up the data, but there are still SQL Server files to back up?

The folders I have in C:\Program Files\Microsoft SQL Server\ are:

80, 90, MSSQL, MSSQL.1\MSSQL, MSSQL.2\MSSQL and MSSQL.3\MSSQL

TIA,

- Frink

TimChapman wrote re: How to Automate SQL Server Express Backups
on 24 Aug 2009 1:41 PM

The only files you really need to back up are the ones w/ extensions .mdf, .ldf, or .ndf.  If there are other large files..investigate.  They may have custom extensions as sql data or log files.

Dodger wrote re: How to Automate SQL Server Express Backups
on 5 Feb 2010 4:37 PM

Hey Tim

Please could you help me - I ran my backup script in Management Express and it worked. I then tried kicking it off using the batch file like you suggested, it opens sqlcmd briefly and then closes again without kicking off my backup script. I am running Windows Server 2008 (.Net) and SQL Express 2008.

I have tried the following text in the batch file

1) sqlcmd -S .net\SQLEXPRESS -u -i C:\GIS\Definition queries&SQL\BackupSQLdatabase.sql

2)sqlcmd -S .net\SQL2008 -u -i C:\GIS\Definition queries&SQL\BackupSQLdatabase.sql

3) sqlcmd -S -u -i C:\GIS\Definition queries&SQL\BackupSQLdatabase.sql

Im an amateur at this so hopefully you can spot my problem out.

Many thanks

TimChapman wrote re: How to Automate SQL Server Express Backups
on 9 Feb 2010 10:08 PM

Hey,

My guess is that somehow your batch file isn't invoking sqlcmd correctly.  Can you verify that your batch file actually starts the sqlcmd command?

Tim

BrandonGalderisi wrote re: How to Automate SQL Server Express Backups
on 11 Feb 2010 8:56 PM

-S must be immediately followed by the server name.  In the first, you have .net\sqlexpress.  .net is not a valid machine name so the instance names you are specifying are not valid. Also, & to the command line is a command separator.  Meaning that it will take what is before the & and treat it as one command and what is after as another.  If you have a file path with spaces, you must wrap it in quotes.  Try the below with the correct machine name instead of MACHINENAME.

sqlcmd -S MACHINENAME\SQLEXPRESS -u -i "C:\GIS\Definition queries&SQL\BackupSQLdatabase.sql"

Dodger wrote re: How to Automate SQL Server Express Backups
on 18 Feb 2010 11:20 AM

Tim / Brandon apologies for the late response. I naively expected an email to alert me of replies, I will be more attentive.

Firstly to Tim, yes after changing my batch file command as Brandon suggested (thanks) it did open SQLCMD but the SQLCMD window opens displaying the batch file command however the backup script is not executed, no backup was processed. To test the backup script I opened SQL express and manually executed it with no problems.. any suggestions?

Thanks

Dodger wrote re: How to Automate SQL Server Express Backups
on 18 Feb 2010 11:23 AM

@ Tim apologies, please disregard my last comment, it worked successfully!! thanks for the help guys, great job

makan007 wrote re: How to Automate SQL Server Express Backups
on 1 Apr 2010 11:32 PM

How to edit the given script to append or backup the transaction logs as well? TIA.

TimChapman wrote re: How to Automate SQL Server Express Backups
on 4 Apr 2010 9:46 AM

Hi makan007,

You can use this script for tran log backups:

DECLARE @Path NVARCHAR(1000), @FileName NVARCHAR(255), @FullPath NVARCHAR(1255)

SET @Path = '\\db1\E$\SQLServer\2008\Backups\LogBackup\'

SET @FileName = 'Express_Log_' + REPLACE(CONVERT(VARCHAR(10), GETDATE(), 101), '/', '') + '.trn'

SET @FullPath = @Path + @FileName

BACKUP LOG Express

TO DISK = @FullPath

WITH INIT

Copyright SQL Server Nation 2010
Powered by Community Server (Non-Commercial Edition), by Telligent Systems