Deep Dive on Initialize from Backup for Transactional Replication

Share this Post

Sateesh Yele | Microsoft SQL Server Support

How to initialize the subscriber from backup
  1. Create the publication using user interface Replication Wizard.
  2. Under subscription options for the publication, set “allow initialization from backup files” to true
  3. Create a new full backup of the publisher database. If you have existing full backup of the publisher database, you can still use that backup set but we have take a new log backup or differential backup of the publisher database and restore at the subscriber.

Replication Wizards do not have options for creating subscription using “allow initialization from backup”. Instead use SQL commands when creating the subscription supplying the database backup file location.

This creates the subscription and the Distribution agent should start replication commands starting at the LSN location as indicated in the backup header information.

Possible errors when the subscriber is initialized from backups

Msg 21408
Cannot create the subscription. You must specify a value of “Active” or “Subscribed” for the @status parameter. This is because the value specified for the @sync_type parameter is “initialize with backup” or “replication support only”.

Msg 18786
The specified publication does not allow subscriptions to be initialized from a backup. To allow initialization from a backup, use sp_changepublication: set ‘allow_initialize_from_backup’ to ‘true’.

Msg 21407
Cannot create the subscription. If you specify a value of “initialize with backup” for the @sync_type parameter, you must subscribe to all articles in the publication by specifying a value of “all” for the @article parameter.

Msg 21399
The transactions required for synchronizing the subscription with the specified log sequence number (LSN) are unavailable at the Distributor. Specify a higher LSN.

Msg 21397
The transactions required for synchronizing the nosync subscription created from the specified backup are unavailable at the Distributor. Retry the operation again with a more up-to-date log, differential, or full database backup.

Quick Checks:

 allow_initialize_from_backup  = 1

min_autonosync_lsn = the lsn from which the commands should be applied at the subscriber

min_autonosync_lsn will be the same as the syspublications.min_autonosync_lsn

During the execution of sp_addsubscription, we perform validation in sp_MSaddautonosyncsubscription procedure before we create the subscription.

min_autonosync_lsn should be always less than the subscription_seqno

Purged Backup Watermark

When sp_addsubscrption with @backupdevicename=’backup device’, is executed, the “RESTORE HEADERONLY” is run against the backup and returning LASTLSN of the latest backup set.  If the backup set contains multiple backups. This LASTLSN is used as watermark for Distribution agent for when retrieving new transactions from msrepl_transactions.  If the Backup\Restore processes took too long, its possible the Distribution cleanup job has already purged the transaction watermark.  You can use query below to validate LSN watermark.

If the LSN watermark has already been purged from the Distribution database you’ll need to drop and add the Subscriber using one of these options:

  • Disable the Distribution Cleanup Agent
  • Increase the Maximum Cleanup Retention Period
  • With Publisher in Full Recovery, first apply Full backup\restore, then create subscriber using Transaction Log Backup\Restore to catch up Subscriber.
Single Backup Set

If the backup set contains multiple backup sets, the sp_addsubscrption logic select the first backup set and not the last backup set.  This may return an older watermark.  For example, if the Backup Set contains Full and TLOG backup, the watermark for Distribution Agent could use TLOG LSN, however, if backup set contains both, the add subscription command will only return the older FULL watermark LSN.

Solution: When using “allow initialization from backup files” always create the Publisher backup to a NEW backup (*.bak or *.TRN) file.


Share this Post

About: ReplTalk


5 thoughts on “Deep Dive on Initialize from Backup for Transactional Replication”

  1. I am getting the below error, while doing this.

    Msg 701, Level 17, State 123, Procedure sp_MSrepl_changesubstatus, Line 1243

    There is insufficient system memory to run this query.

  2. —Suggest Please

    —Dropping Article from T-Replication

    i want to remove some tables from transnational replication using SSMS2008R2 created from backup file, what option i use

    1.@force_invalidate_snapshot = 0;

    2.@force_invalidate_snapshot = 1;

    3. without this option @force_invalidate_snapshot

    while dropping article.

    —script —-

    USE [testdb]

    DECLARE @publication AS sysname;

    DECLARE @article AS sysname;

    SET @publication = N'Pub_new_test';

    SET @article = N'tabl';

    EXEC sys.sp_dropsubscription

      @publication = @publication,

      @article = @article,

      @subscriber = 'sub1',

      @destination_db = 'PublisherDatabase_newtest'

    — Drop the transactional article.

    EXEC sp_droparticle

    @publication = @publication,

    @article = @article,

    —  @force_invalidate_snapshot = 0;

    GO

Leave a Reply to RASLeo Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.