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

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s