Email alerts for completion errors with scheduled FreeFileSync jobs
Update (2012/03/07): So I really thought I had this working, but found I’d left off something quite important! On the findstr line there was a missing switch before the search string. I’ve now added it below.
Update (2012/01/16): I’ve added a few lines so the log file is zipped before it’s sent and then deleted afterwards. I use 7zip but you could use something else. See code below.
I’m a big fan of FreeFileSync and use it at work for a few back up processes. However, it doesn’t have any error reporting/email alerting built into it. This means I have to manually check logs for errors. Adding complexity (i.e. additional required user steps) to back up processes is a terrible idea, so automating this processes would be a lot better.
One of the recent versions of the application included a new feature where it would execute a command line upon completion of a synchronization. With a bit of Googling I came up with a batch file to use with it. You’ll need to download mailsend from here:
Create a file called emailalert.bat. Edit it and add the code below. Be careful with line breaks. There should be 9 lines in total. The lines begin with (ish): @echo; timeout; for; findstr; if; 7zip; mailsend; del); respectively. You’ll need to change the email settings, log location and mailsend location in order to get it working as expected.
for /f "delims=" %%x in ('dir "C:\Log Location" /od /b') do set recent=%%x
findstr /m /C:"completed with errors!" "C:\Log Location\%recent%"
if %errorlevel%==0 (
"C:\Program Files\7-Zip\7z.exe" a "C:\Log Location\%recent%.7z" "C:\Log Location\%recent%"
"C:\Mailsend Location\mailsend.exe" +bc +cc -smtp smtpaddress -t to@address -f from@address -user username -pass password -sub "FreeFileSync Backup: %time%, %date% - Completed with errors!" -attach "C:\Log Location\%recent%"
del "C:\Log Location\%recent%.7z"
Once you’ve created this and edited it. I would change the errorlevel value to “1″ and run it (this will send the error message even if your last sync completed successfully – I’m assuming it did!) so you can test all the settings work correctly. Might take a few attempts to get mailsend up and running.
Once it’s working correctly and you’re receiving the emails with the log attachments, change the errorlevel back. Then add the full filepath to the batch file to FreeFileSync in Synchronization Settings under ‘On completion’.
The ‘timeout’ is in the script because I think there’s a bit of a delay before the log file is finished being created once the bat file is executed. I’m sure you could get away with a shorter delay, but I’d rather have a longer delay and definitely receive the error alert email. This command works under Windows 7/Server 2008 but for earlier versions of Windows you’ll need to use a different command line. Try some of the suggestions here: