Assuming that you have a backup file from the "X_ENTWICKLUNG_DM" database from the same server, and you want restore it back into another database. If the value from the $X_DBName
is the same as the original database, that will be the reason for the exception you are facing.
This will restore the database into the default paths for DATA and LOG, without you having to know about them upfront:
Restore-DbaDatabase -SqlInstance $Instance -Path $BackupPath -DatabaseName $NEWDBNAME -ReplaceDbNameInFile -WithReplace
The $NEWDBNAME
variable has to be something else than the original database you took the backup from and are now trying to restore back in.
If you want the logical file names to be name like the name of the new database you restored into, you should do something like this:
Rename-DbaDatabase -SqlInstance . -Database $NEWDBNAME -LogicalName "<DBN>_<FT>"
Update
I just created an new database on my lab server. I named it TEST. The database is empty, but it serves the purpose of testing my code.
I took a full backup of the database with this command:
Backup-DbaDatabase -SqlInstance localhost -Database Test -BackupDirectory C:\Temp\ -CopyOnly -Type full -CompressBackup
After the creation of the backup file, I tested the content of the backup and explicit the filelist, to see what default names the backup would suggest when wanting to restore the backup file:
Get-DbaBackupInformation -SqlInstance localhost -Path C:\Temp\Test_201812072244.bak | Format-list filelist
FileList : {
@{Type=D; LogicalName=Test; PhysicalName=D:\MSSQL\DATA\Test.mdf},
@{Type=L; LogicalName=Test_log; PhysicalName=D:\MSSQL\LOG\Test_log.ldf}
}
After the confirmation about the default file list from the backup file, I ran the below code to generate the raw T-SQL script that the module is going to execute:
Restore-DbaDatabase -SqlInstance localhost -DatabaseName Test_Restored -Path C:\Temp\Test_201812072244.bak -WithReplace -ReplaceDbNameInFile -OutputScriptOnly
RESTORE DATABASE [Test_Restored] FROM DISK = N'C:\Temp\Test_201812072244.bak' WITH FILE = 1, MOVE N'Test' TO N'D:\MSSQL\DATA\Test_Restored.mdf', MOVE N'Test_log' TO 'D:\MSSQL\LOG\Test_Restored_log.ldf', NOUNLOAD, REPLACE, STATS = 10
The -OutputScriptOnly parameter to the Restore-DbaDatabase cmdlet will not execute the script, only generate it and output it to your console.
Please note that the file names it is going to restore is actually renamed to be the names of the new database that I'm trying to restore into.
I would recommend that you create an empty database and play around with that, until your commands executes and behaves as expected. I would recommend that you run an update, just in case.
Update-Module dbatools -Force
While you troubleshoot, please try and execute the Get-DbaBackupInformation cmdlet to help you investigate the issue you're facing.