Simple SQL Server backup/restore

-- 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

Leave a Reply

Your email address will not be published.