title: SQL Log Backups - "too recent to apply to the database"
body: <p>I have a rather large database (for our hardware setup) and am trying to get Mirroring going on it.  I cannot seem to get the Primary and Mirror databases close enough in sync to allow Mirroring to start.</p>
  
  <p>The full backup takes about 10 minutes, plus another 8 minutes to transfer the BAK to the Mirror server and another 10 minutes to restore it.  So, to try to get things as close together as I can I have scripted out the following:</p>
  
  <p>** On the Primary **</p>
  
  <pre><code>SET NOCOUNT ON
  USE master
  
  
  DECLARE @dbFileName     VARCHAR(75)
        , @logFileName    VARCHAR(75)
  
  SELECT @dbFileName     = 'F:\SQL_Backups\RIVFramework_Prod1\Mirror\prod1.mdf.' + CONVERT(VARCHAR(10), GETDATE(), 102) + '.bak'  -- formats date in YYYY.MM.DD --
  SELECT @logFileName    = 'F:\SQL_Backups\RIVFramework_Prod1\Mirror\prod1.ldf.' + CONVERT(VARCHAR(10), GETDATE(), 102) + '.bak'  -- formats date in YYYY.MM.DD --
  
  SELECT @dbFileName     
  SELECT @logFileName    
  
  /**
   ** 1 -  Make sure your database is in full recovery mode
   **/
  ALTER DATABASE RIVFramework_Prod1
  SET RECOVERY FULL
  
  /**
   ** 2 - Backup the database
   **/
  BACKUP DATABASE RIVFramework_Prod1
  TO DISK = @dbFileName
  
  /**
   ** 3 - Backup the database log
   **/
  BACKUP LOG RIVFramework_Prod1
  TO DISK = @logFileName
  
  SET NOCOUNT OFF
  </code></pre>
  
  <p>** Copy the files **</p>
  
  <hr>
  
  <p>***NOTE:  I altered the main database restore statement to allow the log file to be restored later correctly.  Changed </p>
  
  <blockquote>
    <p>WITH NORECOVERY,</p>
  </blockquote>
  
  <p>to </p>
  
  <blockquote>
    <p>WITH REPLACE, NORECOVERY,</p>
  </blockquote>
  
  <hr>
  
  <p>** On the Mirror **</p>
  
  <pre><code>SET NOCOUNT ON
  USE master
  
  /**
   ** Restore backup to Mirror server:
   ** MIRROR SERVER
   ** 4 - Restore to the mirror database with no recovery option
   **/ 
  RESTORE DATABASE RIVFramework_Prod1
  FROM DISK = 'F:\XFer\prod1.mdf.2011.06.10.bak'
  WITH REPLACE, NORECOVERY,   -- You need to have both options in here so the later log file restore works.
   MOVE 'RIVFramework_Prod1' TO 'F:\SQL2008\RIVFramework_Prod1.mdf',
   MOVE 'CampaignAnalytics' TO 'F:\SQL2008\RivFramework_Prod1_CampaignAnalytics.ndf',
   MOVE 'SalesAnalytics' TO 'F:\SQL2008\RivFramework_Prod1_SalesAnalytics.ndf',
   MOVE 'RIVFramework_Prod1_log' TO 'G:\SQL2008\RIVFramework_Prod1_log.ldf'
  
  
  /**
   ** 5 - Restore the mirror database logs with no recovery
   **/
  RESTORE LOG RIVFramework_Prod1
  FROM DISK = 'F:\XFer\prod1.ldf.2011.06.10.bak'
  WITH NORECOVERY
  GO
  </code></pre>
  
  <p>Now, I am about 30 minutes out of sync.  So I do:</p>
  
  <p>** On the Primary **</p>
  
  <pre><code>SET NOCOUNT ON
  USE master
  
  
  DECLARE @logFileName    VARCHAR(75)
  
  SELECT @logFileName    = 'F:\SQL_Backups\RIVFramework_Prod1\Mirror\prod1.ldf.a.' + CONVERT(VARCHAR(10), GETDATE(), 102) + '.bak'    -- formats date in YYYY.MM.DD --
  
  SELECT @logFileName    
  
  /**
   ** 1 -  Make sure your database is in full recovery mode
   **/
  ALTER DATABASE RIVFramework_Prod1
  SET RECOVERY FULL
  
  /**
   ** 3 - Backup the database log
   **/
  BACKUP LOG RIVFramework_Prod1
  TO DISK = @logFileName
  
  SET NOCOUNT OFF
  </code></pre>
  
  <p>** Copy the files **</p>
  
  <p>** On the Mirror **</p>
  
  <pre><code>SET NOCOUNT ON
  USE master
  
  /**
   ** 5 - Restore the mirror database logs with no recovery
   **/
  RESTORE LOG RIVFramework_Prod1
  FROM DISK = 'F:\XFer\prod1.ldf.a.2011.06.10.bak'
  WITH NORECOVERY
  GO
  </code></pre>
  
  <p>And I am greeted with the following error:</p>
  
  <blockquote>
    <p>Msg 4305, Level 16, State 1, Line 53
    The log in this backup set begins at
    LSN 61063000000325800001, which is too
    recent to apply to the database. An
    earlier log backup that includes LSN
    61063000000117800001 can be restored.
    Msg 3013, Level 16, State 1, Line 53
    RESTORE LOG is terminating abnormally.</p>
  </blockquote>
  
  <p>Any ideas what I am missing???</p>
  
  <p>TIA</p>