magritton
6/16/2014 - 6:52 PM

The collects all of the DBs in the old server instance and generates a restore script. I used this when moving DBs

The collects all of the DBs in the old server instance and generates a restore script. I used this when moving DBs

$line = ""
$line = "USE [master]`r`n"
Get-ChildItem "C:\DBBackup\DBBackup" -Filter *.BAK | 
Foreach-Object {
    $name = $_.Name.Replace(".BAK","")

    $line += "RESTORE DATABASE [" + $name + "_2010] FROM  DISK = N'" + $_.FullName
    $line += "' WITH  FILE = 1,  MOVE N'" + $name + "'"
    $line += " TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\"
    $line += $name + "_2010.mdf',  MOVE N'" + $name
    $line += "_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\"
    $line += $name + "_2010_log.LDF',  NOUNLOAD,  STATS = 5`r`n"
    $line | out-file c:\temp\temp.txt
   #$_.FullName
}
USE [master]
RESTORE DATABASE [Bdc_Service_DB_6358c473529d48338f0dc0b9720d347e_2010] FROM  DISK = N'C:\DBBackup\DBBackup\Bdc_Service_DB_6358c473529d48338f0dc0b9720d347e.BAK' WITH  FILE = 1,  MOVE N'Bdc_Service_DB_6358c473529d48338f0dc0b9720d347e' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Bdc_Service_DB_6358c473529d48338f0dc0b9720d347e_2010.mdf',  MOVE N'Bdc_Service_DB_6358c473529d48338f0dc0b9720d347e_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Bdc_Service_DB_6358c473529d48338f0dc0b9720d347e_2010_log.LDF',  NOUNLOAD,  STATS = 5
SELECT --name,
'RESTORE DATABASE [' + name + '] FROM  DISK = N''G:\MSSQL11.SP2013\MSSQL\Backup\' + name + '.BAK'' WITH  FILE = 1,  NOUNLOAD,  STATS = 5'
FROM master..sysdatabases
WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb');