半岛权威十大直营(官方)网站

Data Compare for SQL Server:如何按计划自动同步两个SQL Server数据库中的数据?

翻译|使用教程|编辑:黄竹雯|2019-06-11 14:37:18.240|阅读 553 次

概述:SQL数据库中的数据通常需要实时同步 - 可以通过检查一个数据库的更新然后将它们应用到另一个数据库来实现。在这种情况下,变更检测和同步过程应按计划自动运行,无需外部干预。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

相关链接:

SQL数据库中的数据通常需要实时同步 - 可以通过检查一个数据库的更新然后将它们应用到另一个数据库来实现。在这种情况下,变更检测和同步过程应按计划自动运行,无需外部干预。

我们如何才能实现这一目标?

Data Compare for SQL Server是一个外部工具,允许你比较SQL数据库,备份和脚本文件夹中的数据。使用dbForge Data Compare for SQL Server,你可以安排几乎实时的数据库同步。

你可以按照以下步骤设置该过程:

  • 运行Data Compare for SQL Server【立即下载最新版dbForge Data Compare for SQL Server
  • 在“New Data Comparison(新建数据比较)”窗口中,在相应的选项卡中选择源数据库和目标数据库:

    01-New-Data-Comparison.png

  • 如果需要,你可以在“Options(选项)”选项卡中设置各种比较设置。
  • 在“Mapping(映射)”选项卡中,你可以选择要比较的对象。此外,如果有必要,你可以指定键列和列表以进行比较:

    2.png

  • 当一切准备就绪后,按下右下角的Compare(比较)按钮开始比较过程
  • 完成比较后,你可以详细查看结果:

    3.png

  • 使用相应的复选标记选择所有必需的对象,然后单击“Save(保存)”:

    4.png

    保存的项目(dcomp)文件将包含调度数据同步所需的所有对象和选项。

  • 保存项目(dcomp)文件后,按“Synchronize(同步)”按钮将打开“Synchronization wizard(同步向导)”:

    5.png

  • 选择“Execute the script directly against the target database(直接对目标数据库执行脚本)”,以便在设置所有必需选项后同步数据库:

    6.png

  • 现在,按右下角的“Synchronize(同步)”
  • 同步过程结束后,你可以在窗口的底部窗格中查看同步结果。

自动化流程

由于我们已经在Data Compare for SQL Server中成功测试了同步过程并保存了项目(dcomp)文件,因此我们可以使用PowerShell脚本自动执行该过程。

准备工作

首先,我们需要创建一个函数来检查Outputs文件夹是否存在 - 它将用于存储带日期戳的输出摘要。我们希望确保保存每个同步的易于查找的应用程序日志,以防我们将来需要执行故障排除:

#checks if the Outputs folder exists. If it doesn’t, the script creates it and returns its full path
function CheckAndCreateFolder($rootFolder, [switch]$Outputs)
{
    $location = $rootFolder

    #set the location based on the used switch
    if($Outputs -eq $true)
    {
        $location += "\Outputs"
    }
    #create the folder if it doesn't exist and return its path
    if(-not (Test-Path $location))
    { mkdir $location -Force:$true -Confirm:$false | Out-Null }

    return $location
}

接下来,让我们定义的根文件夹和数据标记的输出摘要位置:

#set the root folder
$rootFolder = "D:\DataSync\"

#set the location of output files
$outsLoc = CheckAndCreateFolder $rootFolder -Outputs

变量和开关

在本节中,我们定义应用程序的位置以及数据戳记变量。此外,我们定义包含应用程序参数的变量,例如:

  • 保存项目(dcomp)文件的路径;
  • / sync开关用于直接同步目标数据库;
  • / rece开关,当数据源相等时返回'102 – No differences detected(102 - 无差异检测)'消息
  • 带有日期戳的输出摘要。

以下脚本允许我们实现此目的:

#define the tool’s location, date stamp variable and the tool’s parameters 
$toolLocation   = "C:\Program Files\Devart\dbForge Studio for MySQL\dbforgemysql.com"
$dateStamp = (Get-Date -Format "Mmddyyyy_HHMMss")

#output log file path
$logPath = "$outsLoc\DataOutput_$dateStamp.txt"

$Params = "/datacompare /compfile:""D:\DataSync\Project\test_DB_1vstest_DB_2.dcomp"" /log:""$logPath"""
$sync = " /sync"

执行

PowerShell脚本的下一部分将使用我们在上一步中说明的参数从其位置调用Data Compare。然后,定义返回代码变量:

