Send Backup email

From MikroTik Wiki
Revision as of 10:50, 29 October 2007 by Normis (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

To send an email with a regular backup of all system settings, three steps are needed. Firstly you must ensure you have set the outgoing email SMTP server settings in "tool email", then create the script that generates the backup file itself, lastly is to create a schedule to run that backup script on a regular basis.

This first example definitely work on ROS v2.9.44 as this has been lifted straight from a live running system.

Firstly, input the details of your SMTP Server so that the system knows where to send any emails, The IP address 1.2.3.4 should be replaced by the IP of your upstream SMTP server. If you are running your own Email server, then use that, otherwise enter the IP address of your ISP's Email server. Set your "from" email address to something sensible, but it does not need to be a real email address that exists, as we will never be sending emails TO the Miktrotik, this is just an email address so that you can recognise where it has come from.

/ tool e-mail 
set server=1.2.3.4 from="SomeSystemName@somewhere.tld" 

Example of a "Generate a Backup" Script

/ system script
add name="backup_mail" source="/system backup save name=email_backup \n/tool \
   e-mail send file=email_backup.backup to=\"someone@somewhere.tld\" body=\"See \
   attached file for System Backup\" subject=\(\[/system identity get name\] \
   . \" \" .  \[/system clock get time\] . \" \" . \[/system clock get date\] \
   . \"  Backup\"\)\n"

It is suggested that you now test run this script and see if creates a backup file as an attachment and sends it to your email address you've setup in the script.

run backup_mail

If you don't receive the email, check the run count for that script by issuing the "print" command. This will show how many times the script has run and when it was last started. If the count is incrementing each time the script is run manually and the date time stamp is recent, then the problem is not likely to be with any syntax errors within the script itself, but with your email settings.

Now we have to add a schedule so that the script is run with the frequency you require, e.g. daily, weekly, monthly. Change the start-date to a genuine date in the future for whichever day of the week you wish to receive the first backup email. Change the start-time to the time of the day you want the email to be sent, finally change the interval period to however many days later you wish it to repeat this script. In this case I have set it to send me an email on a weekly basis.

Example of a "Weekly Scheduler" Script

/ system scheduler
add name="sched_backup_mail" on-event="backup_mail" start-date=jan/01/1970 start-time=07:30:00 interval=7d \
comment="" disabled=no

Example 2

/system script add name=ebackup source={/system backup save name=([/system identity get name] . "-" . \
[:pick [/system clock get date] 7 11] . [:pick [/system clock get date] 0 3] . [:pick [/system clock get date] 4 6]); \
/tool e-mail send to="youremail@yourdomain.com" subject=([/system identity get name] . " Backup " . \
[/system clock get date]) file=([/system identity get name] . "-" . [:pick [/system clock get date] 7 11] . \
[:pick [/system clock get date] 0 3] . [:pick [/system clock get date] 4 6] . ".backup"); :delay 10; \
/file rem [/file find name=([/system identity get name] . "-" . [:pick [/system clock get date] 7 11] . \
[:pick [/system clock get date] 0 3] . [:pick [/system clock get date] 4 6] . ".backup")]; \
:log info ("System Backup emailed at " . [/sys cl get time] . " " . [/sys cl get date])}

The above example shows a series of commands executed via a scheduler entry, which can help you manage multiple backups from multiple routers more easily. Here is what it does:

1) It makes a backup named as the routers system identity plus the date and time.

2) It emails you the backup it just created (assuming you have already set your email server address under "/tool e-mail")

3) It waits 10 seconds for the router to complete the email task, and then deletes the backup you made (to conserve space on the router).

4) It inserts an entry in the system log telling you that it ran.