Skip to content

Instantly share code, notes, and snippets.

@matejskubic
Created June 1, 2023 14:02
Show Gist options
  • Save matejskubic/ef8dd6f1573f65aa101fc371910464eb to your computer and use it in GitHub Desktop.
Save matejskubic/ef8dd6f1573f65aa101fc371910464eb to your computer and use it in GitHub Desktop.
SQL backup restore to Azure Blob Storage
CREATE CREDENTIAL [https://_name_.blob.core.windows.net/backup] WITH IDENTITY='Shared Access Signature', SECRET='sp=racwdl&st=2023-06-01T12:17:10Z&se=2024-01-01T21:17:10Z&spr=https&sv=2022-11-02&sr=c&sig=doW...3D'
DECLARE @dbToBackup as sysname = 'DB'
DECLARE @folder as nvarchar(50) = 'shrinked'
DECLARE @file as nvarchar(100) = @dbToBackup + N'_' + FORMAT(GETDATE(), N'yyyy-MM-dd_hhmmss') + N'.bak'
DECLARE @destUrl as nvarchar(500) = N'https://_name_.blob.core.windows.net/backup/' + @folder + N'/' + @file
PRINT @destUrl
BACKUP DATABASE @dbToBackup TO
URL = @destUrl
WITH NOFORMAT, NOINIT, NAME = N'Full Database Backup', NOSKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 5
RESTORE DATABASE [_new_db_] FROM URL = N'https://_name_.blob.core.windows.net/be-backup/shrinked/DB_2023-06-01_125836.bak'
WITH FILE = 1,
MOVE N'AXDBBuild_Data' TO N'C:\MSSQL_DATA\DB.mdf',
MOVE N'AXDBBuild_Log' TO N'C:\MSSQL_LOGS\DB_Log.ldf',
NOUNLOAD, STATS = 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment