透過 FTP 傳遞快照集

適用於:SQL Server

此主題說明如何使用 SQL Server Management Studio 或 Transact-SQL,在 SQL Server 中透過 FTP 傳遞快照集。

依預設,快照集儲存在定義為「通用命名慣例」(UNC) 共用的資料夾中。 複寫還允許您指定「檔案傳輸通訊協定」(FTP) 共用,而不是 UNC 共用。 若要使用 FTP,您必須設定 FTP 伺服器,然後設定使用 FTP 的一個發行集和一個或多個訂閱。 如需有關如何設定 FTP 伺服器的詳細資訊,請參閱 Internet Information Services (IIS) 文件集。 如果您為發行集指定 FTP 資訊,依預設,該發行集的訂閱會使用 FTP。 只有當執行 IIS 的電腦與「散發者」之間被防火牆隔開時,Web 同步處理才會使用 FTP。 在此情況下,可以使用 FTP,從散發者和執行 IIS 的電腦來傳送快照集 (快照集一定會使用 HTTPS 傳送到訂閱者)。

重要

建議您使用「Microsoft Windows 驗證」和 UNC 共用,而不要使用 FTP 共用,因為系統必須儲存 FTP 密碼,而該密碼會以純文字格式從「訂閱者」或執行 IIS 的電腦 (使用 Web 同步處理時) 傳送到 FTP 伺服器。 此外,因為對快照集共用的存取由單一帳戶控制,所以無法確定已篩選的合併式發行集之「訂閱者」僅從其資料分割存取快照集檔案。

限制事項

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

必要條件

  • 若要使用「檔案傳輸通訊協定」(FTP) 傳送快照集檔案,您必須先設定 FTP 伺服器。 如需詳細資訊,請參閱 Microsoft Internet Information Services (IIS) 文件。

安全性

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

最佳安全性作法是不允許匿名登入 FTP 伺服器。 快照集代理程式必須有您指定之目錄的寫入權限,而散發代理程式或合併代理程式則必須有讀取權限。 如果使用提取訂閱,則您必須指定共用目錄為通用命名慣例 (UNC) 路徑,例如 \\ftpserver\home\snapshots。 如需詳細資訊,請參閱保護快照集資料夾

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

使用 SQL Server Management Studio

設定 FTP 伺服器之後,請在 [發行集屬性 <發行集>] 對話方塊中,指定此伺服器的目錄和安全性資訊。 如需有關存取這個對話方塊的詳細資訊,請參閱< View and Modify Publication Properties>。

若要指定 FTP 資訊

  1. 在 [發行集屬性 - <發行集>] 對話方塊中,從下列任一頁面選取 [允許訂閱者使用 FTP 下載快照集檔案]:

    • [FTP 快照集] 頁面,適用於快照式和交易式發行集,以及執行 Microsoft SQL Server 2005 (9.x) 之前版本的發行者所用合併式發行集。

    • [FTP 快照集和網際網路] 頁面,適用於執行 SQL Server 2005 (9.x) 或更新版本發行者的合併式發行集。

  2. 指定 [FTP 伺服器名稱][通訊埠編號][FTP 根資料夾的路徑][登入] 以及 [密碼] 的值。

    舉例來說,如果 FTP 伺服器根目錄為 \\ftpserver\home,而您想將快照集儲存在 \\ftpserver\home\snapshots 中,請將 [FTP 根資料夾的路徑] 屬性指定為 \snapshots\ftp (複寫會在建立快照集檔案時,在快照集資料夾路徑後附加 'ftp')。

  3. 指定快照集代理程式應該將快照集檔案複製到步驟 2 中指定的目錄。 例如,若要讓快照集代理程式將快照集檔案寫入到 \\ftpserver\home\snapshots\ftp 中,您必須在以下兩處位置的其中一指定路徑 \\ftpserver\home\snapshots:

    • 與此發行集相關的「散發者」的預設快照集位置。
    • 此發行集的替代快照集資料夾位置。 如果壓縮快照集,則需要替代位置。

如需有關修改快照集資料夾位置屬性的詳細資訊,請參閱快照集選項

  1. 選取 [確定]。

使用 TRANSACT-SQL

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

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

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

    • @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 的發行集。 如需詳細資訊,請參閱建立發行集

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

  1. 在發行集資料庫的發行者上,執行 sp_addmergepublication。 指定 @publication、針對 @enabled_for_internettrue 值,以及下列參數的適當值:

    • @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 的發行集。 如需詳細資訊,請參閱建立發行集

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

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

    • 在訂閱資料庫的訂閱者上,執行 sp_addpullsubscription_agent。 指定 @publisher@publisher_db@publication、針對 @job_login@job_password 的 Microsoft Windows 認證 (散發代理程式在訂閱者端執行時會使用此認證),以及 @use_ftptrue 值。
  2. 在發行集資料庫的發行者上,執行 sp_addsubscription ,以註冊提取訂閱。 如需詳細資訊,請參閱 建立提取訂閱

針對使用 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 ,以註冊提取訂閱。 如需詳細資訊,請參閱 建立提取訂閱

針對快照式或交易式發行集變更一個或多個 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 的值。

範例 (Transact-SQL)

下列範例會建立允許訂閱者的合併式發行集,以使用 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'AdventureWorks'; 
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 [AdventureWorks]
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'AdventureWorksReplica';

-- 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'AdventureWorks'; 
SET @publication = N'AdvWorksSalesOfferMergeFtp'; 
SET @publisher = $(PubServer);
SET @login = $(Login);
SET @password = $(Password);
SET @subscriber = $(SubServer);
SET @subscriptionDB = N'AdventureWorksReplica';

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

另請參閱

Replication System Stored Procedures Concepts
變更發行集與發行項屬性
使用快照集初始化訂閱