Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell restore SQL Server database to new database

I have a database $CurrentDB and I want to restore a backup of $CurrentDB to $NewDB. The T-SQL command looks like this:

USE [master]

ALTER DATABASE [NewDB] 
    SET SINGLE_USER WITH ROLLBACK IMMEDIATE

RESTORE DATABASE [NewDB] 
FROM DISK = N'D:\Backups\CurrentDB.bak' 
WITH FILE = 1,  
     MOVE N'CurrentDB' TO N'D:\Databases\NewDB.mdf',  
     MOVE N'CurrentDB_log' TO N'D:\Logs\NewDB_log.ldf',  
     NOUNLOAD, REPLACE, STATS = 5

ALTER DATABASE [NewDB] 
    SET MULTI_USER
GO

I am attempting to user Restore-SqlDatabase but I don't know how to properly -RelocateFile

$CurrentDB = "CurrentDB"
$NewDB = "NewDB"
$NewDBmdf = "NewDB.mdf"
$CurrentDBlog = "CurrentDB_log"
$NewDBldf = "NewDB_log.ldf"
$backupfile = $CurrentDB + "ToNewDB.bak"

$RelocateData = New-Object 
Microsoft.SqlServer.Management.Smo.RelocateFile($CurrentDB, $NewDBmdf)

$RelocateLog = New-Object 
Microsoft.SqlServer.Management.Smo.RelocateFile($CurrentDBlog, $NewDBldf)

Restore-SqlDatabase -ServerInstance $SQLServer -Database $NewDB -BackupFile 
$backupfile -ReplaceDatabase -NoRecovery -RelocateFile @($RelocateData, 
$RelocateLog)

I can't seem to locate an example of what I am attempting to do. I have seen plenty of examples of restoring databases with the same name but different files. I want a different name and different file names. I am open to suggestions.

like image 912
workinonit530 Avatar asked Aug 18 '17 20:08

workinonit530


People also ask

How do I restore a SQL Server database to a new database?

Connect to the appropriate instance of the SQL Server Database Engine, and then in Object Explorer, select the server name to expand the server tree. Right-click Databases, and then select Restore Database. The Restore Database dialog box opens. Select the database to restore from the drop-down list.

How do you restore a .BAK file to a new database?

Restore the database from a BAK fileRight-click on the database server in the left navigation pane, click Tasks, click Restore. The name of the restoring database appears in the To database list box. To create a new database, enter its name in the list box. Select 'From device'.


1 Answers

You don't have to use SMO just because your're in PowerShell.

import-module sqlps

$database = "NewDb"
$backupLocation = "D:\Backups\CurrentDB.bak"
$dataFileLocation = "D:\Databases\NewDB.mdf"
$logFileLocation = "D:\Logs\NewDB_log.ldf"

$sql = @"

USE [master]

ALTER DATABASE [$database] 
    SET SINGLE_USER WITH ROLLBACK IMMEDIATE

RESTORE DATABASE [$database] 
FROM DISK = N'$backupLocation' 
WITH FILE = 1,  
     MOVE N'CurrentDB' TO N'$dataFileLocation',  
     MOVE N'CurrentDB_log' TO N'$logFileLocation',  
     NOUNLOAD, REPLACE, STATS = 5

ALTER DATABASE [$database] 
    SET MULTI_USER
"@

invoke-sqlcmd $sql

And if you don't have sqlps installed, you can use System.Data.SqlClient from Powershell to run TSQL.

like image 111
David Browne - Microsoft Avatar answered Sep 18 '22 12:09

David Browne - Microsoft