翻譯|使用教程|編輯:黃竹雯|2019-06-11 14:37:18.240|閱讀 615 次
概述:SQL數(shù)據(jù)庫中的數(shù)據(jù)通常需要實(shí)時(shí)同步 - 可以通過檢查一個(gè)數(shù)據(jù)庫的更新然后將它們應(yīng)用到另一個(gè)數(shù)據(jù)庫來實(shí)現(xiàn)。在這種情況下,變更檢測和同步過程應(yīng)按計(jì)劃自動(dòng)運(yùn)行,無需外部干預(yù)。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
SQL數(shù)據(jù)庫中的數(shù)據(jù)通常需要實(shí)時(shí)同步 - 可以通過檢查一個(gè)數(shù)據(jù)庫的更新然后將它們應(yīng)用到另一個(gè)數(shù)據(jù)庫來實(shí)現(xiàn)。在這種情況下,變更檢測和同步過程應(yīng)按計(jì)劃自動(dòng)運(yùn)行,無需外部干預(yù)。
Data Compare for SQL Server是一個(gè)外部工具,允許你比較SQL數(shù)據(jù)庫,備份和腳本文件夾中的數(shù)據(jù)。使用dbForge Data Compare for SQL Server,你可以安排幾乎實(shí)時(shí)的數(shù)據(jù)庫同步。
你可以按照以下步驟設(shè)置該過程:
在“New Data Comparison(新建數(shù)據(jù)比較)”窗口中,在相應(yīng)的選項(xiàng)卡中選擇源數(shù)據(jù)庫和目標(biāo)數(shù)據(jù)庫:
在“Mapping(映射)”選項(xiàng)卡中,你可以選擇要比較的對(duì)象。此外,如果有必要,你可以指定鍵列和列表以進(jìn)行比較:
完成比較后,你可以詳細(xì)查看結(jié)果:
保存的項(xiàng)目(dcomp)文件將包含調(diào)度數(shù)據(jù)同步所需的所有對(duì)象和選項(xiàng)。
由于我們已經(jīng)在Data Compare for SQL Server中成功測試了同步過程并保存了項(xiàng)目(dcomp)文件,因此我們可以使用PowerShell腳本自動(dòng)執(zhí)行該過程。
首先,我們需要?jiǎng)?chuàng)建一個(gè)函數(shù)來檢查Outputs文件夾是否存在 - 它將用于存儲(chǔ)帶日期戳的輸出摘要。我們希望確保保存每個(gè)同步的易于查找的應(yīng)用程序日志,以防我們將來需要執(zhí)行故障排除:
#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 }
接下來,讓我們定義的根文件夾和數(shù)據(jù)標(biāo)記的輸出摘要位置:
#set the root folder $rootFolder = "D:\DataSync\" #set the location of output files $outsLoc = CheckAndCreateFolder $rootFolder -Outputs
在本節(jié)中,我們定義應(yīng)用程序的位置以及數(shù)據(jù)戳記變量。此外,我們定義包含應(yīng)用程序參數(shù)的變量,例如:
以下腳本允許我們實(shí)現(xiàn)此目的:
#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腳本的下一部分將使用我們在上一步中說明的參數(shù)從其位置調(diào)用Data Compare。然后,定義返回代碼變量:
#initiate the comparison of data sources (Invoke-Expression ("& `"" + $toolLocation +"`" " +$Params)) $returnCode = $LASTEXITCODE $message = ""
該腳本的最后一部分用于為三種可能的結(jié)果創(chuàng)建適當(dāng)?shù)捻憫?yīng):
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
現(xiàn)在該作業(yè)已經(jīng)自動(dòng)化,可以按照你喜歡的任何方式進(jìn)行調(diào)度 - 例如,在Windows Scheduler的幫助下進(jìn)行。
一切準(zhǔn)備就緒后,可以隨時(shí)查看輸出摘要。在此示例中,輸出文件的位置由$ outsLoc變量定義,因此輸出文件將保存到$ rootFolder \ $ outsLoc - 在此特定示例中,DataSync \ Outputs:
如果在執(zhí)行腳本時(shí)發(fā)生錯(cuò)誤,將顯示錯(cuò)誤消息以提供有關(guān)此錯(cuò)誤的潛在原因的更多信息。此外,將創(chuàng)建包含錯(cuò)誤詳細(xì)信息的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
如果在設(shè)置過程中出現(xiàn)任何問題或疑問,可以留言告訴我們。
感恩相伴16載,感恩答謝20萬+新老用戶,感恩相送三重鉅惠好禮!限量產(chǎn)品優(yōu)惠券等你來搶!
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn