Question : restore script needed for standby

Hello,
I am trying to come up with a script that restores the "latest" backup to the standby databases but here is my dilemma....

Our backup plan is as follows:

nightly full backups
twice daily differential backups
hourly transaction log backups

These backups are pushed over to the standby servers in different folders

I need to automate the process of finding the right backups.... and restoring the database(full, differential, transaction logs)

Can someone provide me a with a template that can do all of this I have been unable to find what I need online. Thanks.



Answer : restore script needed for standby

If you don't get anything better, this proc is close.  It restores the most recent backup and then restores the subsequent logs.  We don't do differentials, so it doesn't handle them.   Some paths are hard-coded, there are assumptions about database logical names, and it presumes backup files are named with the format created by sql maintenance plans.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
ALTER PROCEDURE [dbo].[p_RestoreBackupDirForNightlyLoad]

  @i_sDevDbType        varchar(8),
  @i_sLogPathAppName   varchar(40)

AS
BEGIN

DECLARE
   @bValidDevDbType       bit,
   @bValidLogPathAppName  bit,
   @sErrorMessage         varchar(250)

SET @bValidDevDbType = 0
SELECT @bValidDevDbType = 1
 WHERE @i_sDevDbType IN ('Modified', 'Nightly')
 
IF @bValidDevDbType = 0
   BEGIN
    SET @sErrorMessage = @i_sDevDbType + ' - Input parameter invalid.  Must equal "Modified" or "Nightly"'
	GOTO ERROR
   END
;

SET @bValidLogPathAppName = 0
SELECT @bValidLogPathAppName = 1
 WHERE @i_sLogPathAppName IN (various database names here)
 
IF @bValidLogPathAppName = 0
   BEGIN
    SET @sErrorMessage = @i_sLogPathAppName + ' - Input parameter invalid.'
	GOTO ERROR
   END
;

DECLARE
   @sLogFullPath         varchar(100),
   @sBackupFileList      varchar(100),

   @iBAKFileNameStartPos int,

   @iCmdReturnCode       int,
   @sCommand             varchar(150),
   @sBAKFile_MaxTS       varchar(12),
   @sDirBAKFile          varchar(100),
   @sDirTRNFile          varchar(100)


DECLARE 
   @LockResult           int;

print '*** time before LOCK REQUEST is ' + CONVERT (varchar(25), getdate(), 121 );
    EXEC @LockResult = sp_getapplock @Resource = 'tempdb..tables', 
									 @LockMode = 'Exclusive',
									 @LockOwner = 'Session',
									 @LockTimeout  = 3600000;

if @LockResult >= 0 
   begin
     print '*** LOCK granted at ' + CONVERT (varchar(25), getdate(), 121 );
   end
else
   begin
     print '*** LOCK not successful, time is ' + CONVERT (varchar(25), getdate(), 121 );
   end
;


    IF OBJECT_ID(N'tempdb..CmdDirContents_t', N'U') IS NOT NULL 
        DROP TABLE tempdb..CmdDirContents_t;


    CREATE TABLE tempdb..CmdDirContents_t 
    (
     DirList       varchar(100)  NULL
     );


    IF OBJECT_ID(N'tempdb..CmdDirAndTS_t', N'U') IS NOT NULL 
        DROP TABLE tempdb..CmdDirAndTS_t;


    CREATE TABLE tempdb..CmdDirAndTS_t 
    (
     DirList       varchar(100)  NOT NULL,
     Backup_TS     varchar(12)   NOT NULL     
     );


    SET @sLogFullPath = 'K:\BackMeUp\User\'
                      + @i_sLogPathAppName
                      + '\';
    print  'LogFullPath path to backup is ' + IsNull(@sLogFullPath,'')

    SET @sBackupFileList = @sLogFullPath + 'BackupFileList.txt'
    SET @sCommand = 'del ' + @sBackupFileList;

    exec @iCmdReturnCode = xp_cmdshell @sCommand
    SET @sCommand = 'dir ' + @sLogFullPath + ' > ' + @sBackupFileList
    exec @iCmdReturnCode = xp_cmdshell @sCommand
    print  'BackupFileList is located at ' + IsNull(@sBackupFileList,'')
    

