<

Windows Server Backup - Notifier Script

Published on
4,110 Points
110 Views
Last Modified:
One of the issues with Windows Server Backup is that, unless a user logs onto the server and checks physically, there is no way for them to know if the native backup utility is working or not.  This script takes care of that.

What does it do?

The batch file will notify by email, people you specify of certain events associated with Windows Server Backup.  Additionally Microsoft deprecated the send email function in Task Scheduler, so this provides a simple workaround.


How does it work?

The batch file relies on you to set tasks in Event Viewer when certain events happen.  It passes these Event IDs to the batch file which then uses them in the following way:

  • Writes a very basic ongoing log.
  • Uses Swithmail as the SMTP engine (free download) for sites where Exchange isn't installed on prem.
  • sends mail to people you nominate to inform them about the progress.


Events Reported

  • Backup start
  • Backup Completed OK
  • No Disk connected
  • Destination Disk failure
  • Unspecified error


Setup Overview

  1. Create a Scripts Folder somewhere on the server drives.
  2. Download SWITHMAIL from Sourceforge and save it to the same location.
  3. Copy the CODE below to Notepad.
  4. Edit the Bach file to include paths you used to your Scripts Folder.
  5. Test email via Blat.
  6. Create some events in Event Viewer to use as the event trigger.
  7. Create Tasks for these events.
  8. Configure the recipients you want to receive notifications.


