First, I create a batch file, called backup.bat, with the content
@echo off
robocopy H:\Home V:\Home /MIR /R:1 /W:1 /LOG:V:\Home.txt
robocopy K:\Documents V:\Documents /MIR /R:1 /W:1 /LOG:V:\Documents.txt
copy /b V:\Documents.txt +V:\Home.txt V:\Backup.txt
The last line is joining the 2 log files into a single file, backup.txt
Schedule this daily with the Windows Scheduler, having an actions:
Start a Program: cmd.exe
Add arguments: /c D:\scripts\backup.bat
That bit is done for backup job.
Now, the email bit. I create a powershell script, called email.ps1, with the content:
function sendMail{
Write-Host "Sending Email"
#SMTP server name
$smtpServer = "smtprelay.domain.local"
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
#Email structure
$msg.From = "backup@mydomain.id.au"
$msg.ReplyTo = "backup@mydomain.id.au"
$msg.To.Add("me@mydomain.id.au")
$msg.subject = "Backup Email - Daily"
$msg.body = "Backup Email - Daily"
$attachment = New-Object System.Net.Mail.Attachment("V:\Backup.txt", 'text/plain')
$msg.Attachments.Add($attachment)
#Sending email
$smtp.Send($msg)
}
#Calling function
sendMail
The email powershell script attach the backup.txt file and send it away
On the same schedule job created earlier, add a second action:
Start a Program: powershell
Add arguments: D:\scripts\email.ps1
Done. Second action will be executed after the 1st action is running and it will grab the log and attach it to the email.
No comments:
Post a Comment