Syed Jahanzaib Personal Blog to Share Knowledge !

November 19, 2012

Mikrotik Daily Backup via GMAIL


 

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

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
  2. Export Complete Mikrotik Configuration in a file
  3. UPload to FTP
  4. Send files via Email (using Gmail SMTP)

 


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 6.x Series, well tested with 6.4x
# with Email & FTP Feature (FTP feature added at 20-JUL-2019]
# Syed Jahanzaib / aacable AT hotmail DOT com / https://aacable.wordpress.com
# Configure following options
:local SLEEP "30s";
:log warning "Mikrotik Router Backup JOB Script Started . . . Powered by Syed Jahanzaib"
# Set FTPUPLOAD to FALSE if you dont want the FTP feature
:local FTPUPLOAD true;
# Set EMAILUPLOAD to FALSE if you dont want the EMAIL feature
:local EMAILUPLOAD true;
:local company "ZAIB Ltd"
# Set Adminmails to email addressess on whcih you want to receive Backup emails, I am using 2 email accounts for primary/secondary backup purposes
:local adminmail1 zaib1@gmail.com
:local adminmail2 zaib2@hotmail.com
# GMAIL SMTP DYNAMIC Config Section, Make sure to change these values to match your's / Jz
# GMAIL SMTP will be set dynamically later, each time the script runs
:local gmailid zaib1@gmail.com
:local gmailuser zaib1
:local gmailpwd zaib1Password
:local gmailport 587
# Set FTP Server, or set FTPUPLOAD to flase in top config section
:local FTPSRV "10.0.0.1"
:local FTPUSER "ftpid"
:local FTPPASS "ftppass"
######################################
###### Configurable options ends here
###### Donot EDIT below
######################################
:local ID ([/system identity get name])
:local DATE ([/system clock get date])
:local TIME [/system clock get time];
:local name [/system identity get name];
:local DAYIN3DIGIT ""
:local months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
:local daytbl [:toarray "sun,mon,tue,wed,thu,fri,sat"]
:local month [:pick $DATE 0 3]
:local day [:pick $DATE 4 6]
:local dayc [:pick $DATE 5 6]
:local year [:pick $DATE 7 11]
# if the first char is a 0 (zero) only read last char, else script fails
:if ([:pick $DATE 4 5] = 0) do={ :set day ($dayc)}
:local sum 0
:local aaa 0
:local yyy 0
:local mmm 0
:local nmonth 1
:for mindex from=0 to=[:len $months] do={
:if ([:pick $months $mindex] = $month) do={:set nmonth ($mindex + 1) }
}
:set aaa ((14 - $nmonth) / 12)
:set yyy ($year - $aaa)
:set mmm ($nmonth + 12 * $aaa - 2)
:set sum (7000 + $day + $yyy + ($yyy / 4) - ($yyy / 100) + ($yyy / 400) + ((31 * $mmm) / 12))
:set sum ($sum - (($sum / 7) * 7))
:set DAYIN3DIGIT [:pick $daytbl $sum]
:local hour [:pick $TIME 0 2];
:local min [:pick $TIME 3 5];
:local sec [:pick $TIME 6 8];
:local month [:pick $DATE 0 3];
:local monthname [ :pick $DATE 0 3 ]
:set month ([ :find $months $month -1 ] + 1);
:if ($month < 10) do={ :set month ("0" . $month); }
:local day [:pick $DATE 4 6];
:local year [:pick $DATE 7 11];
:local backupconfig;
:local backupexport;
#:set backupconfig ($name. "-" .$year."-".$month."-".$day."-".$hour.$min."."."backup");
#:set backupexport ($name. "-" .$year."-".$month."-".$day."-".$hour.$min."."."rsc");
:set backupconfig ($name. "-" .$DAYIN3DIGIT."-".$day."-".$monthname."-".$year."-"."hhmm".$hour.$min."."."backup");
:set backupexport ($name. "-" .$DAYIN3DIGIT."-".$day."-".$monthname."-".$year."-"."hhmm".$hour.$min."."."rsc");
:log error "$backupconfig";
:log error "$backupexport";
# Additional Check for GMAIL SMTP Resolving.
# If gmail SMTP failed to resolve, then it should set manual IP for gmail smtp
# So that email should go even if there is a dns failure.
# By ZAIB - 18-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"];
}
# 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
:if ($EMAILUPLOAD) do={
:log warning "Email Upload is Enabled.";
:put "Email Upload is Enabled.";
} else={
:log warning "Email Upload is Disabled.";
:put "Email Upload is Disabled.";
}
:if ($FTPUPLOAD) do={
:log warning "FTP Upload is Enabled.";
:put "FTP Upload is Enabled.";
} else={
:log warning "FTP Upload is Disbled.";
:put "FTP Upload is Disbled.";
}
#######################################################
# Start creating Backup files backup and export both
:log warning "$company : Creating new up to date backup files . . . "
/system backup save name=$backupconfig dont-encrypt=yes
/export file=$backupexport
#######################################################
:log warning "$company : Backup JOB process pausing for $SLEEP so that it can complete creating backup. Usually it is required for Slow systems ..."
:delay $SLEEP
:if ($EMAILUPLOAD) do={
: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="$ID - $DATE $TIME - Configuration BACKUP File" file=$backupconfig start-tls=yes
/tool e-mail send to=$adminmail1 subject="$ID - $DATE $TIME - Configuration EXPORT File" file=$backupexport start-tls=yes
# Send same backup files to second email account , just for duplication and backup
/tool e-mail send to=$adminmail2 subject="$ID - $DATE $TIME - Configuration BACKUP File" file=$backupconfig start-tls=yes
/tool e-mail send to=$adminmail2 subject="$ID - $DATE $TIME - Configuration EXPORT File" file=$backupexport start-tls=yes
}
#Upload to FTP if its set to TRUE
:if ($FTPUPLOAD) do={
:log warning "Backup JOB is now sending Backup to FTP Server $FTPSRV File . . ."
/tool fetch address=$FTPSRV src-path="$backupconfig" user=$FTPUSER mode=ftp password=$FTPPASS dst-path="/$backupconfig" upload=yes
/tool fetch address=$FTPSRV src-path="$backupexport" user=$FTPUSER mode=ftp password=$FTPPASS dst-path="/$backupexport" upload=yes
}
:log warning "$company : BACKUP JOB: Sleeping for $SLEEP so email can be delivered & FTP can be uploaded, "
:delay $SLEEP
# REMOVE Old backup files to save MIKROTIK ROS DISK space.
/file remove $backupconfig
/file remove $backupexport
# 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"
:put "$company : Backup JOB: Process Finished & Backup File Removed. All Done. You should verify your inbox for confirmation, Regard's Syed Jahanzaib"
# Script ENDs here

Click on OK to save the script.

Schedule to run it daily & you are good to go. !

 


 

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

%d bloggers like this: