Subscribe For Free Updates!

We'll not spam mate! We promise.

Sunday, 24 November 2013

Mikrotik Auto Backup & Email using GMAIL SMTP


The aim of writing this short guide is howto automate mikrotik configuration backup + export file  and user manager database  backup 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
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
2) Backup User Manager Database [You can skip this section if its not required]
3) Export Complete Mikrotik Configuration
After creating 3 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-script
In the Source box, paste the below script.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
:log info "Mikrotik Backup JOB Started . . . By Syed Jahanzaib"
:global backupfile configbackup
:global mikrotikexport mtexport_backup
:global umbackup um_backup
 
:log info "Deleting old Backup File If available otherwise ignore & process further . . . "
 
/system backup save name=$backupfile
/export file=$mikrotikexport
/tool user-manager database save name=$umbackup
 
:log info "Backup process pausing for 10s so it complete creating backup file"
:delay 10s
:log info "Start Sending Backup File via Email using GMAIL SMTP  . . ."
 
/tool e-mail send to="YourGMAIL-ID@gmail.com" password=GMAILPASSWD subject=([/system clock get date] . \
" Mikrotik Config Backup by zaib") from=YourGMAIL-ID@gmail.com file=$backupfile server=173.194.69.109 tls=yes
 
/tool e-mail send to="YourGMAIL-ID@gmail.com" password=GMAILPASSWD subject=([/system clock get date] . \
" Mikrotik Export Backup by zaib") from=YourGMAIL-ID@gmail.com file=$mikrotikexport server=173.194.69.109 tls=yes
 
/tool e-mail send to="YourGMAIL-ID@gmail.com" password=GMAILPASSWD subject=([/system clock get date] . \
" Mikrotik UM Backup by zaib") from=YourGMAIL-ID@gmail.com file=$umbackup server=173.194.69.109 tls=yes
 
:delay 40s
:log info "Backup Finished"
:log info "Deleting Backup File. All Done. Allah Hafiz"
/file remove $backupfile
/file remove $mikrotikexport
/file remove $umbackup
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

Open Terminal & paste the following config
/tool e-mail set address=74.125.45.108 from=gmailid@gmail.com password=mypassword port=587 starttls=no user=gmailid
Testing the Script . .  .
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 server=173.194.69.109 port=587 tls=yes user=yourgmailid@gmail.com password=yourgmailpass t
o=aacable@hotmail.com subject=test body=test
Now execute the script.
/sys script run backup-script
You may see the following screens if every thing is setup correctly. Also open the LOG window.
(Screenshot Posting Pending)
script-start.
.

Adding Scheduler to run the script on daily basis

Open Terminal & paste the following command
1
2
3
/system scheduler
add comment="Execute Backup Script Daily at 1am" disabled=no interval=1d name=BackupnMail on-event=backup-script policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive start-date=nov/05/2012 start-time=01:00:00
.

GMAIL  Configuration  on  Mikrotik: [Short Reference]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[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
Naveed Ahmad

Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT → , ,
FOLLOW US →
SHARE IT →

1 comments:

  1. Our expert from Gmail technical support phone number can take care of each kind of trouble faced by you, whether easy or complicated. They provide extensive and step-by-step resolutions to all the problems of the customers.https://www.buzzfeed.com/jiprinojit/gmail-support-services-to-resolve-2hmtt

    ReplyDelete

 
".