Cobian Backup mail notifications
From Notes_Wiki
Home > Backup server > Cobian Backup mail notifications
Cobian Backup Post-Backup PowerShell Email Notification KB
Overview
This knowledge base (KB) document explains how a PowerShell script is executed by Cobian Backup after a backup job completes in order to send automated email notifications with backup status details.
Background
Cobian Backup provides options to run scripts before and after backup operations. In this implementation, a .bat script having powershell script is configured in the Post-backup section of a Cobian Backup job. Once the backup completes, Cobian automatically triggers the script. The script parses the Cobian backup logs and sends an email notification with the backup result.
Execution Flow
- Cobian Backup job starts
- Backup process completes
- Cobian triggers the Post-backup event (.bat file)
- PowerShell script is initiated by .bat file
- Cobian backup log is parsed
- Backup status email is sent
- Script execution details are logged
PowerShell Script
param(
[string]$Job,
[string]$DateStr = "",
[string]$TimeStr = ""
)
$LogFile = "C:\Scripts\Logs\tally-mail.log"
$CobianLogs = "C:\Program Files (x86)\Cobian Backup 11\Logs"
$SmtpServer = "<ip address>"
$SmtpPort = 25
$From = "mail id"
$Recipients = @(
"reciepent mail id"
)
$User = "mail id"
$Pass = "password"
function Write-Log($msg) {
$ts = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
Add-Content -Path $LogFile -Value "[$ts] $msg"
}
$Status = "UNKNOWN"
$TotalSize = "Unknown"
$ErrorCount = "Unknown"
try {
$latestLog = Get-ChildItem -Path $CobianLogs -Filter "*.txt" |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
if ($latestLog) {
$lastLine = Get-Content $latestLog.FullName |
Select-String "\*\*\* Backup done" |
Select-Object -Last 1
if ($lastLine -and
$lastLine.ToString() -match "Errors:\s*(\d+).*Total size:\s*([\d\.,]+\s*(?:MB|GB|KB|bytes))") {
$ErrorCount = [int]$matches[1]
$TotalSize = $matches[2]
$Status = if ($ErrorCount -eq 0) { "PASS" } else { "FAIL" }
}
}
}
catch {
Write-Log "ERROR parsing Cobian log: $($_.Exception.Message)"
}
try {
if ($DateStr -and $TimeStr) {
$dt = [datetime]::Parse("$DateStr $TimeStr")
$PrettyDate = $dt.ToString('yyyy-MM-dd')
$PrettyTime = $dt.ToString('HH:mm')
} else {
$now = Get-Date
$PrettyDate = $now.ToString('yyyy-MM-dd')
$PrettyTime = $now.ToString('HH:mm')
}
} catch {
$now = Get-Date
$PrettyDate = $now.ToString('yyyy-MM-dd')
$PrettyTime = $now.ToString('HH:mm')
}
$Subject = "mail subject - $Status"
$Body = @"
Backup
Status : $Status
Errors : $ErrorCount
Date : $PrettyDate
Time : $PrettyTime
Total Size : $TotalSize
"@
try {
$msg = New-Object system.net.mail.mailmessage
$msg.From = $From
foreach ($rcpt in $Recipients) { $msg.To.Add($rcpt) }
$msg.Subject = $Subject
$msg.Body = $Body
$smtp = New-Object Net.Mail.SmtpClient($SmtpServer, $SmtpPort)
$smtp.EnableSsl = $false
$smtp.Credentials = New-Object System.Net.NetworkCredential($User, $Pass)
$smtp.Send($msg)
Write-Log "Mail sent OK: Status=$Status Errors=$ErrorCount Size=$TotalSize"
exit 0
}
catch {
Write-Log "ERROR sending mail: $($_.Exception.Message)"
exit 1
}