#initiate the comparison of data sources
(Invoke-Expression ("& `"" + $toolLocation +"`" " +$Params))
     $returnCode = $LASTEXITCODE
     
     $message = ""

该脚本的最后一部分用于为三种可能的结果创建适当的响应:

  • 发生错误,将打开输出摘要
  • 存在差异,例如返回代码0 - 成功
  • 没有差异,例如返回代码100 - 未检测到差异
if ($returnCode -notin (100, 101))
     { #an error is encountered
       $logPath = "$outsLoc\DataOutput_error.txt"

       $message >> $logPath
       clear-content $logPath
       $message = "`r`n $returnCode - An error is encountered"

       #output file is opened when an error is encountered
       Invoke-Item "$logPath"
     }
     else{
     if ($returnCode -eq 101)
    {
    clear-content $logPath
    (Invoke-Expression ("& `"" + $toolLocation +"`" " +$Params+$sync))
    $returnCode = $LASTEXITCODE

   #schema changes are detected
   }
   if($returnCode -eq 0)
   {
       $message = "`r`n $returnCode - Schema changes were successfully synchronized"
   }
   else
   {
       #there are no schema changes
       if($returnCode -eq 100)
       {
           $message = "`r`n $returnCode - There are no schema changes. Job aborted"
       }
   }
   }
   $message >> $logPath

现在该作业已经自动化,可以按照你喜欢的任何方式进行调度 - 例如,在Windows Scheduler的帮助下进行。

审查结果

一切准备就绪后,可以随时查看输出摘要。在此示例中,输出文件的位置由$ outsLoc变量定义,因此输出文件将保存到$ rootFolder \ $ outsLoc - 在此特定示例中,DataSync \ Outputs:

7.png

如果在执行脚本时发生错误,将显示错误消息以提供有关此错误的潜在原因的更多信息。此外,将创建包含错误详细信息的DataOutput_error.txt文件。

以下是完整的脚本:

#checks if the Outputs folder exists. If it doesn’t, the script creates it and returns its full path
function CheckAndCreateFolder($rootFolder, [switch]$Outputs)
{
    $location = $rootFolder

    #set the location based on the used switch
    if($Outputs -eq $true)
    {
        $location += "\Outputs"
    }
    #create the folder if it doesn't exist and return its path
    if(-not (Test-Path $location))
    { mkdir $location -Force:$true -Confirm:$false | Out-Null }

    return $location
}

#set the root folder
$rootFolder = "D:\DataSync\"

#set the location of output files
$outsLoc = CheckAndCreateFolder $rootFolder -Outputs

#define the tool’s location, date stamp variable and the tool’s parameters 
$toolLocation   = "C:\Program Files\Devart\dbForge Studio for MySQL\dbforgemysql.com"
$dateStamp = (Get-Date -Format "Mmddyyyy_HHMMss")

#output log file path
$logPath = "$outsLoc\DataOutput_$dateStamp.txt"

$Params = "/datacompare /compfile:""D:\DataSync\Project\ALLA1vsALLA2.dcomp"" /log:""$logPath"""
$sync = " /sync"

#initiate the comparison of data sources
(Invoke-Expression ("& `"" + $toolLocation +"`" " +$Params))
     $returnCode = $LASTEXITCODE
     
     $message = ""

if ($returnCode -notin (100, 101))
     { #an error is encountered
       $logPath = "$outsLoc\DataOutput_error.txt"

       $message >> $logPath
       clear-content $logPath
       $message = "`r`n $returnCode - An error is encountered"

       #output file is opened when an error is encountered
       Invoke-Item "$logPath"
     }
     else{
     if ($returnCode -eq 101)
    {
    clear-content $logPath
    (Invoke-Expression ("& `"" + $toolLocation +"`" " +$Params+$sync))
    $returnCode = $LASTEXITCODE

   #schema changes are detected
   }
   if($returnCode -eq 0)
   {
       $message = "`r`n $returnCode - Schema changes were successfully synchronized"
   }
   else
   {
       #there are no schema changes
       if($returnCode -eq 100)
       {
           $message = "`r`n $returnCode - There are no schema changes. Job aborted"
       }
   }
   }
   $message >> $logPath

如果在设置过程中出现任何问题或疑问,可以留言告诉我们。

感恩相伴16载,感恩答谢20万+新老用户,感恩相送三重钜惠好礼!限量产品优惠券等你来抢!

购买dbForge Data Compare for SQL Server正版授权的朋友可以点击""哦~~~

慧都年中活动火热开启


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@wqylolg.cn


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP