0

Suppose I have a full backup, differential backup and 10 log backup files.

The recommended practice is to restore each with no recovery and then apply the final log backup with recovery.

Suppose I do the prior restore with recovery, then I cannot apply the subsequent backups - it gives error.

Is this by design and is there any way to allow for this (restore with recovery when there are subsequent backups that need restoring)? - for example when doing a database migration I want to restore the full backup and allow the target database to be used in readonly mode, and then apply differential and log backups subsequently.

1 Answer 1

3

You can set the database into STANDBY mode while recovering, but this will be readonly.

You cannot set it to fully recover. This is by design.

Due to the way write-ahead logging (a system used by most RDBMSs) works, transactions may be partially written to the log at the point the backup is made. The rest of the transaction will be in the next log backup.

Therefore, when restoring, if you tell the server to bring the database online, there will be in-flight transactions that have not yet been fully written to the main data files, but are fully present in the log files. These can be either rolled back: old data partially overwritten is copied back to the main data files; or they can be rolled forward: new data is copied into the main data files.

Where a transaction was not fully committed to the log, there is no way to roll it forwards, as the data simply isn't there. It can only be rolled back. So to bring the database online, these transactions must be discarded.

Then when you try restoring more log files, you will have a situation where earlier transactions have been discarded, but you are trying to restore later transactions. This will leave the database in an inconsistent state. So this is disallowed.

Whereas if the database remains offline (or in standby), the following log backup in the sequence to be restored will have the rest of the transaction data, allowing it to be rolled forward, and later transactions can be applied over the top.

5
  • Whereas if the database remains offline - by offline do you mean in stand by? or in norecovery please?
    – variable
    Commented Apr 12, 2022 at 11:21
  • The standby link you have shared says STANDBY is also not allowed when a database upgrade is necessary. - any idea what a database upgrade means please?
    – variable
    Commented Apr 12, 2022 at 11:24
  • From the same doc "When restoring backup sets from an earlier version of SQL Server, a database upgrade might be required." Also see learn.microsoft.com/en-us/sql/relational-databases/… "If you restore an older version database to SQL Server 2019 (15.x), that database will automatically upgrade to SQL Server 2019 (15.x). This prevents the database from being used with an older version of the Database Engine." Commented Apr 12, 2022 at 11:30
  • Ok so effectively if I am moving from SQL server 2016 to 2019, then it won't support restoring the database on target in standby mode.
    – variable
    Commented Apr 12, 2022 at 11:50
  • That is correct. The reason is that STANDBY does recovery and then will do UNDO of that recovery when you do a subsequent restore. When you restore to a later version, recovery includes upgrading meta-data (system tables etc) and that cannot be reverted. Commented Apr 12, 2022 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.