Skip to content

Instantly share code, notes, and snippets.

@juanonsoftware
Last active April 12, 2018 09:52
Show Gist options
  • Save juanonsoftware/44fd8ce4211643f8c5132ac7b985be59 to your computer and use it in GitHub Desktop.
Save juanonsoftware/44fd8ce4211643f8c5132ac7b985be59 to your computer and use it in GitHub Desktop.
Script to backup an SQL Server database
-- NOTE
-- Must update dbName & Folder
declare @dbName nvarchar(150);
declare @folder nvarchar(100);
set @dbName = 'TestDB';
set @folder = 'D:\Mega\Wip\SQL\TestDB\Backup\';
declare @path nvarchar(255);
declare @dateInfo nvarchar(50);
-- https://msdn.microsoft.com/en-us/library/ms186862.aspx
set @dateInfo = REPLACE(convert(nvarchar, getdate(), 20), ':', '');
set @dateInfo = REPLACE(@dateInfo, '-', '');
set @dateInfo = REPLACE(@dateInfo, ' ', '-');
set @path = @folder + @dbName + '-' + @dateInfo + '.bak';
print 'Backup path: ' + @path;
-- https://msdn.microsoft.com/en-us/library/ms187510.aspx
backup database @dbName
to disk = @path
WITH
DIFFERENTIAL
--COMPRESSION
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment