Simple SQL Server backup/restore

Posted by Joe Enos on April 26, 2021 · 1 min read
-- Back Up ---------------------------------------------

declare @filename nvarchar(255) = 'C:\backups\'
+ format(sysutcdatetime(), 'yyyyMMddHHmmss') + '.bak';

backup database MyDB to disk = @filename;
go

-- Restore ---------------------------------------------

-- Find the logical names - result will be the MDF and LDF
-- logical names:
restore filelistonly
from disk = 'C:\backups\20201103130743.bak';
go

restore database DifferentDB
from disk = 'C:\backups\20201103130743.bak'
with replace,
move '**MDF Logical Name**' to 'c:\data\DifferentDB.mdf',
move '**LDF Logical Name**' to 'c:\data\DifferentDB_log.ldf';
go