DECLARE
   @sSqlStmt   nvarchar(250)

        SET @sSqlStmt = 
            N'BULK INSERT tempdb..CmdDirContents_t ' + 
            N'FROM ' + QUOTENAME(@sBackupFileList)  + N' ' + 
            N'WITH ' + 
               N'(FIELDTERMINATOR = ''\t'', ' + 
               N'FIRSTROW = 1, ' + 
               N'KEEPNULLS, ' +
               N'ROWTERMINATOR = ''\n'')';

        EXEC sp_executesql @sSqlStmt;

        IF @@ERROR = 0
           Print 'Successful Insert into tempdb..CmdDirContents_t'


    DELETE 
      FROM tempdb..CmdDirContents_t
     WHERE DirList IS NULL;

    DELETE 
      FROM tempdb..CmdDirContents_t
     WHERE DirList NOT LIKE '%_backup_%.bak'
       AND DirList NOT LIKE '%_backup_%.trn';

    INSERT tempdb..CmdDirAndTS_t
    SELECT DirList,
           Backup_TS = LEFT(RIGHT(DirList, 16), 12)
      FROM tempdb..CmdDirContents_t;

   SELECT * from tempdb..CmdDirAndTS_t;

   SELECT @sBAKFile_MaxTS = mxbk.Backup_TS
     FROM (SELECT TOP 1
                  *
             FROM tempdb..CmdDirAndTS_t
            WHERE DirList LIKE '%_backup_%.bak'
            ORDER BY 2 DESC
           ) mxbk ;

   print ' '
   print 'Timestamp of backup file chosen is ' + IsNull(@sBAKFile_MaxTS,'');

   SELECT @iBAKFileNameStartPos = PATINDEX('%' + @i_sLogPathAppName + '_b' + '%', DirList)
     FROM tempdb..CmdDirAndTS_t
    WHERE DirList LIKE '%_backup_%.bak'
      AND Backup_TS = @sBAKFile_MaxTS;


   SELECT @sDirBAKFile = @sLogFullPath + SUBSTRING(DirList, @iBAKFileNameStartPos, LEN(DirList))
     FROM tempdb..CmdDirAndTS_t
    WHERE DirList LIKE '%_backup_%.bak'
      AND Backup_TS = @sBAKFile_MaxTS;

DECLARE
   @sCoreAppName        varchar(128),
   @sRstrDBName         varchar(128),
   @sLogicalNameData    varchar(128),
   @sLogicalNameLog     varchar(128),
   @sPhysicalNameData   varchar(128),
   @sPhysicalNameLog    varchar(128)

   SET @sCoreAppName = REPLACE(@i_sLogPathAppName, 'Sdc', '');
   SET @sRstrDBName =  REPLACE(@sCoreAppName, 'Prod', '');
   SET @sRstrDBName =  @i_sDevDbType + @sRstrDBName;

   SET @sLogicalNameData = @i_sLogPathAppName + '_Data';
   SET @sLogicalNameLog = @i_sLogPathAppName + '_Log';

   SET @sPhysicalNameData = 'M:\MsSqlData\' + @sRstrDBName + '_Data.MDF';
   SET @sPhysicalNameLog = 'N:\MsSqlTranLog\' + @sRstrDBName + '_Log.LDF';

   print 'Restore DB Name is ' + IsNull(@sRstrDBName,'');
   print 'Logical Name-Data is ' + IsNull(@sLogicalNameData,'')
   print 'Physical Name-Data is ' + IsNull(@sPhysicalNameData,'')
   print 'Logical Name-Log is ' + IsNull(@sLogicalNameLog,'')
   print 'Physical Name-Log is ' + IsNull(@sPhysicalNameLog,'')
   print 'Backup File is ' + IsNull(@sDirBAKFile,'')
   print ' '

   RESTORE DATABASE @sRstrDBName
      FROM DISK = @sDirBAKFile 
      WITH REPLACE,
        MOVE @sLogicalNameData TO @sPhysicalNameData, 
        MOVE @sLogicalNameLog TO @sPhysicalNameLog,
        NORECOVERY;

   print 'Completed restoring database with backup file of ' + @sDirBAKFile


DECLARE TrnLog_cursor 
  CURSOR FOR 
         SELECT DirTRNFile = @sLogFullPath + SUBSTRING(DirList, @iBAKFileNameStartPos, LEN(DirList))
           FROM tempdb..CmdDirAndTS_t
          WHERE DirList LIKE '%_backup_%.trn'
            AND Backup_TS >= @sBAKFile_MaxTS
          ORDER BY Backup_TS asc;

   OPEN TrnLog_cursor

   FETCH NEXT FROM TrnLog_cursor 
    INTO @sDirTRNFile

   EXEC @LockResult = sp_releaseapplock @Resource = 'tempdb..tables',
										@LockOwner = 'Session'

print '*** time after LOCK RELEASE is ' + CONVERT (varchar(25), getdate(), 121 );


   WHILE @@FETCH_STATUS = 0
   BEGIN

      RESTORE LOG @sRstrDBName
         FROM DISK = @sDirTRNFile 
         WITH NORECOVERY;

      print 'Completed applying Transaction Logfile of ' + @sDirTRNFile
      print ' '

      FETCH NEXT FROM TrnLog_cursor 
       INTO @sDirTRNFile

   END

   CLOSE TrnLog_cursor
   DEALLOCATE TrnLog_cursor

   RESTORE DATABASE @sRstrDBName
   WITH RECOVERY

   RETURN 0


ERROR:
   RAISERROR(@sErrorMessage, 16, 1)
      RETURN -1


END
Random Solutions  
 
programming4us programming4us