Results 1 to 8 of 8

Thread: [RESOLVED] SQL Server 2005 Auto Restore

  1. #1

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Resolved [RESOLVED] SQL Server 2005 Auto Restore


    I know it sounds like a bad title but here is what I want to do:

    I have backups running of various SQL Server (actually LiteSpeed backups but not important for this point). The backups are named DBName_date_time.bak and stored on a server.

    I want to find the lastest backup of the database (full each night) and apply that to another server (a reporting and test server). I understand how to call the LiteSpeed restore xp_ proc and what goes inside it.

    How do I get the latest file for each database using T-SQL.

    Gary
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server 2005 Auto Restore

    The backups - if done with a mainteance plan - are stored in some tracking table. Have you tried to find this system table with backup history info?

    I'm 95% sure of this...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    Hyperactive Member
    Join Date
    Jan 2008
    Location
    Merseyside
    Posts
    456

    Re: SQL Server 2005 Auto Restore

    (info below and link might be useful)

    Tracking Tables

    Several tables exist in the msdb database that track the backup operations which occurred on the server. These tables include:

    backupfile - Contains one row for each data or log file that is backed up
    backupmediafamily - Contains one row for each media family
    backupmediaset - Contains one row for each backup media set
    backupset- Contains a row for each backup set

    http://vyaskn.tripod.com/backup_and_..._litespeed.htm
    Last edited by kevchadders; Feb 6th, 2009 at 10:25 AM.

  4. #4

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL Server 2005 Auto Restore

    In the env I'm in I'm not sure I get the luxury of looking back the database where the backups occurred from.

    I'm thinking of using xp_cmdShell to perform a Dir operation and storing the results to a table then querying the table for the latest date.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL Server 2005 Auto Restore

    Here is what I came up with so far:

    sql Code:
    1. CREATE PROCEDURE dbo.MyDdRestore
    2. (
    3.     @path varchar(2000),
    4.     @Supress int = 0,
    5.     @DBName varchar(100),
    6.     @PhysicalDataName Varchar(100),
    7.     @PhysicalLogName Varchar(100),
    8.     @NewPathDataFile Varchar(1000),
    9.     @NewPathLogFile Varchar(1000)
    10. )
    11. AS
    12.  
    13. SET NOCOUNT ON
    14. EXEC sp_configure 'show advanced options', 1
    15. -- To update the currently configured value for advanced options.
    16. RECONFIGURE
    17. -- To enable the feature.
    18. EXEC sp_configure 'xp_cmdshell', 1
    19. -- To update the currently configured value for this feature.
    20. RECONFIGURE
    21.  
    22. TRUNCATE TABLE Directory_Contents_Stage
    23. TRUNCATE TABLE Directory_Contents
    24.  
    25.     DECLARE @cmd varchar(4000)
    26.     IF Right(@path,7) <> '\*.bak'
    27.         BEGIN
    28.             Set @path = @path + '\*.bak'
    29.         END
    30.  
    31.      SELECT @cmd = 'Dir "' + @path + '"'
    32.      --print @cmd
    33. INSERT INTO Directory_Contents_Stage(dir_output) EXEC master..xp_cmdshell @cmd
    34.  
    35.   IF EXISTS (SELECT * FROM Directory_Contents_Stage
    36.        WHERE dir_output = 'The system cannot find the file specified.')
    37.     BEGIN
    38.          PRINT 'The system cannot find the file specified.'
    39.          EXEC sp_configure 'show advanced options', 1
    40.          -- To update the currently configured value for advanced options.
    41.          RECONFIGURE
    42.          -- To enable the feature.
    43.          EXEC sp_configure 'xp_cmdshell', 0
    44.          -- To update the currently configured value for this feature.
    45.          RECONFIGURE
    46.  
    47.          RETURN
    48.     END
    49.  
    50.   IF EXISTS (SELECT * FROM Directory_Contents_Stage
    51.        WHERE dir_output = 'File Not Found')
    52.     BEGIN
    53.          PRINT 'File Not Found'
    54.          EXEC sp_configure 'show advanced options', 1
    55.         -- To update the currently configured value for advanced options.
    56.         RECONFIGURE
    57.         -- To enable the feature.
    58.         EXEC sp_configure 'xp_cmdshell', 0
    59.         -- To update the currently configured value for this feature.
    60.         RECONFIGURE
    61.         RETURN
    62.     END        
    63.  
    64.   IF EXISTS (SELECT * FROM Directory_Contents_Stage
    65.        WHERE dir_output = 'The system cannot find the path specified.')
    66.     BEGIN
    67.          PRINT 'The system cannot find the path specified.'
    68.          EXEC sp_configure 'show advanced options', 1
    69.          -- To update the currently configured value for advanced options.
    70.          RECONFIGURE
    71.          -- To enable the feature.
    72.          EXEC sp_configure 'xp_cmdshell', 0
    73.          -- To update the currently configured value for this feature.
    74.          RECONFIGURE
    75.          RETURN
    76.     END
    77.  
    78.      UPDATE Directory_Contents_Stage SET [dir] = @path
    79.  
    80. DELETE FROM Directory_Contents WHERE [dir] = @path
    81.  
    82. INSERT INTO Directory_Contents (Create_Time, File_Size, [File_Name], [dir], [Struct_Type])
    83.      SELECT   CONVERT(datetime,SUBSTRING(dir_output,1,20)) AS [Create_Time]
    84.      , CONVERT(bigint,LTRIM(RTRIM(REPLACE(SUBSTRING(dir_output,21,19),',','')))) AS [File_Size]
    85.      , SUBSTRING(dir_output,40,(LEN(dir_output)-39)) AS [File_Name]
    86.      , [dir]
    87.      , 'FILE'
    88.       FROM  Directory_Contents_Stage
    89.      WHERE  SUBSTRING(dir_output,1,1) <> ' '
    90.        AND (SUBSTRING(dir_output,1,1) <> ' '
    91.        AND  SUBSTRING(dir_output,25,5) <> CHAR(60)+'Dir'+CHAR(62))
    92.  
    93.  
    94. INSERT INTO Directory_Contents (Create_Time, [File_Name], [dir], [Struct_Type])
    95.      SELECT   CONVERT(datetime,SUBSTRING(dir_output,1,20)) AS [Create_Time]
    96.      , SUBSTRING(dir_output,40,(LEN(dir_output)-39)) As [File_Name]
    97.      , [dir]
    98.      , CHAR(60)+'Dir'+CHAR(62) AS  [Struct_Type]
    99.        FROM  Directory_Contents_Stage
    100.       WHERE  SUBSTRING(dir_output,25,5) = CHAR(60)+'Dir'+CHAR(62)
    101. EXEC sp_configure 'show advanced options', 1
    102. -- To update the currently configured value for advanced options.
    103. RECONFIGURE
    104. -- To enable the feature.
    105. EXEC sp_configure 'xp_cmdshell', 0
    106. -- To update the currently configured value for this feature.
    107. RECONFIGURE
    108.  
    109. IF @Supress = 0
    110.  
    111.   DECLARE c1 CURSOR FOR   Select File_name From Directory_Contents Where Create_time In (
    112.                             select max(create_Time)From Directory_Contents
    113.                             where Struct_Type = 'FILE' And
    114.                                   File_Name LIKE   '%_db%' ESCAPE '_'
    115.                             Group by Substring(File_name,1,14) )
    116.  
    117.     Declare @FileName as Varchar(100)
    118.     Open c1
    119.     Fetch c1 into @FileName
    120.     WHILE @@FETCH_STATUS = 0
    121.         BEGIN
    122.             IF @DBName = SubString(@FileName,1,Len(@DBName))
    123.                 BEGIN
    124.                     DECLARE @cmdRestore Varchar(max)
    125.                     DECLARE @sMovingData Varchar(max)
    126.                     DECLARE @sMovingLog VARCHAR(Max)
    127.  
    128.                     if @NewPathLogFile <> ''
    129.                        Set @sMovingLog = ', @with = ''move ''' + @PhysicalLogName + ''' to ''' + @NewPathLogFile + ''''    
    130.                    
    131.                     if @NewPathDataFile <> ''
    132.                        Set @sMovingData = ', @with = ''move ''' + @PhysicalDataName + ''' to ''' + @NewPathDataFile + ''''
    133.  
    134.  
    135.                     Set @cmdRestore = 'exec master.dbo.xp_restore_database @database = ''' + @DBName + ''', @filename = ''' +  @FileName + ''',@with=''STATS = 10''' + @sMovingData + @sMovingLog + ',@affinity = 0,@logging = 0'
    136.                     Print @cmdRestore
    137.                 END
    138.             Fetch c1 into @FileName
    139.         END
    140.     CLOSE c1
    141.     DEALLOCATE c1
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL Server 2005 Auto Restore

    Reworked with out cursor

    sql Code:
    1. IF EXISTS (SELECT Name From SYS.OBJECTS WHERE Name = 'proc_MyDdRestore' AND type = 'P')
    2.     DROP PROCEDURE proc_MyDdRestore
    3. GO
    4.  
    5. CREATE PROCEDURE dbo.proc_MyDdRestore
    6. (
    7.     @path varchar(2000),
    8.     @Supress int = 0,
    9.     @DBName varchar(100),
    10.     @PhysicalDataName Varchar(100),
    11.     @PhysicalLogName Varchar(100),
    12.     @NewPathDataFile Varchar(1000),
    13.     @NewPathLogFile Varchar(1000)
    14. )
    15. AS
    16.  
    17. SET NOCOUNT ON
    18. EXEC sp_configure 'show advanced options', 1
    19. -- To update the currently configured value for advanced options.
    20. RECONFIGURE
    21. -- To enable the feature.
    22. EXEC sp_configure 'xp_cmdshell', 1
    23. -- To update the currently configured value for this feature.
    24. RECONFIGURE
    25.  
    26. TRUNCATE TABLE Directory_Contents_Stage
    27. TRUNCATE TABLE Directory_Contents
    28.  
    29.     DECLARE @cmd varchar(4000)
    30.     IF Right(@path,7) <> '\*.bak'
    31.         BEGIN
    32.             Set @path = @path + '\*.bak'
    33.         END
    34.  
    35.      SELECT @cmd = 'Dir "' + @path + '"'
    36.      --print @cmd
    37. INSERT INTO Directory_Contents_Stage(dir_output) EXEC master..xp_cmdshell @cmd
    38.  
    39.   IF EXISTS (SELECT * FROM Directory_Contents_Stage
    40.        WHERE dir_output = 'The system cannot find the file specified.')
    41.     BEGIN
    42.          PRINT 'The system cannot find the file specified.'
    43.          EXEC sp_configure 'show advanced options', 1
    44.          -- To update the currently configured value for advanced options.
    45.          RECONFIGURE
    46.          -- To enable the feature.
    47.          EXEC sp_configure 'xp_cmdshell', 0
    48.          -- To update the currently configured value for this feature.
    49.          RECONFIGURE
    50.  
    51.          RETURN
    52.     END
    53.  
    54.   IF EXISTS (SELECT * FROM Directory_Contents_Stage
    55.        WHERE dir_output = 'File Not Found')
    56.     BEGIN
    57.          PRINT 'File Not Found'
    58.          EXEC sp_configure 'show advanced options', 1
    59.         -- To update the currently configured value for advanced options.
    60.         RECONFIGURE
    61.         -- To enable the feature.
    62.         EXEC sp_configure 'xp_cmdshell', 0
    63.         -- To update the currently configured value for this feature.
    64.         RECONFIGURE
    65.         RETURN
    66.     END        
    67.  
    68.   IF EXISTS (SELECT * FROM Directory_Contents_Stage
    69.        WHERE dir_output = 'The system cannot find the path specified.')
    70.     BEGIN
    71.          PRINT 'The system cannot find the path specified.'
    72.          EXEC sp_configure 'show advanced options', 1
    73.          -- To update the currently configured value for advanced options.
    74.          RECONFIGURE
    75.          -- To enable the feature.
    76.          EXEC sp_configure 'xp_cmdshell', 0
    77.          -- To update the currently configured value for this feature.
    78.          RECONFIGURE
    79.          RETURN
    80.     END
    81.  
    82.      UPDATE Directory_Contents_Stage SET [dir] = @path
    83.  
    84. DELETE FROM Directory_Contents WHERE [dir] = @path
    85.  
    86. INSERT INTO Directory_Contents (Create_Time, File_Size, [File_Name], [dir], [Struct_Type])
    87.      SELECT   CONVERT(datetime,SUBSTRING(dir_output,1,20)) AS [Create_Time]
    88.      , CONVERT(bigint,LTRIM(RTRIM(REPLACE(SUBSTRING(dir_output,21,19),',','')))) AS [File_Size]
    89.      , SUBSTRING(dir_output,40,(LEN(dir_output)-39)) AS [File_Name]
    90.      , [dir]
    91.      , 'FILE'
    92.       FROM  Directory_Contents_Stage
    93.      WHERE  SUBSTRING(dir_output,1,1) <> ' '
    94.        AND (SUBSTRING(dir_output,1,1) <> ' '
    95.        AND  SUBSTRING(dir_output,25,5) <> CHAR(60)+'Dir'+CHAR(62))
    96.  
    97.  
    98. INSERT INTO Directory_Contents (Create_Time, [File_Name], [dir], [Struct_Type])
    99.      SELECT   CONVERT(datetime,SUBSTRING(dir_output,1,20)) AS [Create_Time]
    100.      , SUBSTRING(dir_output,40,(LEN(dir_output)-39)) As [File_Name]
    101.      , [dir]
    102.      , CHAR(60)+'Dir'+CHAR(62) AS  [Struct_Type]
    103.        FROM  Directory_Contents_Stage
    104.       WHERE  SUBSTRING(dir_output,25,5) = CHAR(60)+'Dir'+CHAR(62)
    105. EXEC sp_configure 'show advanced options', 1
    106. -- To update the currently configured value for advanced options.
    107. RECONFIGURE
    108. -- To enable the feature.
    109. EXEC sp_configure 'xp_cmdshell', 0
    110. -- To update the currently configured value for this feature.
    111. RECONFIGURE
    112.  
    113. IF @Supress = 0
    114.     BEGIN
    115.         Declare @FileName as Varchar(100)
    116.  
    117.         Select @FileName = File_name From Directory_Contents Where Create_time In (
    118.                 select max(create_Time)From Directory_Contents
    119.                 where Struct_Type = 'FILE' And
    120.                       SubString(File_Name,1,Len(@DBName)) = @DBName
    121.                 Group by Substring(File_name,1,14))
    122.  
    123.         DECLARE @cmdRestore Varchar(max)
    124.         DECLARE @sMovingData Varchar(max)
    125.         DECLARE @sMovingLog VARCHAR(Max)
    126.  
    127.         if @NewPathLogFile <> ''
    128.            Set @sMovingLog = ', @with = ''move "'+ + @PhysicalLogName + '" to "' + @NewPathLogFile + '"'''    
    129.                    
    130.         if @NewPathDataFile <> ''
    131.            Set @sMovingData = ', @with = ''move "' + @PhysicalDataName + '" to "' + @NewPathDataFile + '"'''
    132.  
    133.         Set @cmdRestore = 'exec master.dbo.xp_restore_database @database = ''' + @DBName + ''', @filename = ''' +  @FileName + ''',@with=''STATS = 10''' + @sMovingData + @sMovingLog + ',@affinity = 0,@logging = 0'
    134.         Print @cmdRestore
    135.     END
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server 2005 Auto Restore

    This is very nice - I might find a use for this immediately...

    We are developing a complex case management / document tracking system - and have a need for the server engine to be able to find files in a disk folder.

    Thanks!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL Server 2005 Auto Restore

    Steve

    I might try placing the whole thing in a Try Catch structure to ensure that if a error occurs I get the xp_cmdshell set back to turned off for security reasons.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width