Data Compare for MySQL allows you to set up automatic email delivery of the log file in both cases: when SQL data comparison goes well and when it fails. The task can be completed by means of the command-line functionality.
To set up automatic email notifications, perform the following steps:
1. Open Notepad and enter the source server connection, port, database name, username, and password in a single line separated by commas.
TestUser, source_database_name, source_server_name, Port, Password
TestUser
and Password
are the username and password to the source server you want to connect to.source_database_name
is a name of the source database located on the server you connect to.source_server_name
is a name of the source server you want to connect to.Port
is a port number of the source server.2. Save the .txt file.
3. Repeat the steps 1 and 2 for the target database.
Configure the sender and the recipient of the comparison results. For this, create a Windows PowerShell cmdlet file ( .ps1). It is a script that contains a series of lines written in the PowerShell scripting language.
1. In Notepad, type in the following code:
# Define SMTP Server and port
$SmtpServer = "smtp.office365.com"
$SmtpPort = "587"
# Define email data
$EmailFrom = "[email protected]"
$EmailTo = "[email protected]"
$EmailSubject = "Test"
$EmailBody = "This is a test email."
$attachment= "path_to_file"
# Define credentials (Make sure to replace 'myusername' and 'mypassword' with your actual Office365 username and password)
$MailUsername = "[email protected]"
$MailPassword = ConvertTo-SecureString "[password]" -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $MailUsername, $MailPassword
# Send the email
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -Body $EmailBody -SmtpServer $SmtpServer -port $SmtpPort -UseSsl -Credential $Credentials -Attachment $attachment
#end
In the syntax above:
2. Save the file as .ps1.
Create an executable .bat file. This way, the task can be performed using the command line.
1. In Notepad, type in the code:
Set Compare="path_to_app"
Set Sender= powershell.exe
FOR /F "eol=; tokens=1,2,3,4,5* delims=, " %%e in (Source_Servers_and_DBs.txt) do (
FOR /F "eol=; tokens=1,2,3,4,5* delims=, " %%j in (Target_Servers_and_DBs.txt) do (
%Compare% /datacompare /source connection:"User Id=%%e; Database=%%f; Host=%%g; Port=%%h; Password=%%i; Character Set=utf8" /target connection:"User Id=%%j; Database=%%k; Host=%%l; Port=%%m; Password=%%n; Character Set=utf8" /log:"D\data_compare_mysql_log.log"
)
IF ERRORLEVEL 0 (
powershell.exe -File D:\PowerShell\send_email_script.ps1
cd.>Compare_result.log
)
)
pause
In the syntax above:
2. Save the file with the .bat extensions.
3. Run the created file.
Note
You can also automate the execution of the created .bat file with the help of Windows Task Scheduler.
The script is executed, displaying the result of the comparison process and generation of the log file and sends the result to the specified email.