翻譯|使用教程|編輯:黃竹雯|2019-06-11 14:37:18.240|閱讀 615 次
概述:SQL數據庫中的數據通常需要實時同步 - 可以通過檢查一個數據庫的更新然后將它們應用到另一個數據庫來實現。在這種情況下,變更檢測和同步過程應按計劃自動運行,無需外部干預。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
SQL數據庫中的數據通常需要實時同步 - 可以通過檢查一個數據庫的更新然后將它們應用到另一個數據庫來實現。在這種情況下,變更檢測和同步過程應按計劃自動運行,無需外部干預。
Data Compare for SQL Server是一個外部工具,允許你比較SQL數據庫,備份和腳本文件夾中的數據。使用dbForge Data Compare for SQL Server,你可以安排幾乎實時的數據庫同步。
你可以按照以下步驟設置該過程:
在“New Data Comparison(新建數據比較)”窗口中,在相應的選項卡中選擇源數據庫和目標數據庫:
在“Mapping(映射)”選項卡中,你可以選擇要比較的對象。此外,如果有必要,你可以指定鍵列和列表以進行比較:
完成比較后,你可以詳細查看結果:
保存的項目(dcomp)文件將包含調度數據同步所需的所有對象和選項。
由于我們已經在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
在本節中,我們定義應用程序的位置以及數據戳記變量。此外,我們定義包含應用程序參數的變量,例如:
以下腳本允許我們實現此目的:
#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 = ""
該腳本的最后一部分用于為三種可能的結果創建適當的響應:
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:
如果在執行腳本時發生錯誤,將顯示錯誤消息以提供有關此錯誤的潛在原因的更多信息。此外,將創建包含錯誤詳細信息的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萬+新老用戶,感恩相送三重鉅惠好禮!限量產品優惠券等你來搶!
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn