Syed Jahanzaib Personal Blog to Share Knowledge !

November 19, 2012

Mikrotik Daily Backup via GMAIL

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

Updated Note as of 18-Aug-2017 : After RouterOS firmware to v 6.40.1 , daily backup email sending via gmail had following error.

mt mail send error after 6.40.1.JPG

Therefore I have updated this post with MC (minor changes) to make it compatible with the new FW that is 6.40.1


This guide will illustrate on how you can setup Script in Mikrotik RouterOS , which can send Mikrotik Backup & Export (or user manager database  = optional) file via GMAIL on daily basis. This way you can have offline backup set for DR purposes in case of any disaster recovery


This guide is divided in three parts

  1. Adding the 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

Important Note: Please make sure to Enable the ‘access to the less secure applications’ in your gmail account settings otherwise sending will not be allowed by Gmail.


1) Adding the Script

The below script perform following functions …

1) Backup Complete Mikrotik Configuration in a file
3) Export Complete Mikrotik Configuration in a file

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) , then the script will delete 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)


Let’s Start …

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 RouterOS "6.40.1"
# This script will take mikrotik backup/export in files, and send them using
# Gmail SMTP. it will then delete those 2 files from the FILES section to preserve disk space in ROS
# It will also continue in case of DNS failure by an additional check
# Syed Jahanzaib / aacable at hotmail com / https:// aacable . wordpress . com
# Last Modified at 18-AUG-2017

:local COMPANY "ZAIB PVT LTD"
:local sub1 ([/system identity get name])
:local sub2 ([/system clock get time])
:local sub3 ([/system clock get date])
:local backupfile "$sub1 mt_config_backup.backup"
:local mikrotikexport "$sub1 mt_export.rsc"
:local adminmail1 ADMIN_MAIL_1@gmail.com
:local adminmail2 ADMIN_MAIL_2@hotmail.com

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

# GMAIL SMTP DYNAMIC Config Section, Make sure to change these values to match your's / Jz
# Additional Check for GMAIL SMTP Resolving.
# If gmail SMTP failed to resolve, then it willset manual IP for gmail smtp
# So that email should go even if there is a dns failure.
# By ZAIB - 17-Aug-2017

:local GMAILSMTPDNSNAME "smtp.gmail.com"
:global GMAILSMTP ""
:local RESOLVELIST {"$GMAILSMTPDNSNAME"}
:foreach addr in $RESOLVELIST do={
:do {:resolve server=8.8.8.8 $addr} on-error={:set GMAILSMTP "0";}}
:if ($GMAILSMTP = 0) do={
:set GMAILSMTP "64.233.167.108";
} else={
:set GMAILSMTP [:resolve "$GMAILSMTPDNSNAME"];
}

#####################################
## Donot change beyon this point ####
#####################################

# 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 - Mikrotik Router Backup JOB Started . . . Powered by Syed Jahanzaib"
:log warning "$COMPANY - Creating new up-to date backup/export files . . . "

# Start creating Backup files backup and export both
/system backup save name=$backupfile dont-encrypt=yes
/export file=$mikrotikexport
:put "$COMPANY : Backup JOB process pausing for 20s so it can complete creating backup. Usually for Slow systems ..."
:log warning "$COMPANY : Backup JOB process pausing for 20s so it can complete creating backup. Usually for Slow systems ..."
:delay 20s

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

# Start Sending email files to 1st email account, you can duplicate following two sending mail entries to add mulitple recipients
/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 2nd 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 20s seconds so email can be delivered, "
:delay 20s

# 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]

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

Advertisements

%d bloggers like this: