简体   繁体   中英

SQL transaction log shipping after differential backup

I'm facing a scenario:

  1. Full backup of production database is created
  2. Full backup is restored to DR database
  3. A differential backup of production database is created, BUT accidentally deleted
  4. Start transaction log backup on production database

The question is, will the first generated transaction log would be able to be restored into DR database, or the deleted differential database backup must be restored to DR database first?

The short answer is no, you need a/the differential backup or a new full backup. So either take a new full backup to restart the backup chain, or a new differential backup to replace the lost/deleted one.

Simplified, a Transaction Log backup will maintain a chronological list of changes made to your database since the last backup (Any backup; either full, differential or another transaction log backup). A differential backup will save a list of all extents that changed since the last Full Backup (so including whatever any transaction log backup already backed up in the mean time). This also means that if you create a full backup, followed by a differential backup, followed by a differential backup, the second differential backup contains everything the first one contains.

A typical scenario is where a full backup is taken, followed by a few transaction log backups. The next step then is a differential backup, and probably more transaction log backups and differential backups until a new full backup is created.

This ensures the time to recovery is reduced by ensuring that in case of disaster, you can take a backup of the tail of the transaction log. You can then restore the full backup, followed by the latest differential backup, followed by any transaction log backups (in chronological order).

[1] If DIFF backup was deleted then I would create another DIFF backup . As long as differential base was not changed (by creating another FULL backup NON COPY_ONLY ; see msdb.dbo.backupset ) this should be ok.

[2] There is no requirement for Log Shipping to apply/restore a DIFF backup after a FULL backup. After a restore of FULL should be enough to start/restart log shipping: to start all three jobs: LS_?_Backup , LS_?_Copy and LS_?_Restore . Also I would check if there is a maintenance plan that will take log backups - this should be disabled (it's ok because now LS will take care of log backups).

[3] After a FULL restore, LOG backups (*.trn) can be applied if backup chain isn't broken (without any DIFF applied):

  • This means that between FULL backup and LOG backup there is NO other FULL backup ( NON COPY_ONLY ) and also

  • We have all LOG backups between FULL backup and current LOG backup.

Example: This will create following backups: F D1 L1 L2 and then it will restore using following sequence: F L1 L2 . At the end of example, you will see that dbo.tab1 has all rows.

USE master;
GO
CREATE DATABASE CocoJambo;
GO
ALTER DATABASE CocoJambo 
SET RECOVERY FULL
GO
USE CocoJambo
GO
CREATE TABLE dbo.tab1(id int not null primary key)
go
insert dbo.tab1(id) values (1)
go
BACKUP DATABASE CocoJambo TO DISK = 'CocoJambo_full.bak' WITH STATS = 25 -- FULL backup
GO
insert dbo.tab1(id) values (2)
go
BACKUP DATABASE CocoJambo TO DISK = 'CocoJambo_diff_01.bak' WITH STATS = 25, DIFFERENTIAL -- DIFF backup
GO
BACKUP LOG CocoJambo TO DISK = 'CocoJambo_log_01.trn' WITH STATS = 25
GO
insert dbo.tab1(id) values (3)
go
BACKUP LOG CocoJambo TO DISK = 'CocoJambo_log_02.trn' WITH STATS = 25
GO

-- RESTORE
USE master 
GO
DROP DATABASE CocoJambo
GO
RESTORE DATABASE CocoJambo_restored 
FROM DISK = 'CocoJambo_full.bak' -- FULL backup restore
WITH 
STATS = 25, 
NORECOVERY -- To allow future diff/log restores
GO
/*
25 percent processed.
51 percent processed.
77 percent processed.
100 percent processed.
Processed 336 pages for database 'CocoJambo_restored', file 'CocoJambo' on file 1.
Processed 7 pages for database 'CocoJambo_restored', file 'CocoJambo_log' on file 1.
RESTORE DATABASE successfully processed 343 pages in 0.366 seconds (7.310 MB/sec).
*/

RESTORE LOG CocoJambo_restored 
FROM DISK = 'CocoJambo_log_01.trn'
WITH STATS = 25, NORECOVERY
/*
76 percent processed.
100 percent processed.
Processed 0 pages for database 'CocoJambo_restored', file 'CocoJambo' on file 1.
Processed 11 pages for database 'CocoJambo_restored', file 'CocoJambo_log' on file 1.
RESTORE LOG successfully processed 11 pages in 0.109 seconds (0.752 MB/sec).
*/

RESTORE LOG CocoJambo_restored 
FROM DISK = 'CocoJambo_log_02.trn'
WITH STATS = 25, NORECOVERY
/*
100 percent processed.
Processed 0 pages for database 'CocoJambo_restored', file 'CocoJambo' on file 1.
Processed 2 pages for database 'CocoJambo_restored', file 'CocoJambo_log' on file 1.
RESTORE LOG successfully processed 2 pages in 0.083 seconds (0.188 MB/sec).
*/

RESTORE DATABASE CocoJambo_restored 
WITH RECOVERY -- Bring db ONLINE r/w
/*
RESTORE DATABASE successfully processed 0 pages in 1.385 seconds (0.000 MB/sec).
*/

SELECT * FROM CocoJambo_restored.dbo.tab1
/*
id
-----------
1
2
3

(3 row(s) affected)
*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM