February 3, 2019 at 2:15 pm
Hello everyone
I am looking to generate a restoration script for hundreds of databases with their backup Full and log file Trn
I found it on the internet but it is applied qe for a single database
https://www.mssqltips.com/sqlservertip/1584/auto-generate-sql-server-restore-script-from-backup-files-in-a-directory/
thank you for your help
February 3, 2019 at 2:50 pm
You mean this script in the same set of articles?
https://www.mssqltips.com/sqlservertip/1243/auto-generate-sql-server-database-restore-scripts/
February 3, 2019 at 3:05 pm
abdalah.mehdoini - Sunday, February 3, 2019 2:15 PMHello everyone
I am looking to generate a restoration script for hundreds of databases with their backup Full and log file Trn
I found it on the internet but it is applied qe for a single database
https://www.mssqltips.com/sqlservertip/1584/auto-generate-sql-server-restore-script-from-backup-files-in-a-directory/
thank you for your help
All those scripts and the related ones there are for a single database. Take a look at this article and stored procedure that can do all user databases - it might meet your needs:
T-SQL Restore Script Generator
Sue
February 3, 2019 at 4:39 pm
Heh... and all of those scripts appear to be using MSDB, which does you no good at all if the original server(s) where the backups were taken from isn't available.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 4, 2019 at 6:27 am
Who has an idea or feedback on the sp_RestoreGene
function
February 4, 2019 at 6:31 am
abdalah.mehdoini - Monday, February 4, 2019 6:27 AMWho has an idea or feedback on thesp_RestoreGene
function
What's the link for it? (I'm not going to click on every link in this thread to try to find it). And, considering the sp_ prefix, it's probably not a function.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 4, 2019 at 9:31 am
Jeff Moden - Monday, February 4, 2019 6:31 AMabdalah.mehdoini - Monday, February 4, 2019 6:27 AMWho has an idea or feedback on thesp_RestoreGene
functionWhat's the link for it? (I'm not going to click on every link in this thread to try to find it). And, considering the sp_ prefix, it's probably not a function.
February 4, 2019 at 6:43 pm
abdalah.mehdoini - Monday, February 4, 2019 9:31 AMJeff Moden - Monday, February 4, 2019 6:31 AMabdalah.mehdoini - Monday, February 4, 2019 6:27 AMWho has an idea or feedback on thesp_RestoreGene
functionWhat's the link for it? (I'm not going to click on every link in this thread to try to find it). And, considering the sp_ prefix, it's probably not a function.
Like I said earlier in this thread, if you're restoring to the same server and the MSDB database is intact as it was before the restore became a necessity, that might work fine because the msdb.dbo.backupset table is used (as is the backupmediafamily table) in that. He also says as much in the Flower Box Comments in the code. If all you have is a pile of backup files in a folder, it's not going to do a thing for you.
Other than that, it looks pretty good from a 60,000 foot level. I've not done a deep dive on it.
Of course, if you have MSDB available for the server you're doing the backups for, you can just as easily generate the code for a point in time recovery using the GUID. The only time you'd need the code is if you needed to automate the restore of a database every night.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 4, 2019 at 6:54 pm
The real key is... why do you need to "generate a restoration script for hundreds of databases with their backup Full and log file"??? What's the end game here??? And, how often do you need to do it? Using a script like this to do the job may be absolutely the wrong thing to do depending on what the ultimate goal is. For example, if you're trying to copy all the databases from a prod system to a lesser environment, a "SAN Snapshot" would do the whole shebang in just seconds or possibly less.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 26, 2025 at 6:44 pm
Find Below/attached script to restore multiple backup files in single shot as per existing Database data and log file location.
Use master
go
DECLARE @date CHAR(8)
SET @date = (SELECT CONVERT(char(8), GETDATE(), 112))
DECLARE @path VARCHAR(125)
SET @path = '\\TestServer\Backup' -- Provide the backup path here
;WITH MoveCmdCTE ( DatabaseName, MoveCmd )
AS ( SELECT DISTINCT
DB_NAME(database_id) ,
STUFF((SELECT ' ' + CHAR(13)+', MOVE ''' + name + ''''
+ CASE Type
WHEN 0 THEN ' TO ''M:\SQLData\' -- Provide SQL Data File Path here
ELSE ' TO ''N:\SQLLogs\' ---- Provide SQL Log File Path here
END
+ REVERSE(LEFT(REVERSE(physical_name),
CHARINDEX('\',
REVERSE(physical_name),
1) - 1)) + ''''
FROM sys.master_files sm1
WHERE sm1.database_id = sm2.database_ID
FOR XML PATH('') ,
TYPE).value('.', 'varchar(max)'), 1, 1, '') AS MoveCmd
FROM sys.master_files sm2
)
SELECT 'Alter database ['+ name+'] set Single_User with rollback immediate;'+
' RESTORE DATABASE ['+ name + '] FROM DISK = ''' + @path + '' + name + '.bak'' WITH FILE = 1 '+movecmdCTE.MoveCmd
+', RECOVERY, REPLACE, STATS=5,buffercount=600;'+
'Alter database ['+ name+'] set Multi_User ;'
FROM sys.databases d
INNER JOIN MoveCMDCTE ON d.name = movecmdcte.databasename
WHERE d.name not in ('master','model','msdb','tempdb','distribution') --LIKE '%DatabaseName%'
GO
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy