Mikrotik Automated Backup

I want to share a neat way how to automate the backup of your Mikrotik configuration.The script provided here i use to do FTP backups 3 times a day.

I have a couple of these devices and i change the configuration pretty often on them, so i had to automate the process. While searching the web I have found this cool blog containing Mikrotik Automated Backup script.

Thanks for the nice script Harry B!

With some small modifications this script works perfectly doing two files export and backup of the whole configuration: Just Copy and Paste the script in System -> Scripts…

!NOTE:Make sure you don’t put sensitive user and password as FTP passes this in plain text over the network!

The Backup Script


# ftp server
 :local ftphost "FTP SERVER"
 :local ftpuser "FTP USER"
 :local ftppassword "FTP USER PASSWORD"
 :local ftppath "FTP PATH"

 # 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="System backup finished (1/2).";
 /export compact file=$fname2
 :log info message="Config export finished (2/2)."

 # upload the system backup
 :log info message="Uploading system backup (1/2)."
 /tool fetch address="$ftphost" src-path=$fname1 user="$ftpuser" mode=ftp password="$ftppassword" dst-path="$ftppath/$fname1" upload=yes

 # upload the configuration export
 :log info message="Uploading configuration export (2/2)."
 /tool fetch address="$ftphost" src-path=$fname2 user="$ftpuser" mode=ftp password="$ftppassword" dst-path="$ftppath/$fname2" upload=yes

 # delay time to finish the upload - increase it if your backup file is big
 :delay 120s;

 # 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="Configuration backup finished.";

 The Scheduler


Now, lets make some automation and do backups 3 times a day at 12pm, 4pm and 23pm:

 /system scheduler
 add interval=1d name="Backup at 4pm" on-event=\
 "/system script run AutomateBackup" policy=\
 ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
 start-date=apr/16/2014 start-time=16:00:00
 
add interval=1d name="Backup at 12pm" on-event=\
 "/system script run AutomateBackup" policy=\
 ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
 start-date=apr/16/2014 start-time=12:00:00
 
add interval=1d name="Backup at 23pm" on-event=\
 "/system script run AutomateBackup" policy=\
 ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
 start-date=apr/16/2014 start-time=23:00:00

2 thoughts on “Mikrotik Automated Backup

  1. Hi, is there any option to send file to ftp from Mikrotik and folder like disk1?
    I collect logs to pendrive and want to copy those logs to ftp but im stuck :(

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>