How do I set it up?

  1. Create a folder on your C Drive (any partition will do) called Scripts.  Open the Scripts folder and create another called Backup_Related.
  2. Save your batch file to this folder and call it Go.BAT.
  3. Download Swithmail from https://sourceforge.net/projects/swithmail/ Save and extract SWITHMAIL.EXE to this same folder.
  4. Copy the code below into Notepad (any text editor will do) and save as Go.Bat into \Scripts\Backup_Related
  5. REM +--------------------------------------------------------------+
    REM | Ask Itee Backup Notifier                                     |
    REM +------------+---------+---------------------------------------+
    REM | Purpose : Allows notifications to be attached to events      |
    REM |           reported to the Windows Event Log by Windows Server|
    REM |           Backup and send these via email to people you      |
    REM |           nominate in the batch file.                        |
    REM | Credits : 'tbare' for his util Swithmail.exe                 |
    REM +------------+---------+---------------------------------------+
    REM |    Date    | Version |              Notes                    |
    REM +--------------------------------------------------------------+
    REM | 08/06/2019 | 1.0     | Send users an alert on backup activity|
    REM +--------------------------------------------------------------+
    
    Rem set below to 'on' to pause sctipt andf modify behaviour during testing
    SET Testing=off
    
    
    REM Set Log Location as variable
    SET AI_BackupNotifier_LogLocation=C:\Scripts\Backup_related\%COMPUTERNAME%01.txt
    SET MsgBody=C:\Scripts\Backup_related\MsgBody.txt
    
    :START
    
    IF %Testing%==on PAUSE
    
    IF %1%==1 GOTO 1
    IF %1%==4 GOTO 4
    IF %1%==5 GOTO 5
    IF %1%==19 GOTO 19
    IF %1%==49 GOTO 49
    
    :Unknown_Error
    REM Backup Failed Don't know why
    SET subj="Backup Failed - an unknown error has ocurred"
    SET body=Backup of Server failed for an unknown reason, contact Shane (%1%)
    ECHO Backup Return Code = %1%  >>%AI_BackupNotifier_LogLocation%
    ECHO Subject            = %subj%  >>%AI_BackupNotifier_LogLocation%
    ECHO Body               = %body%  >>%AI_BackupNotifier_LogLocation%
    ECHO %body% - %1%  >%MsgBody% 
    GOTO Tail
    
    :1
    REM Backup ok
    SET subj="Backup STARTED - of UHYSServer"
    SET body=Backup of Server Started. (%1%)
    ECHO Backup Return Code = %1%  >>%AI_BackupNotifier_LogLocation%
    ECHO Subject            = %subj%  >>%AI_BackupNotifier_LogLocation%
    ECHO Body               = %body%  >>%AI_BackupNotifier_LogLocation%
    ECHO %body% >%MsgBody% 
    GOTO Tail
    
    
    :4
    REM Backup ok
    SET subj="Backup OK - of UHYSServer"
    SET body=Backup of Server Suceeded without error. (%1%)
    ECHO Backup Return Code = %1%  >>%AI_BackupNotifier_LogLocation%
    ECHO Subject            = %subj%  >>%AI_BackupNotifier_LogLocation%
    ECHO Body               = %body%  >>%AI_BackupNotifier_LogLocation%
    ECHO %body%  >%MsgBody% 
    GOTO Tail
    
    :5
    REM Backup Failed
    SET subj="Backup Failed - of UHYSServer"
    SET body=Backup of server failed please investigate (%1%)
    ECHO Backup Return Code = %1%  >>%AI_BackupNotifier_LogLocation%
    ECHO Subject            = %subj%  >>%AI_BackupNotifier_LogLocation%
    ECHO Body               = %body%  >>%AI_BackupNotifier_LogLocation%
    ECHO %body%  >%MsgBody% 
    GOTO Tail
    
    :19
    REM Backup Failed Can't write to drive connected
    SET subj="Backup Failed - I can't write to the backup drive"
    SET body=I can't save the backup to the connected drive, it may have failed (%1%)
    ECHO Backup Return Code = %1%  >>%AI_BackupNotifier_LogLocation%
    ECHO Subject            = %subj%  >>%AI_BackupNotifier_LogLocation%
    ECHO Body               = %body%  >>%AI_BackupNotifier_LogLocation%
    ECHO %body%  >%MsgBody%
    ECHO . >>%MsgBody%
    WBADMIN GET DISKS >>%MsgBody%
    GOTO Tail
    
    :49
    REM Backup Failed No drive connected
    SET subj="Backup Failed - NO Drive can be found to run backup"
    SET body=The backup failed when I could not see a drive connected to receive the backup  (%1%)
    ECHO Backup Return Code = %1%  >>%AI_BackupNotifier_LogLocation%
    ECHO Subject            = %subj%  >>%AI_BackupNotifier_LogLocation%
    ECHO Body               = %body%  >>%AI_BackupNotifier_LogLocation%
    ECHO %body%  >%MsgBody%
    ECHO . >>%MsgBody%
    WBADMIN GET DISKS >>%MsgBody%
    GOTO Tail
    
    
     
    :Tail
    
    ECHO ___________________________________________________ >>%AI_BackupNotifier_LogLocation%
    ECHO ...          F I N I S H E D ! ! ! ! !          ... >>%AI_BackupNotifier_LogLocation%
    ECHO ___________________________________________________ >>%AI_BackupNotifier_LogLocation%
    ECHO ___________________________________________________ >>%AI_BackupNotifier_LogLocation%
    :SENDLOG
    REM    :::::::::::::: Lets set some variables ::::::::::::::
        set eMailto=recipient.1@yourcompany.com,recipient.2@yourcompany.com
    Rem Set reciptient to just in line below you while testing.
    IF %Testing%==on set eMailto=justme@yourcompany.com
        set eMailfr=ServerName.backup@yourcompanyname.com
        set server=yourmailserver.com
    REM Echo to log 
    REM Swithmail values
        ECHO eMailto = %eMailto%  >>%AI_BackupNotifier_LogLocation%
        ECHO eMailfr = %eMailfr%  >>%AI_BackupNotifier_LogLocation%
        ECHO subj    = %subj%  >>%AI_BackupNotifier_LogLocation%
        ECHO server  = %server%  >>%AI_BackupNotifier_LogLocation%
    
    IF %Testing%==on PAUSE
    
    REM    ::::::::::::::::: Now we run Swithmail ::::::::::::::
    Rem set the subject in line below to testing while testing
    IF %Testing%==on SET Subj="Testing by Your IT Support - please ignore"
    
    %~dp0swithmail.exe /s /to %eMailto% /from %eMailfr% /server %server% /subject %subj% /bodytxt %msgbody% /SSL
    
    
    Rem Reset Variables
        SET AI_BackupNotifier_LogLocation=""
        SET eMailto=""
        SET eMailfr=""
        SET subj=""
        SET server=""
        SET body=""
    
    IF %Testing%==on PAUSE
    
    :END
    • SET AI_BackupNotifier_LogLocation - make sure the path in this matches yours.
    • SET MsgBody make sure this too has the correct path.
    • SET eMailto - configure the email addresses of the recipients.  Just put yourself for now.
    • SET eMailfr - this needs to be a valid email address.
    • SET server - set this to the outgoing server.  All my clients run off-prem spam filtering, so I use the server names used in the clients MX record.
  6. Do a test of email flow.
    • I cheat this way, hit Windows Key + R for the run prompt
    • Browse to the batch file so that the command line is on the run prompt window.


      Append one of the event ID numbers to the end of the command line as shown above (testing Event ID 49)
    • If all is correct, the batch file will execute and send you a dummy report with the event ID shown.
    • If you want to see the batch file pause at sections to see where any errors are happening, change SET Testing=off to SET Testing=on, the script will pause at points in the script.  
    • If it still doesn't work see the troubleshooting section further down.
  7. We need to "create some events" in event viewer if you are doing this from scratch  on a new server.  To do this create a backup job, it only needs to be one file, so that the Stop and Start events are logged to the Event Log
    • Open Event Viewer on the server and using Event ID 1 as an example:
    • Drill down to Applications & Services Logs > Microsoft > Windows > Backup < Operational
    • Locate Event ID 1 with a Source of Backup


    • In the right hand pane of the MMC there should be an option to Attach Task to This Event


    • A Create A Basic Task dialogue will appear.  Set a meaningful name for the item.  Add a description that makes sense so make it verbose, include plenty of detail.  Click next.
    • Leave the section When an Event is Logged with its default values, so click Next.
    • In the Action Section, choose Start A Program and click Next.
    • In the Start A Program section use Browse to browse to the Go.Bat batch file saved into your Scripts Folder.
    • In the Add Arguments field enter the number 1 - this is event ID number we want to pass to the Batch file.
        
      It should look like this but with your path on your server
    • Repeat for the following events:
      • Event ID 4
      • Event ID 5
      • Event ID 19
      • Event ID 49
    • If you are going LIVE with this, don't forget to reset SET Testing=on to SET Testing=off.

