초기 스냅숏 만들기 및 적용

이 항목에서는 SQL Server Management Studio, Transact-SQL 또는 RMO(복제 관리 개체)를 사용하여 SQL Server 2012에서 초기 스냅숏을 만들고 적용하는 방법에 대해 설명합니다. 매개 변수가 있는 필터를 사용하는 병합 게시에는 두 부분으로 구성된 스냅숏이 필요합니다. 자세한 내용은 매개 변수가 있는 필터로 병합 게시에 대한 스냅숏 만들기를 참조하십시오.

항목 내용

  • 다음을 사용하여 초기 스냅숏을 만들고 적용하려면

    SQL Server Management Studio

    Transact-SQL

    RMO(복제 관리 개체)

SQL Server Management Studio 사용

기본적으로 SQL Server 에이전트가 실행 중이면 새 게시 마법사로 게시를 만든 직후 스냅숏 에이전트에서 스냅숏을 생성합니다. 이렇게 생성된 스냅숏은 기본적으로 모든 구독에 대해 배포 에이전트(스냅숏 및 트랜잭션 복제의 경우)나 병합 에이전트(병합 구독의 경우)에 의해 적용됩니다. 스냅숏은 SQL Server Management Studio 및 복제 모니터를 사용하여 생성할 수도 있습니다. 복제 모니터 시작 방법은 복제 모니터 시작을 참조하십시오.

Management Studio에서 스냅숏을 만들려면

  1. Management Studio에서 게시자에 연결한 다음 해당 서버 노드를 확장합니다.

  2. 복제 폴더를 확장한 다음 로컬 게시 폴더를 확장합니다.

  3. 스냅숏을 생성할 게시를 마우스 오른쪽 단추로 클릭한 다음 스냅숏 에이전트 상태 보기를 클릭합니다.

  4. 스냅숏 에이전트 상태 보기 - <Publication> 대화 상자에서 시작을 클릭합니다.

스냅숏 에이전트에서 스냅숏 생성을 마치면 "[100%] 17개 아티클의 스냅숏이 생성되었습니다"라는 메시지가 표시됩니다.

복제 모니터에서 스냅숏을 만들려면

  1. 복제 모니터에서 왼쪽 창의 게시자 그룹을 확장한 다음 게시자를 확장합니다.

  2. 스냅숏을 생성할 게시를 마우스 오른쪽 단추로 클릭한 다음 스냅숏 생성을 클릭합니다.

  3. 스냅숏 에이전트 상태를 보려면 에이전트 탭을 클릭합니다. 자세한 내용을 보려면 표에서 스냅숏 에이전트를 마우스 오른쪽 단추로 클릭한 다음 자세히 보기를 클릭합니다.

스냅숏을 적용하려면

  • 스냅숏이 생성된 후에는 구독을 배포 에이전트나 병합 에이전트와 동기화하여 스냅숏을 적용합니다.

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

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

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

    구독 동기화 방법은 밀어넣기 구독 동기화끌어오기 구독 동기화를 참조하십시오.

맨 위로 이동 링크와 함께 사용되는 화살표 아이콘[Top]

Transact-SQL 사용

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

보안 정보보안 정보

가능한 경우 런타임 시 사용자에게 보안 자격 증명을 입력하라는 메시지가 표시됩니다. 자격 증명을 스크립트 파일에 저장해야 하는 경우에는 파일에 무단으로 액세스하지 못하도록 보안을 설정해야 합니다.

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

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

  2. sp_addpublication_snapshot(Transact-SQL)을 실행합니다. 이때 @publication 및 다음 매개 변수를 지정합니다.

    • 배포자에서 스냅숏 에이전트를 실행하는 Windows 인증 자격 증명을 지정하는 @job_login.

    • 제공된 Windows 자격 증명의 암호인 @job_password.

    • (옵션) 게시자에 연결할 때 에이전트가 SQL Server 인증을 사용하면 @publisher_security_mode에 값 0. 이 경우 @publisher_login@publisher_password에 SQL Server 인증 로그인 정보도 지정해야 합니다.

    • (옵션) 스냅숏 에이전트 작업에 대한 동기화 일정. 자세한 내용은 동기화 일정 지정을 참조하십시오.

    보안 정보보안 정보

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

  3. 아티클을 게시에 추가합니다. 자세한 내용은 아티클 정의를 참조하십시오.

  4. 게시 데이터베이스의 게시자에서 sp_startpublication_snapshot(Transact-SQL)을 실행하고 1단계에서 만든 @publication의 값을 지정합니다.

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

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

  2. 아티클을 게시에 추가합니다. 자세한 내용은 아티클 정의를 참조하십시오.

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

    • -Publication

    • -Publisher

    • -Distributor

    • -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

다음 명령줄 인수는 스냅숏 에이전트를 시작하여 병합 게시에 대한 스냅숏을 생성합니다.

[!참고]

가독성을 높이기 위해 줄 바꿈이 추가되었지만 배치 파일에서 명령은 단일 행으로 작성해야 합니다.

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\110\COM\SNAPSHOT.EXE" -Publication %Publication% 
-Publisher %Publisher% -Distributor %Publisher% -PublisherDB %PublicationDB% 
-ReplicationType 2 -OutputVerboseLevel 1 -DistributorSecurityMode 1 

맨 위로 이동 링크와 함께 사용되는 화살표 아이콘[Top]

RMO(복제 관리 개체) 사용

스냅숏 에이전트는 게시가 만들어진 후 스냅숏을 생성합니다. RMO(복제 관리 개체)를 사용하여 이러한 스냅숏을 프로그래밍 방식으로 생성하고 복제 에이전트 기능에 액세스하도록 관리 코드에 지시할 수 있습니다. 사용하는 개체는 복제 유형에 따라 달라집니다. 스냅숏 에이전트는 SnapshotGenerationAgent 개체를 사용하여 동기적으로 시작하거나 에이전트 작업을 사용하여 비동기적으로 시작할 수 있습니다. 생성된 초기 스냅숏은 구독이 처음 동기화될 때 구독자로 전송되어 적용됩니다. 기존 스냅숏에 유효한 최신 데이터가 필요하게 될 때마다 에이전트를 다시 실행해야 합니다. 자세한 내용은 게시 유지 관리를 참조하십시오.

보안 정보보안 정보

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

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

  1. ServerConnection 클래스를 사용하여 게시자 연결을 만듭니다.

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

  3. LoadProperties 메서드를 호출하여 개체의 나머지 속성을 로드합니다. 이 메서드가 false를 반환하는 경우 2단계에서 게시 속성이 올바르게 정의되지 않았거나 게시가 없습니다.

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

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

  6. (옵션) SnapshotAvailable 값이 true이면 구독자에서 스냅숏을 사용할 수 있습니다.

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

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

  2. ReplicationTypeTransactional 또는 Snapshot 값을 설정합니다.

  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. ReplicationTypeMerge 값을 설정합니다.

  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

맨 위로 이동 링크와 함께 사용되는 화살표 아이콘[Top]

참고 항목

개념

게시 만들기

끌어오기 구독 만들기

밀어넣기 구독 만들기

동기화 일정 지정

스냅숏 만들기 및 적용

스냅숏으로 구독 초기화

복제 관리 개체 개념

복제 보안을 위한 최선의 구현 방법

복제 시스템 저장 프로시저 개념

스크립팅 변수와 함께 sqlcmd 사용