如何:透過 FTP 傳遞快照集 (複寫 Transact-SQL 程式設計)

可以設定讓快照集檔案可在 FTP 伺服器上使用的選項,而且可以透過程式設計方式使用複寫預存程序來修改這些 FTP 設定。使用的程序取決於發行集的類型而定。FTP 快照集傳遞只會搭配提取訂閱一起使用。

安全性注意事項安全性注意事項

若要改善安全性,我們建議您在透過網際網路使用 FTP 快照集傳遞時,實作虛擬私人網路 (VPN)。如需詳細資訊,請參閱<使用 VPN 透過網際網路發行資料>。

針對快照式或交易式發行集啟用 FTP 快照集傳遞

  • 在發行集資料庫的發行者上,執行 sp_addpublication。指定 @publication@enabled_for_internet 的 true 值,以及下列參數的適當值:

    • @ftp_address - 用於傳遞快照集之 FTP 伺服器的位址。

    • (選擇性) @ftp_port - FTP 伺服器所使用的通訊埠。

    • (選擇性) @ftp_subdirectory - 指派給 FTP 登入之預設 FTP 目錄的子目錄。例如,如果 FTP 伺服器根目錄為 \\ftpserver\home,而您想將快照集儲存在 \\ftpserver\home\snapshots 中,請為 @ftp_subdirectory 指定 \snapshots\ftp (複寫會在建立快照集檔案時,在快照集資料夾路徑後加上 'ftp')。

    • (選擇性) @ftp_login - 連接到 FTP 伺服器時所使用的登入帳戶。

    • (選擇性) @ftp_password - FTP 登入的密碼。

      安全性注意事項安全性注意事項

      在最佳安全性作法中,不允許匿名登入 FTP 伺服器。

    [!附註]

    快照集代理程式必須有您指定之目錄的寫入權限,而散發代理程式或合併代理程式則必須有讀取權限。如果使用提取訂閱,則您必須指定共用目錄為通用命名慣例 (UNC) 路徑,例如 \\ftpserver\home\snapshots。如需詳細資訊,請參閱<保護快照集資料夾>。

    這會建立使用 FTP 的發行集。如需詳細資訊,請參閱<如何:建立發行集 (複寫 Transact-SQL 程式設計)>。

針對合併式發行集啟用 FTP 快照集傳遞

  • 在發行集資料庫的發行者上,執行 sp_addmergepublication。指定 @publication@enabled_for_internet 的 true 值,以及下列參數的適當值:

    • @ftp_address - 用於傳遞快照集之 FTP 伺服器的位址。

    • (選擇性) @ftp_port - FTP 伺服器所使用的通訊埠。

    • (選擇性) @ftp_subdirectory - 指派給 FTP 登入之預設 FTP 目錄的子目錄。例如,如果 FTP 伺服器根目錄為 \\ftpserver\home,而您想將快照集儲存在 \\ftpserver\home\snapshots 中,請為 @ftp_subdirectory 指定 \snapshots\ftp (複寫會在建立快照集檔案時,在快照集資料夾路徑後加上 'ftp')。

    • (選擇性) @ftp_login - 連接到 FTP 伺服器時所使用的登入帳戶。

    • (選擇性) @ftp_password - FTP 登入的密碼。

      安全性注意事項安全性注意事項

      在最佳安全性作法中,不允許匿名登入 FTP 伺服器。

    [!附註]

    快照集代理程式必須有您指定之目錄的寫入權限,而散發代理程式或合併代理程式則必須有讀取權限。如果使用提取訂閱,則您必須指定共用目錄為通用命名慣例 (UNC) 路徑,例如 \\ftpserver\home\snapshots。如需詳細資訊,請參閱<保護快照集資料夾>。

    這會建立使用 FTP 的發行集。如需詳細資訊,請參閱<如何:建立發行集 (複寫 Transact-SQL 程式設計)>。

