Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How-to: Quick & Dirty SQL Express scheduled backup
Posted

1 solution

Step1:
Open SQL Management Studio and copy the following SQL script i created earlier into a new query window.

USE [master]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

create PROCEDURE [dbo].[sp_BackupDb]
@dbName sysname, @backupTypeToRun CHAR(1)
AS
BEGIN
SET NOCOUNT ON;

DECLARE @sqlCommand NVARCHAR(1000)
DECLARE @dateTime NVARCHAR(20)

SELECT @dateTime = REPLACE(CONVERT(VARCHAR, GETDATE(),111),'/','-') +'-' +
REPLACE(CONVERT(VARCHAR, GETDATE(),108),':','')

DECLARE @databaseFileName NVARCHAR(200)
SET @databaseFileName = replace(@dbName,']','')
SET @databaseFileName = replace(@databaseFileName,'[','')

IF @backupTypeToRun = 'F'
SET @sqlCommand = 'BACKUP DATABASE ' + @dbName +
' TO DISK = ''C:\DbBackups\' + @databaseFileName + '_Full_' + @dateTime + '.BAK'''

IF @backupTypeToRun = 'D'
SET @sqlCommand = 'BACKUP DATABASE ' + @dbName +
' TO DISK = ''C:\DbBackups\' + @databaseFileName + '_Diff_' + @dateTime + '.BAK'' WITH DIFFERENTIAL'

IF @backupTypeToRun = 'L'
SET @sqlCommand = 'BACKUP LOG ' + @dbName +
' TO DISK = ''C:\DbBackups\' + @databaseFileName + '_Log_' + @dateTime + '.TRN'''

EXECUTE sp_executesql @sqlCommand
END

Step2:
Step 2: In a text editor create a batch file that is named sqlbackup.bat and then copy the text from one of the following examples depending on your scenario into that file:
ex:sqlcmd -S .\SQLEXPRESS -E -Q "use master; EXEC dbo.sp_BackupDb @dbName='sample',@backupTypeToRun='F'"

Step3:
Add that batch file to Create basic task wizard(task schedular)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900