# email config
$SmtpServer="smtp.gmail.com"
$sendFrom="123321@gmail.com"
$sendTo="123321@gmail.com"
$successContent="yohoo,well done guys"
$User = "123321@gmail.com"
$PassWord = ConvertTo-SecureString -String "16位google应用码" -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $PassWord
# filename
$yyyyMMdd=Get-Date -Format "yyyyMMdd"
$filename=$yyyyMMdd + "dump.sql"
$username=$env:UserName
$sucessLabel="Dump completed on"
$emailSubject="good or bad news"
$failContent="yohoo,bad news, check why go ahead, filename " + $filename
$exceptionContent="yohoo,bad news, throw exception,have a look, filename " + $filename
$username
$filename
Try{
echo "start backup"
#backup
#mysqldump -uroot -pXmwefun2022 -B sms_pod --tables accounts deposit_log send_message_detail send_message_task apis price --single-transaction -q > C:\Users\$username\Desktop\$filename
mysqldump -uroot -ppassword -B databaseName --tables accounts --single-transaction -q > C:\Users\$username\Desktop\$filename
# check backup status
$strName=findstr $sucessLabel C:\Users\$username\Desktop\$filename
if($strName -match $sucessLabel){
echo "success"
$emailSubject="good or bad news" + $filename
Send-MailMessage -SmtpServer $SmtpServer -Port 587 -UseSsl -From $sendFrom -To $sendTo -Subject $emailSubject -BodyAsHtml $successContent -Credential $Credential -Encoding ([System.Text.Encoding]::UTF8)
}else{
# send mail
echo "failed"
Send-MailMessage -SmtpServer $SmtpServer -Port 587 -UseSsl -From $sendFrom -To $sendTo -Subject $emailSubject -BodyAsHtml $failContent -Credential $Credential -Encoding ([System.Text.Encoding]::UTF8)
}
}
Catch{
echo "runtime exception, get out the reason"
Send-MailMessage -SmtpServer $SmtpServer -Port 587 -UseSsl -From $sendFrom -To $sendTo -Subject $emailSubject -BodyAsHtml $exceptionContent -Credential $Credential -Encoding ([System.Text.Encoding]::UTF8)
}
pause