Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « When Books On Line is not really correctDealing with the could not allocate new page for database 'TEMPDB'. There are no more pages available in filegroup DEFAULT error message »
    comments

    If you've ever had to restore log files and not use the restore to a point in time option, I'm sure you found it to be a pain executing all the restore statements.

    A good tip when you have to do this is to use vbscript or some other scripting technology to create your own .sql file that you can just execute and restore the .trn files

    So for example, I use Quest Litespeed for compression (see here for more info). This means I have a few procedures at my disposal in which I can use to perform my restores from compressed backups. So given a restore script for Litespeed like this

    1. exec master.dbo.xp_restore_log @database = N'dbname' ,
    2. @filename = N'path\backup.trn',
    3. @filenumber = 1,
    4. @with = N'STATS = 10',
    5. @with = N'NORECOVERY',
    6. @with = N'MOVE N''file.mdf'' TO path\file.mdf''',
    7. @with = N'MOVE N''log.ldf'' TO path\log.ldf''',
    8. @affinity = 0,
    9. @logging = 0
    10. GO

    we can take this code and with a little scripting generate a the call on each .trn file found in a particular folder

    1. Dim fso, folder, files, results,source  
    2. Dim db
    3.  
    4. Set fso = CreateObject("Scripting.FileSystemObject")  
    5. source = Wscript.Arguments.Item(0)  
    6. db = Wscript.Arguments.Item(1)  
    7.  
    8.  
    9. If source = "" Or db = "" Then      
    10.     Wscript.Echo "Check your source or database value passed please and try again"      
    11.     Wscript.Quit  
    12. End If  
    13. Set results = fso.CreateTextFile("RestoreScript.sql", True)  
    14. Set folder = fso.GetFolder(source)  
    15. Set files = folder.Files    
    16.  
    17. For each f In files  
    18.      If InStr(f.Name,".trn") > 0 Then
    19.     results.WriteLine("exec master.dbo.xp_restore_log @database = N'" & db & "' , " & _
    20.             "@filename = N'" & f.Path & "', " & _
    21.             "@filenumber = 1, " & _
    22.             "@with = N'STATS = 10', " & _
    23.             "@with = N'NORECOVERY', " & _
    24.             "@with = N'MOVE N''file.mdf'' TO N''path\file.mdf''', " & _
    25.             "@with = N'MOVE N''log.ldf'' TO N''path\log.ldf''', " & _
    26.             "@affinity = 0, " & _
    27.             "@logging = 0 " & _
    28.             VbCrLf & "GO")
    29.      End If
    30. Next  
    31.  
    32. results.Close

    Now just run that and you'll get your .sql you can execute and be done with it. I usually only do this on special occasions so there aren't parameters like date range and such passed but you could easily add those in and only grab a certain listing of files.

    About the Author

    Ted Krueger is a SQL Server MVP and has been working in development and database administration for 13+ years. Specialties range from High Availability and Disaster / Recovery setup and testing methods down to custom assembly development for SQL Server Reporting Services. Ted blogs and is also one of the founders of LessThanDot.com technology community. Some of the articles focused on are Backup / Recovery, Security, SSIS and working on SQL Server and using all of the SQL Server features available to create stable and scalable database services. @onpnt Personal Blog over at http://onpnt.wordpress.com/
    Social SitingsTwitterLinkedInLTD RSS Feed
    3065 views
    InstapaperVote on HN

    1 comment

    Comment from: Philip Leighton [Visitor]
    Philip Leighton Thanks for this.

    I have used your code to help build .sql files for contingency recovery purposes.

    Thanks !

    09/29/11 @ 09:06

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)