创建并应用初始快照

本主题介绍如何使用 SQL Server Management Studio、Transact-SQL 或复制管理对象 (RMO) 在 SQL Server 2014 中创建和应用初始快照。 使用参数化筛选器的合并发布需要由两部分组成的快照。 有关详细信息,请参阅 为包含参数化筛选器的合并发布创建快照

本主题内容

使用 SQL Server Management Studio

默认情况下,如果运行 SQL Server 代理,在使用“新建发布向导”创建发布后,快照代理将立即生成快照。 然后,默认情况下将由分发代理(对于快照复制和事务复制)或合并代理(对于合并订阅)把此快照应用于所有订阅。 还可以使用 SQL Server Management Studio 和复制监视器生成快照。 有关启动复制监视器的信息,请参阅启动复制监视器

在 Management Studio 中创建快照

  1. 在 Management Studio中连接到发布服务器,然后展开服务器节点。

  2. 展开 “复制” 文件夹,再展开 “本地发布” 文件夹。

  3. 右键单击要为其创建快照的发布,然后单击 “查看快照代理状态”

  4. “查看快照代理状态 - <发布>”对话框中,单击“启动”。

快照代理生成快照后,将显示一条消息,例如“[100%] 已生成 17 个项目的快照”。

在复制监视器中创建快照

  1. 在复制监视器的左窗格中依次展开发布服务器组、发布服务器。

  2. 右键单击要为其生成快照的发布,再单击 “生成快照”

  3. 若要查看快照代理的状态,请单击 “代理” 选项卡。有关更多详细信息,请右键单击网格中的快照代理,再单击 “查看详细信息”

应用快照

  1. 快照生成后,通过用分发代理或合并代理同步订阅来应用此快照:

    • 如果代理设置为连续运行(事务复制下的默认设置),则快照生成后将自动应用。

    • 如果代理设置为根据计划运行,则在安排代理下次运行时应用快照。

    • 如果代理设置为按需运行,则在您下次运行代理时应用快照。

    有关同步订阅的详细信息,请参阅 Synchronize a Push SubscriptionSynchronize a Pull Subscription文件夹中打开。

“使用 Transact-SQL”

可通过创建并运行快照代理作业或通过批处理文件运行快照代理可执行文件,以编程方式创建初始快照。 初始快照生成后,该快照将在订阅首次同步时传输并应用到订阅服务器。 如果您在命令提示符处或通过批处理文件运行快照代理,则只要现有快照变为无效,您就需要重新运行此代理。

重要

如果可能,请在运行时提示用户输入安全凭据。 如果必须在脚本文件中存储凭据,则必须保护文件以防止未经授权的访问。

创建并运行快照代理作业以生成初始快照

  1. 创建快照发布、事务发布或合并发布。 有关详细信息,请参阅 Create a Publication

  2. sp_addpublication_snapshot (Transact-SQL) 执行。 指定 @publication 和以下参数:

    • @job_login,用于指定快照代理在分发服务器上运行时所用的 Windows 身份验证凭据。

    • @job_password****,它是提供的 Windows 凭据的密码。

    • (可选) 如果代理在连接到发布服务器时将使用SQL Server身份验证,则 @publisher_security_mode 的值为 0。 在这种情况下,还必须为@publisher_login和@publisher_password指定SQL Server身份验证登录信息。

    • (可选)快照代理作业的同步计划。 有关详细信息,请参阅 Specify Synchronization Schedules

    重要

    使用远程分发服务器配置发布服务器时,为所有参数提供的值(包括 job_loginjob_password)都会以纯文本方式发送到该分发服务器。 在执行此存储过程之前,应该对发布服务器及其远程分发服务器之间的连接进行加密。 有关详细信息,请参阅启用数据库引擎的加密连接(SQL Server 配置管理器)

  3. 向发布添加项目。 有关详细信息,请参阅 定义项目

  4. 在发布服务器上,对发布数据库执行 sp_startpublication_snapshot (Transact-SQL) ,指定步骤 1 中 @publication 的值。