針對使用 FTP 快照集傳遞的快照式或交易式發行集建立提取訂閱

  1. 在訂閱資料庫的訂閱者上,執行 sp_addpullsubscription。指定 @publisher@publication

    • 在訂閱資料庫的訂閱者上,執行 sp_addpullsubscription_agent。指定 @publisher@publisher_db@publication、針對 @job_login@job_password 的 Microsoft Windows 認證 (散發代理程式在訂閱者上執行時會使用此認證)、@use_ftp 的 true 值。
  2. 在發行集資料庫的發行者上,執行 sp_addsubscription,以註冊提取訂閱。如需詳細資訊,請參閱<如何: 建立提取訂閱 (複寫 Transact-SQL 程式設計)>。

針對使用 FTP 快照集傳遞的合併式發行集建立提取訂閱

  1. 在訂閱資料庫的訂閱者上,執行 sp_addmergepullsubscription。指定 @publisher@publication

  2. 在訂閱資料庫的訂閱者上,執行 sp_addmergepullsubscription_agent。指定 @publisher@publisher_db@publication、針對 @job_login@job_password 的 Windows 認證 (散發代理程式在訂閱者上執行時會使用此認證)、@use_ftp 的 true 值。

  3. 在發行集資料庫的發行者上,執行 sp_addmergesubscription,以註冊提取訂閱。如需詳細資訊,請參閱<如何: 建立提取訂閱 (複寫 Transact-SQL 程式設計)>。

針對快照式或交易式發行集變更一或多個 FTP 快照集傳遞設定

  1. 在發行集資料庫的發行者上,執行 sp_changepublication。針對 @property 指定下列其中一個值,並針對 @value 指定此設定的新值:

    • ftp_address - 用於傳遞快照集之 FTP 伺服器的位址。

    • ftp_port - FTP 伺服器所使用的通訊埠。

    • ftp_subdirectory - 用於 FTP 快照集之預設 FTP 目錄的子目錄。

    • ftp_login - 用於連接 FTP 伺服器的登入。

    • ftp_password - FTP 登入的密碼。

    安全性注意事項安全性注意事項

    可能的話,會在執行階段提示使用者輸入其認證。如果將認證儲存在指令碼檔案中,您必須維護此檔案的安全。

  2. (選擇性) 針對變更的每一個 FTP 設定重複步驟 1。

  3. (選擇性) 若要停用 FTP 快照集傳遞,請在發行集資料庫的發行者上執行 sp_changepublication。針對 @property 指定 enabled_for_internet 值及針對 @value 指定 false 值。

針對合併式發行集變更 FTP 快照集傳遞設定

  1. 在發行集資料庫的發行者上,執行 sp_changemergepublication。針對 @property 指定下列其中一個值,並針對 @value 指定此設定的新值:

    • ftp_address - 用於傳遞快照集之 FTP 伺服器的位址。

    • ftp_port - FTP 伺服器所使用的通訊埠。

    • ftp_subdirectory - 用於 FTP 快照集之預設 FTP 目錄的子目錄。

    • ftp_login - 用於連接 FTP 伺服器的登入。

    • ftp_password - FTP 登入的密碼。

    安全性注意事項安全性注意事項

    可能的話,會在執行階段提示使用者輸入其認證。如果將認證儲存在指令碼檔案中,您必須維護此檔案的安全。

  2. (選擇性) 針對變更的每一個 FTP 設定重複步驟 1。

  3. (選擇性) 若要停用 FTP 快照集傳遞,請在發行集資料庫的發行者上執行 sp_changemergepublication。針對 @property 指定 enabled_for_internet 的值及針對 @value 指定 false 的值。

範例

下列範例會建立允許訂閱者的合併式發行集,以使用 FTP 存取快照集資料。此訂閱者在存取 FTP 共用時,應該使用安全 VPN 連接。sqlcmd 指令碼變數是用來提供登入和密碼值。如需詳細資訊,請參閱<以指令碼變數使用 sqlcmd>。

-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). 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".

