The other week I was horsing around and came up with this method of backing up some files on Windows using Robocopy, and automatically sending an email of the results based on success or failure using Robocopy’s error level output codes.
There’s basically 3 parts that make this thing work:
- The Robocopy script or scripts
- Fail or Success send email script
- Powershell scripts to send the email based on success or failure
What happens is you schedule a task to run the first Robocopy script. Based on the error level output one of two things happen: a success or failure. If there is success you run one batch file, if it fails you run another.
That second batch file runs a PowerShell script that sends an email (saying success or fail in the subject) and attaches the Robocopy log.
Let’s dive in 🙂
I’ll start with an example of the Robycopy .bat file:
@ECHO OFF ECHO "Copying Data...Please wait. . ." ROBOCOPY X:\pathTo\yourDataDirectory y:\destinationPath /S /XO /log:"C:\Tech\robocopyLogs\theLog.log" if %ERRORLEVEL% EQU 16 echo ***FATAL ERROR*** & goto end2 if %ERRORLEVEL% EQU 15 echo OKCOPY + FAIL + MISMATCHES + XTRA & goto end2 if %ERRORLEVEL% EQU 14 echo FAIL + MISMATCHES + XTRA & goto end2 if %ERRORLEVEL% EQU 13 echo OKCOPY + FAIL + MISMATCHES & goto end2 if %ERRORLEVEL% EQU 12 echo FAIL + MISMATCHES & goto end2 if %ERRORLEVEL% EQU 11 echo OKCOPY + FAIL + XTRA & goto end2 if %ERRORLEVEL% EQU 10 echo FAIL + XTRA & goto end2 if %ERRORLEVEL% EQU 9 echo OKCOPY + FAIL & goto end2 if %ERRORLEVEL% EQU 8 echo FAIL & goto end2 if %ERRORLEVEL% EQU 7 echo OKCOPY + MISMATCHES + XTRA & goto end2 if %ERRORLEVEL% EQU 6 echo MISMATCHES + XTRA & goto end2 if %ERRORLEVEL% EQU 5 echo OKCOPY + MISMATCHES & goto end2 if %ERRORLEVEL% EQU 4 echo MISMATCHES & goto end2 if %ERRORLEVEL% EQU 3 echo OKCOPY + XTRA & goto success2 if %ERRORLEVEL% EQU 2 echo XTRA & goto success2 if %ERRORLEVEL% EQU 1 echo OKCOPY & goto success2 if %ERRORLEVEL% EQU 0 echo No Change & goto success2 :end2 REM EMAIL FAILURE FOR X TO Y C:\Tech\Scripts\dataFailSendEmail.bat exit :success2 REM EMAIL SUCCESS FOR X TO Y C:\Tech\Scripts\dataSuccessSendEmail.bat PAUSE
What the code does:
First, this code copies the data from one directory including subfolders, to another. It also saves a log of its activity.
Second, it looks at the error level of Robocopy. Based on what was acceptable for me as success and failure, I defined two possible outcomes. If the email passes, it runs another .bat file for success, which I will show next. If it fails, it opens a failure script.
The Success / Fail .bat File
Now, let’s take a look at the success script:
powershell -ExecutionPolicy ByPass -File C:\Tech\Scripts\roboSuccess.ps1
That’s it! All we do is run a powershell script to send the email, let’s get into that:
Powershell to Send the Email
$User = "yourEmail@email.com" $File = "C:\EmailPassword.txt" $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString) $EmailTo = "firstname.lastname@example.org, ???@email.com" $EmailFrom = "backupEmail@gmail.com" $Subject = "ROBO Backup Success for Data Archive" $Body = "Your files successfully backed up from X:\ to Y:\" $SMTPServer = "smtp.gmail.com" $filenameAndPath = "C:\Tech\robocopyLogs\theLog.log" $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body) $attachment = New-Object System.Net.Mail.Attachment($filenameAndPath) $SMTPMessage.Attachments.Add($attachment) $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) $SMTPClient.EnableSsl = $true $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password); $SMTPClient.Send($SMTPMessage)
One very important thing here is the C:\emailPassword.txt file. To store an encrypted version of your password and pass that to powershell, run this command in PowerShell:
"myPassword" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\EmailPassword.txt"
This is at least a bit more secure that storing it in plain text. If anyone has a way to improve on this, please chime in!
The rest is pretty straighforward. We pass google’s server info and the log file to the email, plus some text based on success or fail. Just recreate this in the opposite to handle any fails.
The log is attached so you can review that and see what happened if it fails.
One more gotcha–you may have to enable “less secure apps”, or some other settings, to get this to work with Gmail, but it shouldn’t be to hard to figure out.
Once you have all the working parts sorted out, you just use the Windows Task Scheduler to run the first Robocopy script at a time that suits you.
There are of course way better solutions for this such as Veeam, etc.–but this may work for some people in certain situations, or for less critical data.
Please feel free to write in and share on how this could be improved. Cheers!