Automatically Restore Backup Files for SQL 2005

  • Comments posted to this topic are about the item Automatically Restore Backup Files for SQL 2005

    "-=Still Learning=-"

    Lester Policarpio

  • Tried this script on SQL 2008, added a few columns and it worked like a charm. We will be saving a lot of time.:D

  • I'm glad it worked for sql version 2008. The only difference is the schema of the tables in sql 2000, 2005 and 2008 🙂

    "-=Still Learning=-"

    Lester Policarpio

  • We braught in the following on a few parts to ensure that we limit the possiblilty duplicates of the .mdf, .ndf and .ldf physical names and database names are not used.

    Have you considered something like that?

    We have quite a few environments and this helped to set them apart.

    DECLARE @RestoreFrom varchar(20)

    SET @RestoreFrom = '_ServerName'

    SET @restoredb = 'RESTORE DATABASE '+(select top 1 DatabaseName+@RestoreFrom from header_lester)

    UPDATE migration_lester SET physicalname = left(Physicalname,charindex('.',Physicalname)-1)+ @RestoreFrom + Right(Physicalname,4)

  • Edited the listing of the path for the location of the .bak files to use

    SET @pathension = 'dir /OD /s /b '+@Path+'*.'+@Extension

    Allowing for all .bak files in subdirectories and a shorter directory listing format. Just easier when all my .bak files were sitting in their own folders 🙂

    Also had to remove the +@path in front of +@migrate in the 'Restoreheaderonly' statement as I was getting multiple '///' when the .bak file location was on the network.

    Currently using it to restore 37 databases though, so it works like a charm!

    Good job...

  • Glad it works 🙂

    "-=Still Learning=-"

    Lester Policarpio

  • I have a problem with running this script, it returns this error message:


    Msg 3201, Level 16, State 2, Line 1

    Cannot open backup device 'C:\folder\abase1.bak'. Operating system error 2(The system cannot find the file specified.).

    Msg 3013, Level 16, State 1, Line 1

    RESTORE HEADERONLY is terminating abnormally.


    I know it can't find abase1.bak because the real physical filename is database1.bak. Please, I'm not very good with SQL but I do have 150+ databases i want to import into MS SQL. Can anyone tell me where i need to look in the script to fix this up?

    Thanks in advance for all helpful hints / solutions.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply