Skip to content

Instantly share code, notes, and snippets.

@kurnias
Last active January 25, 2017 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurnias/5732c326396c976d192c19e0f8c4aafd to your computer and use it in GitHub Desktop.
Save kurnias/5732c326396c976d192c19e0f8c4aafd to your computer and use it in GitHub Desktop.
# Mikrotik configuration backup scripts
# Author: Sindhu Kurnia
# https://gist.github.com/kurnias/5732c326396c976d192c19e0f8c4aafd
#
# Script configuration please adjust this!
:local scriptName "cfgBackup"
:local email "email-address@here.com"
# 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])
# create filename for our backup
:local fname ("disk1/".[/system identity get name]."-".$ds."-".$ts)
:local fname1 ($fname.".backup")
:local fname2 ($fname.".rsc")
# Run configuration backup
/system backup save name=$fname1
/export compact file=$fname2
# Give some delay to make sure files are created successfully
:delay 5s;
# Send config to backup email
/tool e-mail send to="$email" subject=("[Backup]".$fname1) file=$fname1;
/tool e-mail send to="$email" subject=("[Backup]".$fname2) file=$fname2;
# IFTTT webhook notif experimental
# /tool fetch "https://maker.ifttt.com/trigger/cfgBackup/with/key/KwBFNBwS0oMJehPZiXpT5";
# delete last run generated config files
:local lastRun [/system script get [find name="$scriptName"] comment]
/file remove [find name~"$lastRun"];
# save last run filename base, to delete last created config file on next run
/system script set [find name="$scriptName"] comment=$fname;
# Write log
:log info message=("[BACKUP] file: ".$fname." created");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment