Skip to content

Instantly share code, notes, and snippets.

@herbetom
Last active October 17, 2018 09:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herbetom/af0310f9e4385f1e838247541a9390a6 to your computer and use it in GitHub Desktop.
Save herbetom/af0310f9e4385f1e838247541a9390a6 to your computer and use it in GitHub Desktop.
Mikrotik Backup to E-Mail
# Mikrotik RouterOS Backup Script based on
# http://adminlog.eu/mikrotik-automated-backup/
# but changed to send E-Mails instead of an FTP upload
# E-Mail must be configured prior. Some explanation can be found
# here https://wiki.mikrotik.com/wiki/Manual:Tools/email#Properties
:local mailto "mail@example.org"
# start backup
/log info message="Backup Configuration backup started."
# months array
:local months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
# get time
:local ts [/system clock get time]
:set ts ([:pick $ts 0 2].[:pick $ts 3 5].[:pick $ts 6 8])
# get Date
:local ds [/system clock get date]
# convert name of month to number
:local month [ :pick $ds 0 3 ];
:local mm ([ :find $months $month -1 ] + 1);
:if ($mm < 10) do={ :set mm ("0" . $mm); }
# set $ds to format YYYY-MM-DD
:set ds ([:pick $ds 7 11] . $mm . [:pick $ds 4 6])
# file name for system backup - file name will be BCKP-servername-date-time.backup
:local fname1 ("BCKP-".[/system identity get name]."-".$ds."-".$ts.".backup")
# file name for config export - file name will be BCKP-servername-date-time.rsc
:local fname2 ("BCKP-".[/system identity get name]."-".$ds."-".$ts.".rsc")
# backup the configuration
/system backup save name=$fname1
/log info message="Backup System backup finished (1/2)."
/export compact file=$fname2
/log info message="Backup Config export finished (2/2)."
#delay so that all finished
:delay 10s
# send backup via mail
/log info message="Backup Backup being emailed"
/tool e-mail send to="$mailto" subject=([/system identity get name] ." Backup") file="$fname1,$fname2"
#/tool e-mail send to="$mailto" subject=([/system identity get name] ." Backup") file="$fname1"
# delay time to finish the upload - increase it if your backup file is big
:delay 60s
# find file name start with BCKP- then remove
:foreach i in=[/file find] do={ :if ([:typeof [:find [/file get $i name] "BCKP-"]]!="nil") do={/file remove $i}; }
/log info message="Backup Configuration backup finished."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment