SQL Server 2005 added the STOPAT option for the RESTORE DATABASE command. This sounds great – we can stop at some point in time during the database backup process was running! Or? No, we can’t. Here follows some tech stuff why not, and then what the option is really meant for:
A database backup includes all used extents and also all log records that were produced while the backup process was running (possibly older as well, to handle open transactions). When you restore such a backup, SQL Server will from backup media copy the data pages to mdf and ndf file(s), log records to ldf file(s) and then do REDO (roll forward the log records) and possibly also UNDO (rollback open transactions – this is skipped if you specify NORECOVERY option). Why does it need the log records? Because you can do modifications in the database while the database backup is running. After backup process was started (and before it finishes) you can both modify pages not yet copied to backup media, but also pages already copied to backup media. So, the data pages in themselves do not present a consistent state of the database. At restore time, SQL Server uses the log records included in the database backup to “sync” the modifications that were performed while the backup process were running. So, what you end up with is what the database looked like at the end time of the backup process. I’m sure this is well documented somewhere in Books Online so I won’t dive further into the tech stuff here.
So, what is the purpose of the STOPAT option for RESTORE DATABASE?
It is to give you an early warning, in case you try to restore a database backup which is too late for a subsequent STOPAT operation for a log restore. Here’s an example:
- 13:00 db backup
- 13:10 log backup
- 13:20 log backup
- 13:30 db backup
- 13:40 log backup
- 13:50 log backup
Say you now want to restore to 13:15. Intuitively, you say that you will restore 1, 2 and for 3 you do STOPAT 13:15. Yes, that is right.
But say you are under stress, and perhaps even used some tool which confuses things for you? So, you end up restore 4, 5 and for 6 you try STOPAT 13:15. Clearly, you can’t stopat 13:15 for backup number 6 (it only covers 13:40 to 13:50). I.e., restore of 6 gives you an error message. So, you have to re-start the restore process (1, 2 and 3), which could take a long time (if the backups are large). If you had specified STOPAT 13:15 when you restore backup 4, SQL Server would have given you an error immediately, so you wouldn’t have wasted valuable time restoring unnecessary backups!
That is the purpose of STOPAT for RESTORE DATABASE.