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

Comments

Popular posts from this blog

C# Record Serialization

Add timestamp to photo using ImageMagick

Read/write large blob to SQL Server from C#