本主題描述如何使用 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 資訊
- 在 [ 發行集屬性 - <發行集> ] 對話框中,選取 [允許訂閱者使用 FTP 從下列其中一頁下載快照集檔案:
- FTP 快照集 頁面,適用於快照式發行集、交易式發行集,以及適用於執行 Microsoft SQL Server 2005 之前版本的發行者之合併發行集。
- FTP 快照和網際網路 頁面,用於執行 SQL Server 2005 或更新版本之發行者的合併出版物。
- 指定 FTP 伺服器名稱、 埠號碼、 FTP 根資料夾的路徑、 登入和 密碼的值。
例如,如果 FTP 伺服器根目錄是 \\ftpserver\home,而且您希望快照集儲存在 \\ftpserver\home\snapshots,請針對 FTP 根資料夾的屬性 Path 指定 \snapshots\ftp 屬性 (複寫會在建立快照集檔案時附加 'ftp' 至快照集資料夾路徑)。 - 指定快照集代理程序應該將快照集檔案寫入步驟 2 中指定的目錄。 例如,若要讓快照集代理程式將快照集檔案寫入 \\ftpserver\home\snapshots\ftp,您必須在下列兩個位置之一中指定路徑 \\ftpserver\home\snapshots:
- 與此發佈相關聯的發布者的預設快照位置。
如需指定預設快照集位置的詳細資訊,請參閱 指定預設快照集位置。 - 此出版物的替代快照資料夾位置。 如果快照檔案已壓縮,則需要替代位置。
在 [發行集屬性 - <發行>] 對話框的 [快照] 頁面上,於 「將檔案放入下列資料夾」 文本框中輸入路徑。
- 與此發佈相關聯的發布者的預設快照位置。
- 按一下 [確定]。
使用 Transact-SQL
您可以在 FTP 伺服器上設定快照集檔案的選項,而且可以使用復寫預存程式,以程式設計方式修改這些 FTP 設定。 所使用的程序取決於出版物的類型。 FTP 快照集傳遞只會與提取訂閱搭配使用。
啟用用於快照集或交易式發行集的 FTP 快照傳遞
在出版資料庫中的出版者,執行 sp_addpublication。 指定 @publication,@enabled_for_internet 的值為
true,以及下列參數的適當值:- @ftp_address - 用來傳遞快照集之 FTP 伺服器的位址。
- (選擇性) @ftp_port - FTP 伺服器所使用的埠。
- (選擇性) @ftp_subdirectory - 指派給 FTP 登入之預設 FTP 目錄的子目錄。 例如,如果 FTP 伺服器根目錄是 \\ftpserver\home,而且您希望快照集儲存在 \\ftpserver\home\snapshots,請指定 \snapshots\ftp for @ftp_subdirectory (複寫會在建立快照集檔案時,將 'ftp' 附加至快照集文件夾路徑)。
- (選擇性) @ftp_login - 連接到 FTP 伺服器時所使用的登入帳戶。
- (選擇性) @ftp_password - FTP 登入的密碼。
這會建立使用 FTP 的出版物。 如需詳細資訊,請參閱建立出版物。
啟用 FTP 傳遞合併發行的快照
在發行資料庫的發行者上,執行 sp_addmergepublication。 指定 @publication,將 @enabled_for_internet 設定為
true,並指定下列參數的適當值:- @ftp_address - 用來傳遞快照集之 FTP 伺服器的位址。
- (選擇性) @ftp_port - FTP 伺服器所使用的埠。
- (選擇性) @ftp_subdirectory - 指派給 FTP 登入之預設 FTP 目錄的子目錄。 例如,如果 FTP 伺服器根目錄是 \\ftpserver\home,而且您希望快照集儲存在 \\ftpserver\home\snapshots,請指定 \snapshots\ftp for @ftp_subdirectory (複寫會在建立快照集檔案時,將 'ftp' 附加至快照集文件夾路徑)。
- (選擇性) @ftp_login - 連接到 FTP 伺服器時所使用的登入帳戶。
- (選擇性) @ftp_password - FTP 登入的密碼。
這會建立使用 FTP 的出版物。 如需詳細資訊,請參閱建立出版物。
若要建立使用 FTP 快照集傳遞之快照式或交易式發行集的提取訂閱
在訂閱資料庫的訂閱者端,執行 sp_addpullsubscription。 指定 @publisher 和 @publication。
- 在訂閱資料庫的訂閱者端,執行 sp_addpullsubscription_agent。 指定@publisher、@publisher_db、@publication,以及訂閱者端的散發代理程式在執行時所使用的 Microsoft Windows 認證:@job_login 和 @job_password,還有
true作為@use_ftp的值。
- 在訂閱資料庫的訂閱者端,執行 sp_addpullsubscription_agent。 指定@publisher、@publisher_db、@publication,以及訂閱者端的散發代理程式在執行時所使用的 Microsoft Windows 認證:@job_login 和 @job_password,還有
在出版資料庫的發行者上,執行 sp_addsubscription 註冊拉取型訂閱。 如需詳細資訊,請參閱 建立提取訂閱。
若要建立使用 FTP 快照集傳遞之合併式發行集的提取訂閱
- 在訂閱資料庫的訂閱者端,執行 sp_addmergepullsubscription。 指定 @publisher 和 @publication。
- 在訂閱資料庫的訂閱者端,執行 sp_addmergepullsubscription_agent。 指定@publisher、@publisher_db、@publication,以及讓訂閱者端的散發代理程式執行的 Windows 認證下的@job_login和@job_password,並指定
true作為@use_ftp的值。 - 在發行集資料庫的發行者上,執行 sp_addmergesubscription 註冊提取訂閱。 如需詳細資訊,請參閱 建立提取訂閱。
變更一個或多個快照或交易發行集的 FTP 快照傳送設定
在發行集資料庫的發行者上,執行 sp_changepublication。 針對 @property 指定下列其中一個值,併為 @value指定此設定的新值:
-
ftp_address- 用來傳遞快照集之 FTP 伺服器的位址。 -
ftp_port- FTP 伺服器所使用的埠。 -
ftp_subdirectory- 用於 FTP 快照集之預設 FTP 目錄的子目錄。 -
ftp_login- 用來連線到 FTP 伺服器的登入。 -
ftp_password- FTP 登入的密碼。
-
(選擇性)針對要變更的每個 FTP 設定重複步驟 1。
(選擇性)若要停用 FTP 快照集傳遞,請在發行集資料庫的發行者端執行 sp_changepublication 。 針對 @property 指定
enabled_for_internet的值,並針對 @value 指定false的值。
變更合併式發行集的 FTP 快照傳遞配置設定
在發行集資料庫的發行者環境中,執行 sp_changemergepublication。 針對 @property 指定下列其中一個值,併為 @value指定此設定的新值:
-
ftp_address- 用來傳遞快照集之 FTP 伺服器的位址。 -
ftp_port- FTP 伺服器所使用的埠。 -
ftp_subdirectory- 用於 FTP 快照集之預設 FTP 目錄的子目錄。 -
ftp_login- 用來連線到 FTP 伺服器的登入。 -
ftp_password- FTP 登入的密碼。
-
(選擇性)針對要變更的每個 FTP 設定重複步驟 1。
(選擇性)若要停用 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