透過 FTP 傳遞快照集

本主題描述如何使用 SQL Server Management Studio 或 Transact-SQL,在 SQL Server 2014 中透過 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 之前版本的發行者的合併式發行集。
    • FTP快照集和網際網路頁面,適用于執行 SQL Server 2005 或更新版本之發行者的合併式發行集。
  2. 指定 [FTP 伺服器名稱][通訊埠編號][FTP 根資料夾的路徑][登入] 以及 [密碼] 的值。
    舉例來說,如果 FTP 伺服器根目錄為 \\ftpserver\home,而您想將快照集儲存在 \\ftpserver\home\snapshots 中,請將 [FTP 根資料夾的路徑] 屬性指定為 \snapshots\ftp (複寫會在建立快照集檔案時,在快照集資料夾路徑後附加 'ftp')。
  3. 指定快照集代理程式應該將快照集檔案複製到步驟 2 中指定的目錄。 例如,若要讓快照集代理程式將快照集檔案寫入到 \\ftpserver\home\snapshots\ftp 中,您必須在以下兩處位置的其中一指定路徑 \\ftpserver\home\snapshots:
    • 與此發行集相關的「散發者」的預設快照集位置。
      如需指定預設快照集位置的詳細資訊,請參閱 指定預設快照集位置
    • 此發行集的替代快照集資料夾位置。 如果壓縮快照集,則需要替代位置。
      在 [發行集屬性 - < 發行集 >] 對話方塊的 [快照集] 頁面上,于 [將檔案放在下列資料夾] 文字方塊中輸入路徑。
  4. 按一下 [確定] 。

使用 TRANSACT-SQL

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

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

  1. 在發行集資料庫的發行者上,執行 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 的發行集。 如需詳細資訊,請參閱建立發行集

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

  1. 在發行集資料庫的發行者上,執行 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 的發行集。 如需詳細資訊,請參閱建立發行集

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

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

    • 在訂閱資料庫的訂閱者上,執行 sp_addpullsubscription_agent。 指定@publisher@publisher_db@publication、訂閱者端散發代理程式執行@job_login和@job_password的 Microsoft Windows 認證,以及 true 針對@use_ftp的值。
  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
透過 FTP 傳送快照集
變更發行集與發行項屬性
使用快照集初始化訂閱