Restore:
Restoration process in case of any database failure. During any database failure we need to perform a tail log backup before starting the restoration process.
Tail Log Backup:
SQL Server 2005 and later versions usually require that you take a tail-log backup before you start to restore a database. The tail-log backup prevents work loss and keeps the log chain intact. When you are recovering a database to the point of a failure, the tail-log backup is the last backup of interest in the recovery plan. If you cannot back up the tail of the log, you can recover a database only to the end of the last backup that was created before the failure.
· If the database is online and you plan to perform a restore operation on the database, before starting the restore operation, back up the tail of the log using WITH NORECOVERY:
BACKUP LOG test TO disk ='C:\Sandeep\Backup\Log\Test_20120104.trn'
WITH NORECOVERY
· If the database is offline and does not start.
Try to take a tail-log backup. Because no transactions can occur at this time, using WITH NORECOVERY is optional. If the database is damaged, use WITH CONTINUE_AFTER_ERROR, as follows:
BACKUP LOG test TO disk ='C:\Sandeep\Backup\Log\Test_20120104.trn'
WITH CONTINUE_AFTER_ERROR
If the database is damaged, for example, if the database does not start, a tail-log backup succeeds only if the log files are undamaged, the database is in a state that supports tail-log backups, and the database does not contain any bulk-logged changes.
Steps to restore the database:
1) Restore the database with latest full backup with no recovery.
RESTORE DATABASE [Test] FROM DISK = 'C:\Sandeep\Backup\Full\Test_20120104.BAK' WITH NORECOVERY,
Restore the database with latest differential backup with no recovery.
RESTORE DATABASE [Test] FROM DISK = 'C:\Sandeep\Backup\differential\Test_20120104.diff' WITH NORECOVERY
2) Restore the transaction logs and the tail log backup.
· Before restoring the log backups run the below script and check for the backupfinishdate for the latest differential backup applied.
restore headeronly from disk='C:\Sandeep\Backup\Differential\Test_20120104.diff'
BackupFinishDate = 2012-01-04 09:41:16.000
· Now run the below command to check the log backup date time and fileid.
restore headeronly from disk='C:\Sandeep\Backup\Log\Test_20120104.trn'
Some Columns from the Output:
| Position | DatabaseName | BackupFinishDate |
| 1 | Test | 1/4/12 9:06 |
| 2 | Test | 1/4/12 9:30 |
| 3 | Test | 1/4/12 9:44 |
| 4 | Test | 1/4/12 9:54 |
| 5 | Test | 1/4/12 9:54 |
| 6 | Test | 1/4/12 10:18 |
| 7 | Test | 1/4/12 10:42 |
| 8 | Test | 1/4/12 11:06 |
| 9 | Test | 1/4/12 11:30 |
| 10 | Test | 1/4/12 11:54 |
As the differential backup is of 9: 41 PM, restore all the logs after 9:41PM with no recovery and the tail log with recovery using the below script.
RESTORE LOG test
FROM disk='C:\Sandeep\Backup\Log\Test_20120104.trn'
WITH FILE = 3,
NORECOVERY
go
RESTORE LOG test
FROM disk='C:\Sandeep\Backup\Log\Test_20120104.trn'
WITH FILE = 4,
NORECOVERY
go
RESTORE LOG test
FROM disk='C:\Sandeep\Backup\Log\Test_20120104.trn'
WITH FILE = 5,
NORECOVERY
go
RESTORE LOG test
FROM disk='C:\Sandeep\Backup\Log\Test_20120104.trn'
WITH FILE = 6,
NORECOVERY
go
RESTORE LOG test
FROM disk='C:\Sandeep\Backup\Log\Test_20120104.trn'
WITH FILE = 7,
NORECOVERY
go
RESTORE LOG test
FROM disk='C:\Sandeep\Backup\Log\Test_20120104.trn'
WITH FILE = 8,
NORECOVERY
go
RESTORE LOG test
FROM disk='C:\Sandeep\Backup\Log\Test_20120104.trn'
WITH FILE = 9,
NORECOVERY
go
RESTORE LOG test
FROM disk='C:\Sandeep\Backup\Log\Test_20120104.trn'
WITH FILE = 10,
RECOVERY
go
Alternate script for the whole process:
Step 1:
BACKUP LOG test TO disk ='C:\Sandeep\Backup\Log\Test_20120104.trn' WITH NORECOVERY
Step 2: Check for the backupfinish dates and fileid’s of log backup files using restore headeronly. And execute the below script
RESTORE DATABASE [Test] FROM DISK = N'C:\Sandeep\Backup\Full\Test_20120104.BAK' WITH FILE = 1, NORECOVERY, NOUNLOAD, STATS = 10
GO
RESTORE DATABASE [Test] FROM DISK = N'C:\Sandeep\Backup\differential\Test_20120104.diff' WITH FILE = 1, NORECOVERY, NOUNLOAD, STATS = 10
GO
RESTORE LOG [Test] FROM DISK = N'C:\Sandeep\Backup\Log\Test_20120104.trn' WITH FILE = 3, NORECOVERY, NOUNLOAD, STATS = 10
GO
RESTORE LOG [Test] FROM DISK = N'C:\Sandeep\Backup\Log\Test_20120104.trn' WITH FILE = 4, NORECOVERY, NOUNLOAD, STATS = 10
GO
RESTORE LOG [Test] FROM DISK = N'C:\Sandeep\Backup\Log\Test_20120104.trn' WITH FILE = 5, NORECOVERY, NOUNLOAD, STATS = 10
GO
RESTORE LOG [Test] FROM DISK = N'C:\Sandeep\Backup\Log\Test_20120104.trn' WITH FILE = 6, NORECOVERY, NOUNLOAD, STATS = 10
GO
RESTORE LOG [Test] FROM DISK = N'C:\Sandeep\Backup\Log\Test_20120104.trn' WITH FILE = 7, NORECOVERY, NOUNLOAD, STATS = 10
GO
RESTORE LOG [Test] FROM DISK = N'C:\Sandeep\Backup\Log\Test_20120104.trn' WITH FILE = 8, NORECOVERY, NOUNLOAD, STATS = 10
GO
RESTORE LOG [Test] FROM DISK = N'C:\Sandeep\Backup\Log\Test_20120104.trn' WITH FILE = 9, NORECOVERY, NOUNLOAD, STATS = 10
GO
RESTORE LOG [Test] FROM DISK = N'C:\Sandeep\Backup\Log\Test_20120104.trn' WITH FILE = 10, NOUNLOAD, STATS = 10
GO