How to write simple script for automatic backup of MikroTik configuration and upload to FTP server.
Similar as I wrote about Cisco configuration backup in post CISCO automatic configuration backup here I will show you how am I doing backups of MikroTik network devices. There are 2 types of MikroTik backups: configuration backup and configuration export. Difference between those 2 is nicely explained on MikroTik wiki page[1]. To make it short I will paste description here:

The configuration backup can be used for backing up MikroTik RouterOS configuration to a binary file, which can be stored on the router or downloaded from it using FTP for future use. The configuration restore can be used for restoring the router's configuration, exactly as it was at the backup creation moment, from a backup file. The restoration procedure assumes the configuration is restored on the same router, where the backup file was originally created, so it will create partially broken configuration if the hardware has been changed.
The configuration export can be used for dumping out complete or partial MikroTik RouterOS configuration to the console screen or to a text (script) file, which can be downloaded from the router using FTP protocol. The configuration dumped is actually a batch of commands that add (without removing the existing configuration) the selected configuration to a router. The configuration import facility executes a batch of console commands from a script file.

So if you wanna backup whole system (including passwords for example) use system backup. But if you need quick look at configuration or part of configuration you need to use export because system backup gives you binary file you can't read simply with text editor.

System backup is made with next command:

/system backup save name=sysBackup

This will create file sysBackup.backup in /files on your router and it can be downloaded using FTP, winbox or routeros web interface.
Now to make configuration export:

/export file=exportBackup

This will create file exportBackup.rsc inf /files. It can be transfered in the same way as sysBackup.backup file.
Those are two files we need and will upload them after creation to FTP server using script on routeros. Script look like this:

/system script add name=backup2ftp source="\
    \n:local name value=[/system identity get name];\
    \n/system backup save name=\$name;\
    \n/export file=\$name;\
    \n/tool fetch address= user=ftpuser password=ftppassword mode=ftp dst-path=(\"/ftp/mikrotik/\".\$name.\".rsc\") src-path=(\$name.\".rsc\") upload=yes;\
    \n/tool fetch address= user=ftpuser password=ftppassword mode=ftp dst-path=(\"/ftp/mikrotik/\".\$name.\".backup\") src-path=(\$name.\".backup\") upload=yes;\

First we define script name, then variable name that will be same as router name. Then we make backup and export configuration file. Last 2 commands are putting those 2 files to FTP server. Password here is in clear text so keep in mind that. To test this script run it:

/system script run 0

If this is the only script number will be '0' if there are others adjust it accordingly.
Now to have this done automatically we will use routeros scheduler:

/system scheduler add interval=1w name=backupSchedule on-event=backup2ftp

This will run our backup script once per week. Remember to use name of script in on-event variable. To test it you can also run it:

/system scheduler run 0

That's it. Next trick I use is pushing all those configuration files to git repository so I can have some sort of change tracker :). So check it soon in another post.

  1. ↩︎