运行快照代理以生成初始快照

  1. 创建快照发布、事务发布或合并发布。 有关详细信息,请参阅 Create a Publication

  2. 向发布添加项目。 有关详细信息,请参阅 定义项目

  3. 在命令提示符处或批处理文件中,通过运行 snapshot.exe 并指定下列命令行参数,启动 复制合并代理

    • -Publication

    • -Publisher

    • -Distributor

    • -PublisherDB

    • -ReplicationType

    如果您使用的是 SQL Server 身份验证,则还必须指定下列参数:

    • -DistributorLogin

    • -DistributorPassword

    • -DistributorSecurityMode = \@publisher_security_mode

    • -PublisherLogin

    • -PublisherPassword

    • -PublisherSecurityMode = \@publisher_security_mode

示例 (Transact-SQL)

此示例演示如何创建事务发布,并为新的发布添加快照代理作业(使用 sqlcmd 脚本变量)。 此示例还启动该作业。

-- To avoid storing the login and password in the script file, the values 
-- are passed into SQLCMD as scripting variables. For information about 
-- how to use scripting variables on the command line and in SQL Server
-- Management Studio, see the "Executing Replication Scripts" section in
-- the topic "Programming Replication Using System Stored Procedures".

DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publicationDB = N'AdventureWorks2012'; --publication database
SET @publication = N'AdvWorksCustomerTran'; -- transactional publication name
SET @login = $(Login);
SET @password = $(Password);

USE [AdventureWorks]

-- Enable transactional and snapshot replication on the publication database.
EXEC sp_replicationdboption 
  @dbname = @publicationDB, 
  @optname = N'publish',
  @value = N'true';

-- Execute sp_addlogreader_agent to create the agent job. 
EXEC sp_addlogreader_agent 
  @job_login = @login, 
  @job_password = @password,
  -- Explicitly specify the security mode used when connecting to the Publisher.
  @publisher_security_mode = 1;

-- Create new transactional publication, using the defaults. 
USE [AdventureWorks2012]
EXEC sp_addpublication 
  @publication = @publication, 
  @description = N'transactional publication';

-- Create a new snapshot job for the publication, using the defaults.
EXEC sp_addpublication_snapshot 
  @publication = @publication,
  @job_login = @login,
  @job_password = @password;

-- Start the Snapshot Agent job.
EXEC sp_startpublication_snapshot @publication = @publication;
GO

此示例创建一个合并发布,并为此发布添加一个快照代理作业(使用 sqlcmd 变量)。 此示例还启动该作业。

-- To avoid storing the login and password in the script file, the value 
-- is passed into SQLCMD as a scripting variable. For information about 
-- how to use scripting variables on the command line and in SQL Server
-- Management Studio, see the "Executing Replication Scripts" section in
-- the topic "Programming Replication Using System Stored Procedures".

DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publicationDB = N'AdventureWorks2012'; 
SET @publication = N'AdvWorksSalesOrdersMerge'; 
SET @login = $(Login);
SET @password = $(Password);

-- Enable merge replication on the publication database.
USE master
EXEC sp_replicationdboption 
  @dbname = @publicationDB, 
  @optname=N'merge publish',
  @value = N'true';

-- Create new merge publication, using the defaults. 
USE [AdventureWorks]
EXEC sp_addmergepublication 
  @publication = @publication, 
  @description = N'Merge publication.';

-- Create a new snapshot job for the publication, using the defaults.
EXEC sp_addpublication_snapshot 
  @publication = @publication,
  @job_login = @login,
  @job_password = @password;

-- Start the Snapshot Agent job.
EXEC sp_startpublication_snapshot @publication = @publication;
GO

以下命令行参数启动快照代理,以为合并发布生成快照。

注意

为便于阅读,添加了换行符。 但在批处理文件中,命令必须位于一行中。

@ECHO OFF
SET InstanceName=%computername%
REM<snippetstartmergesnapshot_10>
REM -- Declare variables
SET Publisher=%InstanceName%
SET PublicationDB=AdventureWorks2012 
SET Publication=AdvWorksSalesOrdersMerge 

