modified on 2 February 2010 at 07:33 ••• 70,912 views

Send Backup email

From MikroTik Wiki

Jump to: navigation, 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" 

Contents

"Generate a Backup" Script

Example 1

/ 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 2

:log info "backup beginning now"
:global backupfile ([/system identity get name] . "-" . [/system clock get time])
/system backup save name=$backupfile
:log info "backup pausing for 10s"
:delay 10s
:log info "backup being emailed"
/tool e-mail send to="USERNAME@gmail.com" subject=([/system identity get name] . \
" Backup") from=ROUTER@MAIDOMAIN.com file=$backupfile server=1.3.3.7
:log info "backup finished"

The destination for this example is confirmed to work with GMail's service as of 07:18, 27 December 2007 (EET).

The main reason this example is shown is to demonstrate the use of variables for file names in a script and to have a full backup of everything on the MikroTik Router. This includes almost every bit of data from the old router including graphs, users, passwords and such. This is not manipulatable outside of the MikroTik environment. To do so you would need to use the export command.

The backup file name will appear as routeridentityname-23:14:30.backup in this example.

"Weekly Scheduler" Script

Example 1

/ 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.

Example 3 by DesertAdmin

NOTE: Make sure the identity of your routerbaord is all one word for its name this is used for the email address and can not have any spaces in the identity name.

Cut the following and paste it in:


/system script add name=backup source={/export 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]); \
/tool e-mail send to="backup@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] . ".rsc"); :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] . ".rsc")]; \
:log info ("System Backup emailed at " . [/sys cl get time] . " " . [/sys cl get date])}
{
:global namE;
:set namE ("<".[/sys identity get name]."@YOURDOMAIN.com>");
/tool e-mail set server=IP.OF.YOUR.SMTP from=$namE;
:put $namE;
}
/system scheduler
add name="sched_backup" on-event="backup" start-date=jan/01/1970 start-time=07:30:00 interval=7d \
comment="" disabled=no
/

End of Cut and PASTE section.

This does the same as the above examples; but this is a VERY readable RSC file. This makes it more universal for other Routerboards. If the original board gets fried then you can simply cut and paste what you need back into another board and be back up and running.

TIP: Create another email account called like backup@YOURDOMAIN.com and then send all of your backups to that email address so it does not clutter up your personal email. This will also give you the ability to sort or go through all backup emails easier because you will not be sorting through tons of personal email and risk deleting an important backup.

The true beauty about this is for configuration control. If there are multiple admins working on a network and something is changed you can go back to an earlier version and recall the .rsc file to see the differences.

I hope this helps you Mikrotikers out. I know it has saved me especially during lighting season.

-Cheers DesertAdmin