按需 SQL 快照复制与 PowerShell 同步
On demand SQL snapshot replication synchronization with PowerShell
我正在尝试创建一个 PowerShell 脚本,以便更轻松地执行从我们的主数据服务器到我们的测试服务器的按需同步。我们目前正在使用快照复制,并且只在项目之间同步数据,因此可能需要几天或几周的时间才能更新数据。涉及许多数据库,我希望能够 运行 一个 PowerShell 脚本来更新快照、重新初始化订阅,然后启动同步过程。我一直在使用复制监视器执行此操作,但有很多步骤,我希望能够确保每次都以完全相同的方式发生,因此脚本。
关于 SQL 复制和 PowerShell 的主题,我所能找到的只是如何编写新的发布和订阅脚本或监视当前 运行 宁复制过程。似乎没有任何关于实际启动同步过程的内容。最终我想封装这个功能,只需要提供几个参数,如发布者、发布者和订阅者。
是否有人拥有用于启动 MS SQL 服务器的按需复制的工具?
谢谢
这是我为同步事务复制请求订阅而编写的旧脚本。这一切都是基于使用 RMO to Synchronize a Subscription.
RMO 涵盖所有复制类型,因此它应该适用于稍作更改的快照:
$asm = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
$asm = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Replication")
$asm = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.RMO")
function Do-Replication
{
<#
.SYNOPSIS Initiate Merge Replication Sync .DESCRIPTION This Function kicks of a Transactional Replication Synchronization
.EXAMPLE Give an example of how to use it .PARAMETER subscriber The SQL Instance Name name of the Publication, EG localhost
.PARAMETER spublisher The SQL Instance Name name of the Publisher, eg MyPublisher .PARAMETER publication The name of the publication
.PARAMETER subscriptionDatabase The name of the Subscriber Database .PARAMETER publicationDatabase The name of the publisher database
.PARAMETER forceReInit $true to force a ReInitialization of the subscription, $false otherwise .PARAMETER verboseLevel Logging verbosity level
.PARAMETER retries Number of times to retry the sync in case of a failure
#>
param(
[string][Parameter(Mandatory = $true,position = 0)] $subscriber,
[string][Parameter(Mandatory = $true,position = 1)] $publisher,
[string][Parameter(Mandatory = $true,position = 2)] $publication,
[string][Parameter(Mandatory = $true,position = 3)] $subscriptionDatabase,
[string][Parameter(Mandatory = $true,position = 4)] $publicationDatabase,
[Boolean][Parameter(Mandatory = $true,position = 5)] $forceReInit,
[int32][Parameter(Mandatory = $true,position = 6)] $verboseLevel,
[int32][Parameter(Mandatory = $true,position = 7)] $retries)
"Subscriber: $subscriber";
"Publisher: $publisher";
"Publication: $publication";
"Publication Database: $publicationDatabase";
"Subscription Database: $subscriptionDatabase";
"ForceReInit: $forceReinit";
"VerboseLevel: $verboseLevel";
"Retries: $retries";
for ($counter = 1; $counter -le $retries; $counter++)
{
"Subscriber $subscriber";
$serverConnection = New-Object Microsoft.SqlServer.Management.Common.ServerConnection $subscriber;
try
{
$serverConnection.Connect();
$transPullSubscription = New-Object Microsoft.SqlServer.Replication.TransPullSubscription;
$transPullSubscription.ConnectionContext = $serverConnection;
$transPullSubscription.DatabaseName = $subscriptionDatabase;
$transPullSubscription.PublisherName = $publisher;
$transPullSubscription.PublicationDBName = $publicationDatabase;
$transPullSubscription.PublicationName = $publication;
if ($true -ne $transPullSubscription.LoadProperties())
{
throw New-Object System.ApplicationException "A subscription to [$publication] does not exist on [$subscriber]"
}
if ($null -eq $transPullSubscription.PublisherSecurity)
{
throw New-Object System.ApplicationException "There is insufficent metadata to synchronize the subscription. Recreate the subscription with the agent job or supply the required agent properties at run time.";
}
$transPullSubscription.SynchronizationAgent.Output = "c:\temp\ReplicationLog.txt";
$transPullSubscription.SynchronizationAgent.OutputVerboseLevel = $verboseLevel;
if ($forceReInit -eq $true)
{
$transPullSubscription.Reinitialize();
}
$transPullSubscription.SynchronizationAgent.Synchronize();
"Sync Complete";
return;
}
catch [Exception]
{
if ($counter -lt $retries)
{
$_.Exception.Message + ": " + $_.Exception.InnerException "Retry $counter";
continue;
}
else
{
return $_.Exception.Message + ": " + $_.Exception.InnerException
}
}
}
}
Do-Replication -subscriber "DBROWNE0" "DBROWNE0" "inventory" "subscriberTest" "replTest" $false 1 4;
我正在尝试创建一个 PowerShell 脚本,以便更轻松地执行从我们的主数据服务器到我们的测试服务器的按需同步。我们目前正在使用快照复制,并且只在项目之间同步数据,因此可能需要几天或几周的时间才能更新数据。涉及许多数据库,我希望能够 运行 一个 PowerShell 脚本来更新快照、重新初始化订阅,然后启动同步过程。我一直在使用复制监视器执行此操作,但有很多步骤,我希望能够确保每次都以完全相同的方式发生,因此脚本。
关于 SQL 复制和 PowerShell 的主题,我所能找到的只是如何编写新的发布和订阅脚本或监视当前 运行 宁复制过程。似乎没有任何关于实际启动同步过程的内容。最终我想封装这个功能,只需要提供几个参数,如发布者、发布者和订阅者。
是否有人拥有用于启动 MS SQL 服务器的按需复制的工具?
谢谢
这是我为同步事务复制请求订阅而编写的旧脚本。这一切都是基于使用 RMO to Synchronize a Subscription.
RMO 涵盖所有复制类型,因此它应该适用于稍作更改的快照:
$asm = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
$asm = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Replication")
$asm = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.RMO")
function Do-Replication
{
<#
.SYNOPSIS Initiate Merge Replication Sync .DESCRIPTION This Function kicks of a Transactional Replication Synchronization
.EXAMPLE Give an example of how to use it .PARAMETER subscriber The SQL Instance Name name of the Publication, EG localhost
.PARAMETER spublisher The SQL Instance Name name of the Publisher, eg MyPublisher .PARAMETER publication The name of the publication
.PARAMETER subscriptionDatabase The name of the Subscriber Database .PARAMETER publicationDatabase The name of the publisher database
.PARAMETER forceReInit $true to force a ReInitialization of the subscription, $false otherwise .PARAMETER verboseLevel Logging verbosity level
.PARAMETER retries Number of times to retry the sync in case of a failure
#>
param(
[string][Parameter(Mandatory = $true,position = 0)] $subscriber,
[string][Parameter(Mandatory = $true,position = 1)] $publisher,
[string][Parameter(Mandatory = $true,position = 2)] $publication,
[string][Parameter(Mandatory = $true,position = 3)] $subscriptionDatabase,
[string][Parameter(Mandatory = $true,position = 4)] $publicationDatabase,
[Boolean][Parameter(Mandatory = $true,position = 5)] $forceReInit,
[int32][Parameter(Mandatory = $true,position = 6)] $verboseLevel,
[int32][Parameter(Mandatory = $true,position = 7)] $retries)
"Subscriber: $subscriber";
"Publisher: $publisher";
"Publication: $publication";
"Publication Database: $publicationDatabase";
"Subscription Database: $subscriptionDatabase";
"ForceReInit: $forceReinit";
"VerboseLevel: $verboseLevel";
"Retries: $retries";
for ($counter = 1; $counter -le $retries; $counter++)
{
"Subscriber $subscriber";
$serverConnection = New-Object Microsoft.SqlServer.Management.Common.ServerConnection $subscriber;
try
{
$serverConnection.Connect();
$transPullSubscription = New-Object Microsoft.SqlServer.Replication.TransPullSubscription;
$transPullSubscription.ConnectionContext = $serverConnection;
$transPullSubscription.DatabaseName = $subscriptionDatabase;
$transPullSubscription.PublisherName = $publisher;
$transPullSubscription.PublicationDBName = $publicationDatabase;
$transPullSubscription.PublicationName = $publication;
if ($true -ne $transPullSubscription.LoadProperties())
{
throw New-Object System.ApplicationException "A subscription to [$publication] does not exist on [$subscriber]"
}
if ($null -eq $transPullSubscription.PublisherSecurity)
{
throw New-Object System.ApplicationException "There is insufficent metadata to synchronize the subscription. Recreate the subscription with the agent job or supply the required agent properties at run time.";
}
$transPullSubscription.SynchronizationAgent.Output = "c:\temp\ReplicationLog.txt";
$transPullSubscription.SynchronizationAgent.OutputVerboseLevel = $verboseLevel;
if ($forceReInit -eq $true)
{
$transPullSubscription.Reinitialize();
}
$transPullSubscription.SynchronizationAgent.Synchronize();
"Sync Complete";
return;
}
catch [Exception]
{
if ($counter -lt $retries)
{
$_.Exception.Message + ": " + $_.Exception.InnerException "Retry $counter";
continue;
}
else
{
return $_.Exception.Message + ": " + $_.Exception.InnerException
}
}
}
}
Do-Replication -subscriber "DBROWNE0" "DBROWNE0" "inventory" "subscriberTest" "replTest" $false 1 4;