Create Tran. Replication from Backup

Most SQL Server DBAs like to use GUI to do everything, so they also prefer to create transactional replication by using the creation wizard. By the way, the GUI wizard only supports initializing subscription by snapshot, which is actually export the whole published tables to bcp files, truncate those tables in subscription database, then import the bcp files. If the transactional replication is for the whole database, i.e. all tables, this snapshot initialization process can take very long time to complete.
Another way to create transactional replication for the whole database is "initialize susbscription from database backup", but it can only be done by SQL script. Also, there are some tricky things that the SQL Server BOL didn't mention. Let me show you the detailed steps, I used it to create transactional replication for a 1TB database.

1. At 12:00am, create the replication publication in publisher:
a. Open SQL Management Studio, Connect the publisher sql server by an sysadmin account;
b. Expand “Replication” -> right-click "Local Publications" -> "New Publication";
c. In welcome page, click Next -> choose the publication database -> Next;
d. Select publication type as "Transactional publication" -> Next;
e. Select all tables as the published articles;
f. Click "Article Properties" -> "Set Properties of All Table Articles";
g. In "Action if name is in use", select "Keep existing object unchanged";
h. Click Next -> then Next again to skip filter;
i. In "Snapshot Agent" page, ensure no any checkbox ticked -> Next;
j. In "Agent Security", tick "Use the security settings from the Snapshot Agent", then click "Security Settings…";
k. Select "Run under the SQL Agent service account", and "Using the following SQL Server login:", enter "sa" login and its password;
l. Click OK -> Next;
m. In "Wizard Actions" page -> tick "Create the publication";
n. In "Complete the Wizard" page -> type the publication name -> Finish;
o. After the publication created successfully, go back to SQL Management Studio -> expand "Replication" -> "Local Publications" -> right-click the newly created publication -> click "Properties";
p. In "Subscription Options" -> check "Snaphot always available" is "False", and "Allow pull subscriptions" is "True" -> set "Allow initialization from backup files" to "True".

2. Disable the "Distribution cleanup" job in distributor server: (this step didn't mentioned in BOL)
a. In SQL Management Studio, connect to the distributor sql server by and sysadmin account;
b. "SQL Server Agent" -> "Jobs" -> right-click "Distribution clean up: distribution" -> click "Disable".

3. Restore the published database on subscriber server:
a. copy the latest published database full backup file, from publisher to subscriber;
b. In SQL Management Studio, connect to subscriber sql server by an sysadmin account;
c. Execute the following query to restore the database in subscriber:
RESTORE DATABASE <db> FROM DISK = N'<full_bak_file>' WITH NORECOVERY, REPLACE;
d. In SQL Management Studio, connect to publisher by an sysadmin account;
e. Execute the following query to backup transaction log of published database in publisher:
BACKUP LOG <db> TO DISK = N'\\<subscriber>\<shared folder>\DB_log.trn';
f. Connect to subscriber, execute the following query to restore the transaction log:
RESTORE LOG <db> FROM DISK = N'\\<subscriber>\<shared folder>\DB_log.trn';

4. Create the pull subscription
a. In SQL Management Studio, connect to publisher by an sysadmin account;
b. Execute the following query:
use <db>
exec sp_addsubscription @publication = N'<publication>', @subscriber = N'<subscriber>', @destination_db = N'<db>', @subscription_type = N'Pull', @sync_type = N'initialize with backup', @article = N'all', @update_mode = N'read only', @subscriber_type = 0, @backupdevicetype = 'DISK', @backupdevicename = N'\\<subscriber>\<shared folder>\DB_log.trn';
c. In SQL Management Studio, connect to subscriber by an sysadmin account;
d. Execute the following query:
use <db>
exec sp_addpullsubscription @publisher = N'<publisher>', @publication = N'<publication>', @publisher_db = N'<db>', @independent_agent = N'True', @subscription_type = N'pull', @description = N'', @update_mode = N'read only', @immediate_sync = 0;
exec sp_addpullsubscription_agent @publisher = N'<publisher>', @publisher_db = N'<db>', @publication = '<publication>', @distributor = N'<distributor>', @distributor_security_mode = 0, @distributor_login = N'sa', @distributor_password = N'<pwd>', @enabled_for_syncmgr = N'False', @frequency_type = 64, @frequency_interval = 0, @frequency_relative_interval = 0, @frequency_recurrence_factor = 0, @frequency_subday = 0, @frequency_subday_interval = 0, @active_start_time_of_day = 0, @active_end_time_of_day = 235959, @active_start_date = 0, @active_end_date = 0, @alt_snapshot_folder = N'', @working_directory = N'', @use_ftp = N'False', @job_login = N'<sql agent domain account>', @job_password = '<domain account pwd>', @publication_type = 0;
e. Check the replication is functioning normal, by opening Replication Monitor in SQL Management Studio -> Publisher server -> right-click Replication -> Launch Replication Monitor -> publisher server -> the publication -> Tracer Tokens -> Insert Tracer.

5. Re-enable the "Distribution cleanup" job in distributor server:
a. In SQL Management Studio, connect to distributor by an sysadmin account;
b. "SQL Server Agent" -> "Jobs" -> right-click "Distribution clean up: distribution" -> click "Enable".

6. Find and resolve Orphaned DB Users in the subscription database:
a. Run the following query in subscriber to find any user accounts needed to map:
EXEC <the DB>.dbo.sp_change_users_login 'Report'
b. Compare the result with sql logins;
c. Run the following query to map any orphaned db user to its sql login:
ALTER USER <dbuser> WITH LOGIN = <sqllogin>;

7. Truncate transaction log space in subscription database:
a. In SQL Management Studio, connect to subscriber;
b. Databases -> right-click the DB -> Properties -> Options -> set "Recovery model:" as "Simple" -> OK;
c. Databases -> right-click the DB -> Tasks -> Shrink -> Files -> Select "File type:" as "Log", "Shrink action" as "Release unused space" -> OK;
d. Check the transaction log size is reduced, by right-click the database -> Reports -> Disk Usage.

No comments:

Post a Comment