How to create differential backup?

Using the following T-SQL one can create differential backup or make it part of scheduled job.

declare @datetimestamp varchar(20)
declare @filename nvarchar(100)
declare @name nvarchar(100)

SELECT @datetimestamp = REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(20), GETDATE(), 120), '-', ''), ' ', '_'), ':', '')

SELECT @filename = 'C:\testDBBackup\testDBbdiff' + @datetimestamp + '.bak'
SELECT @name = 'testDB Diff Backup ' + @datetimestamp

BACKUP DATABASE testDB
TO DISK = @filename
WITH DIFFERENTIAL,INIT, NAME = @name

Leave a Reply