다음을 통해 공유


초기 스냅샷 만들기 및 적용

이 항목에서는 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. 스냅샷이 생성되면 구독을 배포 에이전트 또는 병합 에이전트와 동기화하여 적용합니다.

    • 에이전트가 지속적으로 실행되도록 설정된 경우(트랜잭션 복제의 기본값) 스냅샷이 생성된 후 자동으로 적용됩니다.

    • 에이전트가 일정에 따라 실행되도록 설정된 경우 다음에 에이전트가 실행되도록 예약될 때 스냅샷이 적용됩니다.

    • 에이전트가 요청 시 실행되도록 설정된 경우 다음에 에이전트를 실행할 때 적용됩니다.

    구독 동기화에 대한 자세한 내용은 밀어넣기 구독 동기화끌어오기 구독 동기화를 참조하세요.

Transact-SQL 사용

스냅샷 에이전트 작업을 만들고 실행하거나 일괄 처리 파일에서 스냅샷 에이전트 실행 파일을 실행하여 초기 스냅샷을 프로그래밍 방식으로 만들 수 있습니다. 초기 스냅샷이 생성되면 구독이 처음 동기화될 때 구독자에 전송되고 구독자에서 적용됩니다. 명령 프롬프트 또는 일괄 처리 파일에서 스냅샷 에이전트를 실행하는 경우 기존 스냅샷이 유효하지 않을 때마다 에이전트를 다시 실행해야 합니다.

중요합니다

가능한 경우 런타임 시 사용자에게 보안 자격 증명을 입력하라는 메시지가 표시됩니다. 스크립트 파일에 자격 증명을 저장해야 하는 경우 무단 액세스를 방지하기 위해 파일을 보호해야 합니다.

스냅샷 에이전트 작업을 만들고 실행하여 초기 스냅샷을 생성하려면

  1. 스냅샷, 트랜잭션 또는 병합 게시를 만듭니다. 자세한 내용은 게시 만들기를 참조하세요.

  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 인증 로그인 정보도 지정해야 합니다.

    • (선택 사항) 스냅샷 에이전트 작업에 대한 동기화 일정입니다. 자세한 내용은 동기화 일정 지정을 참조하세요.

    중요합니다

    원격 배포자를 사용하여 게시자를 구성할 때 job_loginjob_password 비롯한 모든 매개 변수에 제공된 값이 일반 텍스트로 배포자에 전송됩니다. 이 저장 프로시저를 실행하기 전에 게시자와 해당 원격 배포자 간의 연결을 암호화해야 합니다. 자세한 내용은 데이터베이스 엔진에 대한 암호화된 연결 사용(SQL Server 구성 관리자)을 참조하세요.

  3. 출판물에 기사를 추가합니다. 자세한 내용은 아티클 정의를 참조하세요.

  4. 게시 데이터베이스의 게시자에서 1단계에서 가져온 @publication 값을 사용하여 sp_startpublication_snapshot(Transact-SQL)을 실행합니다.

스냅샷 에이전트를 실행하여 초기 스냅샷을 생성하려면

  1. 스냅샷 게시, 트랜잭션 게시 또는 병합 게시를 생성합니다. 자세한 내용은 게시 만들기를 참조하세요.

  2. 출판물에 기사를 추가합니다. 자세한 내용은 아티클 정의를 참조하세요.

  3. 명령 프롬프트 또는 일괄 처리 파일에서 다음 명령줄 인수를 지정하여 snapshot.exe실행하여 복제 스냅샷 에이전트를 시작합니다.

    • -출판

    • -게시자

    • -분배자

    • -PublisherDB

    • -ReplicationType

    SQL Server 인증을 사용하는 경우 다음 인수도 지정해야 합니다.

    • -DistributorLogin

    • -DistributorPassword

    • -DistributorSecurityMode = 0

    • -PublisherLogin

    • -PublisherPassword

    • -PublisherSecurityMode = 0