-- Declarations for adding a merge publication.
DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
DECLARE @ftp_server AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
DECLARE @ftp_login AS sysname;
DECLARE @ftp_password AS sysname;
DECLARE @ftp_directory AS sysname;
DECLARE @snapshot_folder AS sysname;
DECLARE @article AS sysname;
DECLARE @owner AS sysname;
SET @publicationDB = N'AdventureWorks2008R2'; 
SET @publication = N'AdvWorksSalesOfferMergeFtp'; 
SET @ftp_server = $(Server);
SET @login = $(Login);
SET @password = $(Password);
SET @ftp_login = $(FtpLogin);
SET @ftp_password = $(FtpPassword);
SET @ftp_directory = N'\snapshots\ftp';
-- The snapshot folder is the root FTP folder on the server 
-- with the \snapshot subdirectory.
SET @snapshot_folder = $(AlternateFolder);
SET @article = N'SpecialOffer'; 
SET @owner = N'Sales' 

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

-- Create a new merge publication, enabling FTP snapshot delivery. 
-- Specify the publication compatibility level or it will default to 
-- SQL Server 2000.
USE [AdventureWorks2008R2]
EXEC sp_addmergepublication 
-- Specify the required parameters.
    @publication = @publication,
    @publication_compatibility_level = N'90RTM',
    @enabled_for_internet = N'true',
    @snapshot_in_defaultfolder = N'true',
    @alt_snapshot_folder = @snapshot_folder,
    @ftp_address = @ftp_server,
    @ftp_subdirectory = @ftp_directory,
    @ftp_login = @ftp_login,
    @ftp_password = @ftp_password;

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

-- Add an unfiltered article for the Customer table.
EXEC sp_addmergearticle 
    @publication = @publication, 
    @article = @article, 
    @source_object = @article, 
    @type = N'table', 
    @source_owner = @owner, 
    @destination_owner = @owner, 
    @column_tracking = N'true'; 

-- Start the snapshot job for the publication.
EXEC sp_startpublication_snapshot 
    @publication = @publication;
GO

下列範例會建立合併式發行集的訂閱,其中的訂閱者會使用 FTP 取得快照集。此訂閱者在存取 FTP 共用時,應該使用安全 VPN 連接。sqlcmd 指令碼變數是用來提供登入和密碼值。如需詳細資訊,請參閱<以指令碼變數使用 sqlcmd>。

-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). 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".

-- Execute this batch at the Publisher.
DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
SET @publication = N'AdvWorksSalesOfferMergeFtp';
SET @subscriber = $(SubServer);
SET @subscriptionDB = N'AdventureWorks2008R2Replica';

-- At the Publisher, register the subscription, using the defaults.
EXEC sp_addmergesubscription 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @subscriber_db = @subscriptionDB, 
    @subscription_type = N'pull', 
    @subscriber_type = N'local', 
    @subscription_priority = 0, 
    @sync_type = N'Automatic';
GO
-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). 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".

-- Execute this batch at the Subscriber.
DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
DECLARE @publisher AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
SET @publicationDB = N'AdventureWorks2008R2'; 
SET @publication = N'AdvWorksSalesOfferMergeFtp'; 
SET @publisher = $(PubServer);
SET @login = $(Login);
SET @password = $(Password);
SET @subscriber = $(SubServer);
SET @subscriptionDB = N'AdventureWorks2008R2Replica';

EXEC sp_addmergepullsubscription 
    @publisher = @publisher, 
    @publication = @publication, 
    @publisher_db = @publicationDB, 
    @subscriber_type = N'Local', 
    @subscription_priority = 0, 
    @sync_type = N'Automatic';

exec sp_addmergepullsubscription_agent 
    @publisher = @publisher, 
    @publisher_db = @publicationDB, 
    @publication = @publication, 
    @distributor = @publisher, 
    @distributor_security_mode = 1, 
    @use_ftp = N'true', 
    @job_login = @login, 
    @job_password = @password, 
    @publisher_security_mode = 1, 
    @use_web_sync = 0;
GO