REM --Start the Snapshot Agent to generate the snapshot for AdvWorksSalesOrdersMerge.
"C:\Program Files\Microsoft SQL Server\120\COM\SNAPSHOT.EXE" -Publication %Publication% 
-Publisher %Publisher% -Distributor %Publisher% -PublisherDB %PublicationDB% 
-ReplicationType 2 -OutputVerboseLevel 1 -DistributorSecurityMode 1 
REM</snippetstartmergesnapshot_10>

PAUSE

使用复制管理对象 (RMO)

快照代理将在创建发布后生成快照。 可以使用复制管理对象 (RMO) 和直接托管代码对复制代理功能的访问权限以编程的方式生成这些快照。 所使用的对象取决于复制的类型。 可以使用 SnapshotGenerationAgent 对象同步启动快照代理,也可以使用代理作业异步启动快照代理。 初始快照生成后,该快照将在订阅首次同步时传输并应用到订阅服务器。 只要现有快照不再包含有效的最新数据,您就需要重新运行代理。 有关详细信息,请参阅维护发布

重要

如果可能,请在运行时提示用户输入安全凭据。 如果必须存储凭据,请使用 Windows .NET Framework 提供的 Cryptographic Services Microsoft (加密服务)。

通过启动快照代理作业(异步)为快照发布或事务发布生成初始快照

  1. 使用 ServerConnection 类创建与发布服务器的连接。

  2. 创建的 TransPublication 类的实例。 设置发布的 NameDatabaseName 属性,并将 ConnectionContext 属性设置为步骤 1 中创建的连接。

  3. 调用 LoadProperties 方法以加载该对象的其余属性。 如果此方法返回 false,则说明步骤 2 中的发布属性定义不正确,或者此发布不存在。

  4. 如果 SnapshotAgentExists 的值为 false,请调用 CreateSnapshotAgent 为此发布创建快照代理作业。

  5. 调用 StartSnapshotGenerationAgentJob 方法以启动为此发布生成快照的代理作业。

  6. (可选)SnapshotAvailable 的值为 true 时,订阅服务器具有快照。

通过运行快照代理(同步)为快照发布或事务发布生成初始快照

  1. 创建 SnapshotGenerationAgent 类的实例,并设置下列所需属性:

  2. Transactional 的值设置为 SnapshotReplicationType

  3. 调用 GenerateSnapshot 方法。

通过启动快照代理作业(异步)为合并发布生成初始快照

  1. 使用 ServerConnection 类创建与发布服务器的连接。

  2. 创建的 MergePublication 类的实例。 设置发布的 NameDatabaseName 属性,并将 ConnectionContext 属性设置为步骤 1 中创建的连接。

  3. 调用 LoadProperties 方法以加载该对象的其余属性。 如果此方法返回 false,则说明步骤 2 中的发布属性定义不正确,或者此发布不存在。

  4. 如果 SnapshotAgentExists 的值为 false,请调用 CreateSnapshotAgent 为此发布创建快照代理作业。

  5. 调用 StartSnapshotGenerationAgentJob 方法以启动为此发布生成快照的代理作业。

  6. (可选)SnapshotAvailable 的值为 true 时,订阅服务器具有快照。

通过运行快照代理(同步)为合并发布生成初始快照

  1. 创建 SnapshotGenerationAgent 类的实例,并设置下列所需属性:

  2. Merge 的值设置为 ReplicationType

  3. 调用 GenerateSnapshot 方法。

示例 (RMO)

此示例同步运行快照代理,为事务发布生成初始快照。

// Set the Publisher, publication database, and publication names.
string publicationName = "AdvWorksProductTran";
string publicationDbName = "AdventureWorks2012";
string publisherName = publisherInstance;
string distributorName = publisherInstance;

SnapshotGenerationAgent agent;