Creating Dummy Events - CAUTION

EE have asked I include the ability to create dummy events for you to link to Task Scheduler.

I suggest caution on this if your Event Logs :

  • are audited by 3rd parties,
  • sent to a central log repository for review or
  • customers get stats on systems reliability that are linked to contractual penalties/service delivery.

Doing this will skew those statistics negatively.

Now that’s out of the way you can create dummy events using PoweShell (run as administrator) to create events you want the batch file to monitor and then link to a Task Schedule.  Use the command below with the event ID you need to create so that you can link to it from Event Viewer.  The example below creates a dummy event ID 49 in the backup Event Log.

New-WinEvent -ProviderName Microsoft-Windows-backup -Id 49


Files Created

  1. MsgBody.txt - is written each time by the batch file to compose the body of the email.  You can expand to include things advice to the client on what to do e.g. Disconnect the backup drive and reconnect and contact your IT provider...
  2. %COMPUTERNAME%01.txt - is a very basic log of progress.  It can be handy to review to see how long an event has been occurring e.g. Customer calls and says backup didn't work last night - you log into server, open this log and see it hasn't worked for 18 months :(


Troubleshooting

  1. Set the variable TESTING to on.  Doing this will cause the script to pause at certain times tyo allow you to monitor its progress and see any error messages.  Dont forget to re-set it to off afterwards.
  2. Emails not being sent by Swithmail.  Use the troubleshooting tips on the web, by doing a google search.  I have found things like:
    • Firewall blocking Swithmail - check its logs.
    • Antivirus not liking Swithmail - check its logs and set exceptions.
    • Spam filtering needing a valid eMailFr address - give it a real address.
    • SPF record - needs to have the sites external IP address added to the permitted senders.
    • DITTO for DMARC.
    • SMTP server used to process the email won't accept email from Swithmail for reasons known to the vendor - choose another server.
  3. Emails work, but don't get notifications - check things like:
    • Have you added the Event ID to the Argument section of the task?
    • Have you added all of the event ID's specified, to a task, some you may have to wait till they actually happen before they are generated?  (they can be created but is beyond where I want to go with this)
    • Delete the Event ID tasks that don't work and recreate from scratch.
    • Did I (meaning you) add a typo somewhere when editing the batch file?
  4. Nothing happens
    • I have to ask but you did create a backup job and schedule it, didn't you???
    • Did you leave Testing set to on causing the batch file to pause indefinitely?
    • Did you select the correct Event ID's? - Event ID 1 from other areas such as Disk Admin won't work....
    • Could it be that for ssl usage, your mail server needs to have a port specified? Add the parameter "/port xxx" to your swithmail command (xxx is the port number)
    • If your mail server needs authentication, add a parameter "/pass youroutgoingmailserverpassword" to your swithmail command


Modifications

I have added the following to the original on some sites depending on the issues and setup:

  1. Use WBADMIN GET DISKS to write what disk is connected to the ongoing log.  This is handy if you backup to external drives and suspect someone is not swapping the backup drive each day, or none are connected.
  2. Exchange backup events - for sites that backup only their Exchange Environment.
  3. VSS errors.
  4. Report on Destination disk space (USB drive, NAS etc.) when it gets low


Tested On

  • Server 2008R2
  • Server 2012/R2
  • Server 2016
  • Server 2019 (tested by the article editor)
0
Author:mbkitmgr
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free