Saturday, December 4, 2010

Setting up transactional replication without a snapshot

We had a case where a client needed replication from London to Hong Kong and New York, needless to say we setup the transaction replication we a snapshot and due to the latency issues the snapshot never worked. I decedied to set up replication without the snapshotm heres how to do it if you need to know.

Normally its very easy to setup the publisher and subscriber by default. Thanks to the handy Wizard interface. However, we faced a tiny bit of a problem when using the wizard.

If you are replicating on the same domain and in the same country using the wizzard is great but this is probably only a small about of situations.

I had a problem with the initial snapshot creation as said above when setting up the transactional replication. This is using SQL Server 2008. The initial snapshot creation took a very long time and would not complete. Even after running it over a weekend, for 48 hours, it did not complete. The thing is, while the snapshot agent was running, the database was not accessible to other users. Is there a way to make the snapshot run under low priority?

Since I had to allow users access to the database, I had to stop the snapshot job. Will it start from scratch if restarted or will it continue from where it stopped? I tested and it seemed to start from scratch once again. Another 8+ hours? I don’t think so. Thus, I set out to find a way to create a transactional replication without a snapshot.

I did a little bit of checking around to find out how to do this as we are all DBAs but a DBA does not know everything hence why you are also reading this site. I found a stored procedure called sp_addsubscription(), this will allow one to initialize a subscription without the need to create a snapshot of the publishing database. sp_addsubscription()you will be able to run and create replication with a backup with this and without the snapshot ment not many hours of waiting. I said to myself. So I did. I did the first test in dev and then in prod after it worked,

Here is how to do it
Following are the steps to create a SQL Server 2005 Transactional Replication without an initial snapshot.

1. Create a new publication using the New Publication Wizard. you are able to still do this by using the wizard to create the publication. Just apply the right information in the fields and settings as required according to your environment. However, when you reach the SnapShot Agent dialog, you will need to leave all the checkboxes unticked. The reason being is that you dont want a snapshot remember this? That’s the only thing to note while going through the New Publication Wizard dialogs.

2. Upon completion of the New Publication Wizard, open the property dialog to your publication. You now need to modify a property setting on the publication. You need to set the publication to allow initialization from backup files. Yeah, that’s all there is to it. Just set this setting to True. It really would have been even more dandier if this could be performed within the New Publication Wizard though.

3. you will now need to Perform a full backup of your database. Backup the database to a directory or drive accessible by the SQL Server, such as e:\ted\ted.bak in my case.

4. Restore the backup into the subscriber sql server instance. Don’t know
how to restore from a SQL backup? Google it up. Easy way to restore is from the managemt studio.

5. Run the following stored procedure in the SQL Server Management Studio Query Window on your publisher server.

@publication ='YourPublicationName', --ie tedpub
@subscriber='SubscriberServerDBInstance', --servername in my case tedserver02@destination_db='SubscriberDatabase', -- teddb
@sync_type = 'initialize with backup',
@backupdevicetype = 'disk',
@backupdevicename = d:\ted.bak'

You will need to disable or change the job of the cleanup job to run as SA, I would just disable this job before you run the above command as you may encounter an error.

you will now see a new job in the sql agent just go to the job and right click go to properties and then set the job to run as sa.

round up
backup publication database
copy backup to subscriber server
restoring subscriber database
The transactional replication is now happily running between the two servers. Of course, the above process creates only a basic full database transactional replication. If your requirements are a litte bit more complicated, you might want to take a look at the options you can configure in the sp_addsubscription stored procedure.


  1. Do I need to Take backup and restore at subscriber end, each time when I need to do synchronization.

    1. Yep if you want to not replicate all the commands for hours your need a full backup and restore.

  2. This comment has been removed by the author.