Skip to content

Instantly share code, notes, and snippets.

@andrewthauer
Created March 7, 2014 16:31
Show Gist options
  • Save andrewthauer/9414764 to your computer and use it in GitHub Desktop.
Save andrewthauer/9414764 to your computer and use it in GitHub Desktop.
Backup an MS SQL database using using a timestamp
DECLARE @dbName nvarchar(max) = '<db_name>'
DECLARE @path nvarchar(max) = '<destination_path>'
DECLARE @date datetime = GETDATE()
DECLARE @timeStamp nvarchar(max) = CONVERT(nvarchar(max), @date, 112) + '-' + REPLACE(CONVERT(nvarchar(max), @date, 108), ':', '')
DECLARE @fileName nvarchar(max) = @path + @dbName + '_' + @timeStamp + '.bak'
BACKUP DATABASE @dbName TO DISK = @fileName
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment