Schedule a daily backup with SQL Server Express

As you know MS SQL Server Express Edition hasn’t Agent Service, which can be used to schedule daily backups or other periodic administrative tasks. But you can use for this standard Windows Schedule tool.

SQL Server Express Edition has some limitation, but anyway it is very powerful tool for storing data. For most little or middle projects you can use it without any restrictions. But Express edition has a little problem, it hasn’t SQL Server Agent. But you can use SQLCMD command line tool and standard Scheduled Tasks for Windows instead of it. If you want to set automating administrative periodic task first of all you need to write a sql script. We want to solve problem: write a script which will do a daily backup. Of course you can use MS Management Studio for generating current script (as you know you can download Management Studio Express Edition too from Microsoft site): just click “Script Actions to…” instead of OK button at “Back Up Database” dialog box and you will get a script.

For daily backups I usually use this script:

DECLARE @pathName NVARCHAR(512) 
SET @pathName = 'D:\Backup\db_backup_' + Convert(varchar(8), GETDATE(), 112) + '.bak' 
BACKUP DATABASE [MyDataBase] TO  DISK = @pathName WITH NOFORMAT, NOINIT,  NAME = N'db_backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10

This script makes a backup file with name db_backup_YYYYDDMM.bak where YYYYDDMM – it is current date. This format will give you an opportunity to have every day backup at separate files. Correct this script and try to run it, check that you will get a backup file (I set “d:\backup” folder for files, change it if you want). Next step – save this script to file schedule.sql and put it to “c:\scheduled tasks\” (you can choose any other names for script file and folders, just make sure that you will change it at all places where it will be used). At this folder create a file backup.bat with next contain:

sqlcmd -S SEVERNAME -U UserName -P Password -i schedule.sql
7z a -tzip D:\Backup \db_backup_%date%.zip -i! D:\Backup\db_backup_*.bak
del d:\Backup\db_backup_*.bak

Where: SERVERNAME – database instance name (server name), UserName – sql user, which has a privileges for making backups, Password – password of this user, schedule.sql – name of script with we created on previous step. Second and third lines of this bat script do an archive of this backup file and then delete backup file. I use 7z (http://www.7-zip.org/) utility for this, if you want you can use any other archive tool, or maybe leave backup file unzipped (MS SQL Server 2008 and higher has an opportunity to backup database with compression, so you can just set it in sql script). If you will use 7z like me you need to set full path to 7z.exe tool or you can just copy 7z.exe and 7z.dll files from installed folder of 7z tool to folder where will be located this bat script. And make sure that if you used other folder names or file names at previous steps you will change these names at this bat script too. You can try to run this bat script and check that backup file will be created and zipped.

Last step is to create schedule task. In each Windows versions it can be different way to create this task. This is how to create schedule task for Windows XP - http://support.microsoft.com/kb/308569, this is link how to for Windows 7 - http://windows.microsoft.com/en-US/windows7/schedule-a-task. For other versions I think you can find a how-to at Windows helps or other blog posts.

When you will create this Windows task you should check that user by whom these tasks will be launched has rights to create files at folders, where script will put backups, and this user has right to execute bat file.

Like this you can run not only daily backup. You can execute some procedures, re-index tables or maybe some other administrative stuff – just place what you want at schedule.sql script.

Comments (13)

Radu-Mihail ( ) #
gravatar
Excellent tip, thank you very much!
Dinesh ( ) #
gravatar
Good Tip..
shane carvalho ( ) #
gravatar
nice.
erblina ( ) #
gravatar
Sorry, I used it and when I double click on batch file everything is OK, but when the betch file is executed from scheduled task it dosen't work but it appears "sqlcmd: 'schedule.sql' Invalid File Name "

i copied file name to be ensure that it is not wrong but ... (Windows 7 OS) Could u help me?
Denis Gladkikh ( ) #
gravatar
erblina, I know that you solved your problem, but I will write solution here just for others.

The solution was: Need to set full path to file schedule.sql.
Deepak ( ) #
gravatar
Thanks this help me lot...

But be aware of this that, If your Sql file is located in different location then we should provide full path. like

sqlcmd -S (Local) -U UserName -P Password -i C:\FolderName\BackupFile.sql

otherwise it will not work
Aamir ( ) #
gravatar
I am getting error here when the file is going to archieve. please helo mesqlcmd: 'a' Invalid File Name
Denis Gladkikh ( ) #
gravatar
Aamir, could you show the whole command which you are using?
Ken ( ) #
gravatar
Hi Denis,

I am following your script and it backups the data. However, the bat file is not working...meaning the backups are not being created by the batch file...Do I need to have SQL Agent running for this to work? I am using the Express edition. Can you please help? My script is at the bottom.

sqlcmd -S FILESERVER\SQLEXPRESS -U someserver\thomask -P abc1234 -i C:\ScheduledTasks\Scripts\Backuptestdb.sql
Denis Gladkikh ( ) #
gravatar
Hi Ken,

If you have latest version of SQL Server Express, than why you need the batch file? You can schedule a task in SQL Agent.
Ken ( ) #
gravatar
Hi Dennis,

That was basically my predicament as well...When I installed SQL Server Express edition 2012, it also installed SQL Express Agent. I was a bit confused...Has Microsoft included Agent with 2012 onwards? However, I can't seem to get the Agent started. It fails upon startup...

"SQL Server Agent service on Local Computer started and then stopped" message. Can you please advice...Thanks!
Denis Gladkikh ( ) #
gravatar
Ken, my suggestion is to take a look in windows event viewer, other than that I cannot help you.
SATISH RANA ( ) #
gravatar
YOU CAN SCHEDULE BACKUP IN SQL 2008 VIA STEP BE STEP THIS LINK

http://www.configureserver.blogspot.in/#!http://configureserver.blogspot.com/2013/01/schedule-backup-in-sql-server-2008.html
Submit Comment
If you want to get notifications about new comments at this topic, please fill email text box and check proper item. If you want to place source code in comment body place it in tags [code]...[/code], you can set language like this [code cs]...[/code], where cs can be cs, html, xml, java, js, php, sql, cpp, css.

 

busy