如何通过 FTP 传递快照(复制 Transact-SQL 编程)

通过使用复制存储过程,能以编程方式设置用于在 FTP 服务器提供快照文件的选项并更改这些 FTP 设置。所用的过程由发布的类型决定。FTP 快照传递仅可同请求订阅一起使用。

安全说明安全说明

为了帮助改进安全性,建议您在通过 Internet 使用 FTP 快照传递时实现虚拟专用网络 (VPN)。有关详细信息,请参阅使用 VPN 通过 Internet 发布数据

为快照发布或事务发布启用 FTP 快照传递

  • 在发布服务器上,对发布数据库执行 sp_addpublication。指定 @publication,将值 true 指定给 @enabled_for_internet,并将相应值指定给以下参数:

    • @ftp_address - 用于传递快照的 FTP 服务器的地址。

    • (可选)@ftp_port - FTP 服务器所使用的端口。

    • (可选)@ftp_subdirectory - 分配给 FTP 登录名的默认 FTP 目录的子目录。例如,如果 FTP 服务器根目录是 \\ftpserver\home 并要将快照存储在 \\ftpserver\home\snapshots 下,则将 \snapshots\ftp 指定给 @ftp_subdirectory(复制在创建快照文件时将“ftp”追加到快照文件夹路径中)。

    • (可选)@ftp_login - 连接到 FTP 服务器时使用的登录帐户。

    • (可选)@ftp_password - FTP 登录名的密码。

      安全说明安全说明

      出于安全考虑,最好不允许匿名登录 FTP 服务器。

    注意注意

    快照代理必须对指定的目录具有写权限,而分发代理或合并代理必须具有读权限。如果使用请求订阅,就必须指定一个共享目录作为通用命名约定 (UNC) 路径,例如 \\ftpserver\home\snapshots。有关详细信息,请参阅保护快照文件夹的安全

    此操作将创建一个使用 FTP 的发布。有关详细信息,请参阅如何创建发布(复制 Transact-SQL 编程)

为合并发布启用 FTP 快照传递

  • 在发布服务器上,对发布数据库执行 sp_addmergepublication。指定 @publication,将值 true 指定给 @enabled_for_internet,并将相应的值指定给以下参数:

    • @ftp_address - 用于传递快照的 FTP 服务器的地址。

    • (可选)@ftp_port - FTP 服务器所使用的端口。

    • (可选)@ftp_subdirectory - 分配给 FTP 登录名的默认 FTP 目录的子目录。例如,如果 FTP 服务器根目录是 \\ftpserver\home 并要将快照存储在 \\ftpserver\home\snapshots 下,则将 \snapshots\ftp 指定给 @ftp_subdirectory(复制在创建快照文件时将“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,将用于运行订阅服务器上的分发代理的 Microsoft Windows 凭据指定给 @job_login@job_password,并将值 true 指定给 @use_ftp
  2. 在发布服务器上,对发布数据库执行 sp_addsubscription 以注册请求订阅。有关详细信息,请参阅如何创建请求订阅(复制 Transact-SQL 编程)

创建对使用 FTP 快照传递的合并发布的请求订阅

  1. 在订阅服务器上,对订阅数据库执行 sp_addmergepullsubscription。指定 @publisher@publication

  2. 在订阅服务器上,对订阅数据库执行 sp_addmergepullsubscription_agent。指定 @publisher@publisher_db@publication,将用于运行订阅服务器上的分发代理的 Windows 凭据指定给 @job_login@job_password,并将值 true 指定给 @use_ftp

  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。将值 enabled_for_internet 指定给 @property,将值 false 指定给 @value

为合并发布更改 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。将值 enabled_for_internet 指定给 @property,将值 false 指定给 @value

示例

下面的示例创建一个合并发布,该发布允许订阅服务器通过使用 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