The Perfect MikroTik Config Restore Script

The Perfect MikroTik Config Restore Script

Restoring config files on MikroTik routers have always been a pain. I set out to make the perfect config restore script for MikroTik routers.

Have your every tried to paste configuration commands into a MikroTik router? Yeah, it doesn’t work. As soon as the script adds an interface to a bridge or changes an IP you get disconnected and the rest of the script lines fail to restore.

Using the ‘run after reset‘ method is really the best way to restore a MikroTik router’s configuration but it has its own little caveats like editing the script first and adding a :delay 15s; line at the top.

After reboot, there is no clear indication if the import was successful or failed. While troubleshooting an error riddled backup I had the idea of introducing an audible beep before and after the import process. This lead me to develop this script.

This script is designed to restore plain-text, export files created using the following command.

/export file=myrtrbackup

I personally use the following export command to backup my routers. I find it easier to read and every line of the script is independent so copy and paste just works.

/export terse file=myrtrbackup

Features

  • Introduces a delay – This allows time for the router’s interfaces to initialize before running the import script.
  • Audible prompts – Before and after beep sequences to tell you if the import was successful.
  • Logging to disk – you can inspect perfectrestore.log.0.txt to see if the import was successful. This is useful if your router does not have a builtin speaker (some models don’t).

How to use this script

Time needed: 1 minute.

WinBox Method

  1. Upload perfectrestore.rsc to your router’s flash folder

  2. Rename your backup file backup.rsc and upload it to the router’s flash folder

  3. Click “System | Reset Configuration” in Winbox

  4. Check the box “No Default Configuration” and choose flash/perfectrestore.rsc from the “Run After Reset” dropdown

  5. Click the [Reset Configuration] button

The router will erase the existing configuration, reboot and execute the perfectrestore script. The script will introduce the proper delay, play the beginning beeps, then /import flash/backup.rsc. If the import completes without error you will here the success beep sequence.

CLI Method

  1. Upload perfectrestore.rsc and backup.rsc to your router’s flash folder via scp or ftp
  2. Issue the following command on the router.
    /system reset-configuration no-defaults=yes run-after-reset=flash/perfectrestore.rsc

perfectrestore.rsc script download

#
# perfectrestore.rsc
# Version: 0.1.1
# Author: Jonathan Cutrer
# License: MIT
# Project: https://github.com/joncutrer/perfectrestore
#
 
{
 
:local targetfile "flash/backup.rsc"   
 
# Wait for interfaces to initialize
:delay 15s
 
# Beep Functions
 :local doStartBeep [:parse ":beep frequency=1000 length=300ms;:delay 150ms;:beep frequency=1500 length=300ms;"];
 :local doFinishBeep [:parse ":beep frequency=1000 length=.6;:delay .5s;:beep frequency=1600 length=.6;:delay .5s;:beep frequency=2100 length=.3;:delay .3s;:beep frequency=2500 length=.3;:delay .3s;:beep frequency=2400 length=1;
"];
 
# Setup temporary logging to disk
/system logging action add disk-file-count=1 disk-file-name=flash/perfectrestore.log disk-lines-per-file=4096 name=perfectrestore target=disk
/system logging add action=perfectrestore topics=system,info
/system logging add action=perfectrestore topics=script,info
/system logging add action=perfectrestore topics=warning
/system logging add action=perfectrestore topics=error
/system logging add action=perfectrestore topics=critical
/system logging add action=perfectrestore topics=debug
 
# Play Audible Start Sequence
$doStartBeep
 
# Import the rsc file
:log info "BEGIN IMPORT file=$targetfile"
import $targetfile
:log info "END IMPORT file=$targetfile"
 
# Post import delay
:delay 10s
 
# Play Audible Finish Sequence
$doFinishBeep
 
# Teardown temporary logging to disk
/system logging remove [/system logging find where action=perfectrestore]
/system logging action remove [/system logging action find where name=perfectrestore] 
 
}

Github Project

https://github.com/joncutrer/perfectrestore

I hope this script makes MikroTik Backups and Restores easier for you.
Have questions? Leave a comment below!!!


10 Replies to “The Perfect MikroTik Config Restore Script”

  1. I did a backup with the command /export terse file=backup.rsc and after I ran your script it failed. Checking the log generated by the script I get this error:

    Jan/02/1970 00:00:54 script,error script error: expected end of command (line 195 column 28)

    The line 195 in my backup.rsc says:

    /ipv6 nd set [ find default=yes ] advertise-dns=no

    and the column 28 is the symbol “=” after the word default.

    Why is this happening when I follow the instructions?

    1. Sorry, you can’t brick a router by uploading or executing a config file.

      You CAN lock yourself out of a router by trying to apply a config file that contains errors. And that is the pain point of dealing with mikrotik routers. When a config fails to apply you get no indication of why.

      What this script does, is give you visual and audible indications upon success!

      I would suggest you reset the router to defaults and then copy and paste your config into terminal 4-5 lines at a time until you find the problematic line.

      1. Really ???
        I uploaded your script to my flash folder , I uploaded my old backup (from same router). i ran your script

        Now I only have a blue power led no startup beep no serial access no lan connectivity.
        WTF do you call that?
        BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED BRICKED

        1. Obviously you are frustrated with your situation, but like I suggested you need to perform a config reset.

          It’s slightly different for some models but for must power on the device with the reset button held down until the USR or ACT light begins to blink and then release button. That will set you back to the default config the router came with.

          1. Lets leave this as it is you must be dumb or not able to understand.

            NO BOOTLOADER …….NO BACKUP BOOTLOADER…. NO ACT LED …..NO BEEPS……NO LAN ACTIVITY OR LED……….NO NOTHING

            ITS #####!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.