try
{
    // Set the required properties for Snapshot Agent.
    agent = new SnapshotGenerationAgent();
    agent.Distributor = distributorName;
    agent.DistributorSecurityMode = SecurityMode.Integrated;
    agent.Publisher = publisherName;
    agent.PublisherSecurityMode = SecurityMode.Integrated;
    agent.Publication = publicationName;
    agent.PublisherDatabase = publicationDbName;
    agent.ReplicationType = ReplicationType.Transactional;

    // Start the agent synchronously.
    agent.GenerateSnapshot();

}
catch (Exception ex)
{
    // Implement custom application error handling here.
    throw new ApplicationException(String.Format(
        "A snapshot could not be generated for the {0} publication."
        , publicationName), ex);
}
' Set the Publisher, publication database, and publication names.
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks2012"
Dim publisherName As String = publisherInstance
Dim distributorName As String = publisherInstance

Dim agent As SnapshotGenerationAgent

Try
    ' Set the required properties for Snapshot Agent.
    agent = New SnapshotGenerationAgent()
    agent.Distributor = distributorName
    agent.DistributorSecurityMode = SecurityMode.Integrated
    agent.Publisher = publisherName
    agent.PublisherSecurityMode = SecurityMode.Integrated
    agent.Publication = publicationName
    agent.PublisherDatabase = publicationDbName
    agent.ReplicationType = ReplicationType.Transactional

    ' Start the agent synchronously.
    agent.GenerateSnapshot()

Catch ex As Exception
    ' Implement custom application error handling here.
    Throw New ApplicationException(String.Format( _
     "A snapshot could not be generated for the {0} publication." _
     , publicationName), ex)
End Try

此示例异步启动代理作业,为事务发布生成初始快照。

// Set the Publisher, publication database, and publication names.
string publicationName = "AdvWorksProductTran";
string publicationDbName = "AdventureWorks2012";
string publisherName = publisherInstance;

TransPublication publication;

// Create a connection to the Publisher using Windows Authentication.
ServerConnection conn;
conn = new ServerConnection(publisherName);

try
{
    // Connect to the Publisher.
    conn.Connect();

    // Set the required properties for an existing publication.
    publication = new TransPublication();
    publication.ConnectionContext = conn;
    publication.Name = publicationName;
    publication.DatabaseName = publicationDbName;

    if (publication.LoadProperties())
    {
        // Start the Snapshot Agent job for the publication.
        publication.StartSnapshotGenerationAgentJob();
    }
    else
    {
        throw new ApplicationException(String.Format(
            "The {0} publication does not exist.", publicationName));
    }
}
catch (Exception ex)
{
    // Implement custom application error handling here.
    throw new ApplicationException(String.Format(
        "A snapshot could not be generated for the {0} publication."
        , publicationName), ex);
}
finally
{
    conn.Disconnect();
}
' Set the Publisher, publication database, and publication names.
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks2012"
Dim publisherName As String = publisherInstance

Dim publication As TransPublication

' Create a connection to the Publisher using Windows Authentication.
Dim conn As ServerConnection
conn = New ServerConnection(publisherName)

Try
    ' Connect to the Publisher.
    conn.Connect()

    ' Set the required properties for an existing publication.
    publication = New TransPublication()
    publication.ConnectionContext = conn
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName

    If publication.LoadProperties() Then
        ' Start the Snapshot Agent job for the publication.
        publication.StartSnapshotGenerationAgentJob()
    Else
        Throw New ApplicationException(String.Format( _
         "The {0} publication does not exist.", publicationName))
    End If
Catch ex As Exception
    ' Implement custom application error handling here.
    Throw New ApplicationException(String.Format( _
     "A snapshot could not be generated for the {0} publication." _
     , publicationName), ex)
Finally
    conn.Disconnect()
End Try

另请参阅

创建发布
创建请求订阅
创建推送订阅
指定同步计划
创建并应用快照
使用快照初始化订阅
复制管理对象概念
复制安全最佳做法
Replication System Stored Procedures Concepts
将 sqlcmd 与脚本变量结合使用