Simple SQL Server backup/restore

-- Back Up ---------------------------------------------

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

backup database MyDB to disk = @filename WITH INIT;

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

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

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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s