예시(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 개체를 사용하여 동기적으로 시작하거나, 에이전트 작업을 사용하여 비동기적으로 시작할 수 있습니다. 초기 스냅샷이 생성되면 구독이 처음 동기화될 때 구독자에 전송되고 적용됩니다. 기존 스냅샷에 유효한 up-to-date 데이터가 더 이상 포함되지 않을 때마다 에이전트를 다시 실행해야 합니다. 자세한 내용은 게시 유지 관리를 참조하세요.

중요합니다

가능한 경우 런타임 시 사용자에게 보안 자격 증명을 입력하라는 메시지가 표시됩니다. 자격 증명을 저장해야 하는 경우 Microsoft Windows .NET Framework에서 제공하는 암호화 서비스를 사용합니다.

스냅샷 에이전트 작업을 시작하여 스냅샷 또는 트랜잭션 게시에 대한 초기 스냅샷을 생성하려면(비동기)

  1. 연결을 만들려면 ServerConnection 클래스를 사용하여 게시자에 연결하세요.

  2. TransPublication 클래스의 인스턴스를 만듭니다. 게시에 Name 대한 속성 및 DatabaseName 속성을 설정하고 1단계에서 만든 연결로 속성을 설정합니다 ConnectionContext .

  3. 메서드를 LoadProperties 호출하여 개체의 나머지 속성을 로드합니다. 이 메서드가 반환 false되면 2단계의 게시 속성이 잘못 정의되었거나 게시가 존재하지 않습니다.

  4. SnapshotAgentExists 값이 false인 경우 CreateSnapshotAgent를 호출하여 이 게시에 대한 스냅샷 에이전트 작업을 만듭니다.

  5. 메서드를 StartSnapshotGenerationAgentJob 호출하여 이 게시에 대한 스냅샷을 생성하는 에이전트 작업을 시작합니다.

  6. (선택 사항) SnapshotAvailable의 값이 true일 때, 구독자는 스냅샷을 사용할 수 있습니다.

스냅샷 에이전트를 실행하여 스냅샷 또는 트랜잭션 게시에 대한 초기 스냅샷을 생성하려면(동기)

  1. 클래스의 인스턴스를 SnapshotGenerationAgent 만들고 다음 필수 속성을 설정합니다.

  2. `ReplicationType`에 대해 Transactional 또는 Snapshot의 값을 설정하세요.

  3. GenerateSnapshot 메서드를 호출합니다.

스냅샷 에이전트 작업을 시작하여 병합 게시에 대한 초기 스냅샷을 생성하려면(비동기)

  1. 연결을 만들려면 ServerConnection 클래스를 사용하여 게시자에 연결하세요.

  2. MergePublication 클래스의 인스턴스를 만듭니다. 게시에 Name 대한 속성 및 DatabaseName 속성을 설정하고 1단계에서 만든 연결로 속성을 설정합니다 ConnectionContext .

  3. 메서드를 LoadProperties 호출하여 개체의 나머지 속성을 로드합니다. 이 메서드가 반환 false되면 2단계의 게시 속성이 잘못 정의되었거나 게시가 존재하지 않습니다.

  4. SnapshotAgentExists 값이 false인 경우 CreateSnapshotAgent를 호출하여 이 게시에 대한 스냅샷 에이전트 작업을 만듭니다.

  5. 메서드를 StartSnapshotGenerationAgentJob 호출하여 이 게시에 대한 스냅샷을 생성하는 에이전트 작업을 시작합니다.

  6. (선택 사항) SnapshotAvailable의 값이 true일 때, 구독자가 스냅샷을 사용할 수 있습니다.

스냅샷 에이전트를 실행하여 병합 게시에 대한 초기 스냅샷을 생성하려면(동기)

  1. 클래스의 인스턴스를 SnapshotGenerationAgent 만들고 다음 필수 속성을 설정합니다.

  2. MergeReplicationType 값을 설정합니다.

  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

또한 참조하십시오

게시 만들기
풀 구독 생성
푸시 구독 생성
동기화 일정 지정
스냅샷 만들기 및 적용
스냅샷을 사용하여 구독 초기화
복제 관리 개체 개념
복제 보안 모범 사례
복제 시스템 저장 프로시저 개념
스크립팅 변수와 함께 sqlcmd 사용