Search code examples
sql-serversql-server-2008database-backupsdatabase-restore

Differential Backups in SQL Server 2008; Able to restore through SSMS but unable to restore through Transact SQL


I am facing an issue while restoring the databases from differential backups. Here are the steps I performed

DROP DATABASE DBName_delta
GO
BACKUP DATABASE DBName TO DISK = 'E:\Fullbak1.bak' 
GO
RESTORE FILELISTONLY FROM DISK = 'E:\Fullbak.bak'
GO
RESTORE DATABASE DBName_delta  
    FROM  DISK='E:\Fullbak.bak' 
   WITH MOVE 'DBName_Data' TO 'E:\DBData\DBName_delta.mdf',   
   MOVE 'DBName_Image_Data' TO 'E:\DBData\DBName_delta_Image_Data.mdf',   
   MOVE 'DBName_Log' TO 'D:\DBLog\DBName_delta.ldf',
   NORECOVERY

--Made Some changes in the database
BACKUP DATABASE DBName
   TO DISK = 'E:\DiffBak1.TRN'
   WITH DIFFERENTIAL
GO

--Made Some more changes in the database    
BACKUP DATABASE DBName
   TO DISK = 'E:\DiffBak2.TRN'
   WITH DIFFERENTIAL
GO

RESTORE FILELISTONLY FROM DISK = 'E:\DiffBak1.TRN'
GO

RESTORE LOG DBName_delta FROM DISK='E:\DiffBak1.TRN' WITH NORECOVERY
GO

Msg 4305, Level 16, State 1, Line 2
The log in this backup set begins at LSN 81125000000059600297, which is too recent to apply to the database. An earlier log backup that includes LSN 81121000000116200001 can be restored.

Msg 3013, Level 16, State 1, Line 2
RESTORE LOG is terminating abnormally.

RESTORE FILELISTONLY FROM DISK = 'E:\DiffBak2.TRN'
GO
RESTORE LOG DBName_delta FROM DISK='E:\DiffBak2.TRN' WITH STANDBY = 'c:\undo.ldf'
GO

But when I tried to restore the same E:\DiffBak1.TRN through SSMS by using the WITH NORECOVERY option, it restored the database and again I was able to perform the restore of the same file using the Transact SQL. Am I missing something here? Is this something to do with the RESTORE DATABASE? I am sure that we are not missing any of the logs in between. Any help will be much appreciated.


Solution

  • I am able to figure this out. As I mentioned, I was able to restore the differential database through the SSMS wizard, I took the script from the wizard and found out the differences in the query.

    I was using the below code

    RESTORE LOG DBName_delta FROM DISK='E:\DiffBak1.TRN' WITH NORECOVERY
    GO
    

    I have changed the code to

    RESTORE DATABASE [DBrel02t_delta] FROM  DISK = N'E:\DiffBak1.TRN' WITH  FILE = 1,  NORECOVERY,  NOUNLOAD,  STATS = 10
    GO
    

    This resolved the issue. Use the below query to find out the lsn details of your backup.

    RESTORE HEADERONLY FROM DISK  = N’<backup file>’;