Syed Jahanzaib Personal Blog to Share Knowledge !

November 19, 2012

Mikrotik Auto Backup and Email using GMAIL SMTP

Filed under: Mikrotik Related — Tags: , , , — Syed Jahanzaib / Pinochio~:) @ 2:31 PM

The aim of writing this short guide is howto automate mikrotik configuration backup + export backup file  (and user manager database  = optional) and email it on daily basis. So you can have offline backup set for DR purposes in case of any disaster.
This guide is divided in three parts

1) Adding Script

2) Adding Email Server [now optional , as new script added on 18-jan-2016 also adds tools email options auto every time)

3) Adding Scheduler to run the script on daily basis

1) Adding Script

The below script does the following function.

1) Backup Complete Mikrotik Configuration
3) Export Complete Mikrotik Configuration

After creating 2 files, it will email them using GMAIL  SMTP server (You can change it in the script if you are using some other isp smtp server, some isp’s allow only there IP series to relay through there SMTP server) , and then deletes the files after sending Email to save space on RB (as we all know that RB have very limited storage, so if you dont delete the files created on daily basis, it will soon fill up the storage)

Goto System > Scripts
Click on + icon to add script.
Name it backup
In the Source box, paste the below script.

# Mikrotik Backup Script for Mikrotik 6.x Series, well tested with 6.3x.x
# Syed Jahanzaib / /

:log warning "Mikrotik Router Backup JOB Started . . . Powered by Syed Jahanzaib"
:local backupfile mt_config_backup
:local mikrotikexport mt_export_backup
:local sub1 ([/system identity get name])
:local sub2 ([/system clock get time])
:local sub3 ([/system clock get date])
:local company "ZAIB Pvt Ltd"
:local adminmail1
:local adminmail2

# GMAIL SMTP DYNAMIC Config Section, Make sure to change these values to match your's / Jz
:local gmailid
:local gmailuser YOURGMAILID
:local gmailport 587

:local gmailsmtp
:set gmailsmtp [:resolve ""];

# Setting gmail options in tool email as well, useful when u dont have configured toosl email option
/tool e-mail set address=$gmailsmtp port=$gmailport start-tls=yes from=$gmailid user=$gmailuser password=$gmailpwd

:log warning "$company : Creating new up to date backup files . . . "

# Start creating Backup files backup and export both
/system backup save name=$backupfile dont-encrypt=yes
/export file=$mikrotikexport

:log warning "$company : Backup JOB process pausing for 10s so it can complete creating backup. Usually for Slow systems ..."
:delay 10s

:log warning "Backup JOB is now sending Backup File via Email using GMAIL SMTP . . ."

# Start Sending email files, make sure you ahve configured tools email section before this. or else it will fail
/tool e-mail send to=$adminmail1 subject="$sub3 $sub2 $sub1 Configuration BACKUP File" file=$backupfile start-tls=yes
/tool e-mail send to=$adminmail1 subject="$sub3 $sub2 $sub1 Configuration EXPORT File" file=$mikrotikexport start-tls=yes

# Send same backup files to second email account , jsut for duplication and backup
/tool e-mail send to=$adminmail2 subject="$sub3 $sub2 $sub1 Configuration BACKUP File" file=$backupfile start-tls=yes
/tool e-mail send to=$adminmail2 subject="$sub3 $sub2 $sub1 Configuration EXPORT File" file=$mikrotikexport start-tls=yes

:log warning "$company : BACKUP JOB: Sleeping for 30 seconds so email can be delivered, "
:delay 30s

# REMOVE Old backup files to save space.
/file remove $backupfile
/file remove $mikrotikexport

# Print Log for done
:log warning "$company : Backup JOB: Process Finished & Backup File Removed. All Done. You should verify your inbox for confirmation, Regard's Syed Jahanzaib"

# Script END

Click on OK to save the script.

You can remove user manager backup from the list, if its not required.


Adding (GMAIL) Email Server in tools/e-mail for mikrotik 6.x (not required with above script, but still as a reference you can use it)

Open Terminal & paste the following config

(first resolve the gmail smtp by using command ‘ping and note down the IP address and use it in below section)

/tool e-mail
set address= password=GMAILPASSWORD port=587 start-tls=yes user=YOURGMAILID



First we will enable script logging, so we can see its result / errors etc.

/system logging
add action=echo disabled=no prefix="" topics=scrip

Now Test the script by using following command at terminal. (Also open the log window so you can see the script results or any error)

/tool e-mail send subject="test from gmail" start-tls=yes password=GMAILPASS
agp port=587 user=YOURGMAILID

Now execute the script.

/sys script run backup

Open the LOG window. You may see the following screens if every thing is setup correctly.




Adding Scheduler to run the script on daily basis

Open Terminal & paste the following command

/system scheduler
add comment="Scheduler for daily backup of MT" interval=1d name=daily-backup on-event=backup policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive start-date=jan/01/1970 start-time=00:00:00






[Short Reference]

GMAIL  Configuration  on  Mikrotik 6.17

[admin@AGP-Mikrotik] /tool e-mail> /tool e-mail export
# jul/24/2014 23:52:40 by RouterOS 6.17
# software id = xxxxx
/tool e-mail
set address= last-status=succeeded password=gmailpassword port=587 start-tls=yes user=mygmailid

To Send Email, use this command.

/tool e-mail send server= port=587 start-tls=yes user="mygmailid" passwo
rd="gmailpassword" from="" to="" subject="test" body="test"


GMAIL  Configuration  on  Mikrotik 5.2x [Short Reference]

[admin@MikroTik] > /tool e-mail pr
port: 587
starttls: no
user: gmailid
password: mypassword

[admin@MikroTik] > /tool e-mail ex
# dec/12/2012 10:45:57 by RouterOS 5.20
/tool e-mail
set address= password=mypassword port=587 starttls=no user=gmailid
[admin@MikroTik] >

/tool e-mail> send server= port=587 tls=yes password=xxxxxx subject=test body=test


Syed Jahanzaib

%d bloggers like this: