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 / aacable@hotmail.com / https://aacable.wordpress.com
# SCRIPT LAST MODIFIED : 18-JAN-2016

: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 TO_MAIL_1@gmail.com
:local adminmail2 TO_MAIL_2@hotmail.com

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

:local gmailsmtp
:set gmailsmtp [:resolve "smtp.gmail.com"];

# 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 smtp.gmail.com and note down the IP address and use it in below section)

/tool e-mail
set address=64.233.167.108 from=YOURGMAILID@gmail.com password=GMAILPASSWORD port=587 start-tls=yes user=YOURGMAILID

 

ENABLE   EMAIL   LOGGING   FOR   DEBUG !!

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 to=aacable@hotmail.com subject="test from gmail" start-tls=yes from=YOURGMAILID@gmail.com 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.

log


 

 

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

 


Finally RECEIVED!

gmail_received

 


 

[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=173.194.66.108 from=mygmailid@gmail.com last-status=succeeded password=gmailpassword port=587 start-tls=yes user=mygmailid

To Send Email, use this command.


/tool e-mail send server=173.194.66.108 port=587 start-tls=yes user="mygmailid" passwo
rd="gmailpassword" from="mygmailid@gmail.com" to="aacable@hotmail.com" subject="test" body="test"

DONE 😀

GMAIL  Configuration  on  Mikrotik 5.2x [Short Reference]

[admin@MikroTik] > /tool e-mail pr
address: 74.125.45.108
port: 587
starttls: no
from: gmailid@gmail.com
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=74.125.45.108 from=gmailid@gmail.com password=mypassword port=587 starttls=no user=gmailid
[admin@MikroTik] >

/tool e-mail> send server=74.125.45.108 port=587 tls=yes user=gmailid@gmail.com password=xxxxxx to=aacable@hotmail.com subject=test body=test

 

Regard’s
Syed Jahanzaib

%d bloggers like this: