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!!!

18 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?

  2. BRICKED MY RB962uigs-5hact2hnt ROUTER

    ONLY BLUE POWER LED ON AND NO BEEPS OR OTHER LED’S ON POWER UP

    STAY AWAY

    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 #####!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      2. Thank you very much for you for your advice! Importing a few lines, I found a few errors. After correcting them, the import was successful.

  3. I have used this a number of times and NEVER had anything brick.
    I would suggest trying Netinstall – but if it is completely locked chances are something else is going on.

    Great Job !

  4. Would this work okay with my RB4011? I don’t think it has a speaker as I have never heard it beep. It also does not have a flash folder and all the File space is persistent, so I have changed the path/filenname to just filename.

  5. Worked a treat, just had to use MAC Neigbor Winbox login after waiting. Since I uploaded rsc from different model and firmware level, I was dubious to would work. All difference is port designation, old RB960PGS has 1x sfp, new CRS112 has 4x SFP so just renamed.

  6. Cube 60G ac = BRICK
    This is a very nice script that has worked before. Thanks to the author for their care and it should not be blamed if something fails. It would be nice if it was developed to be better documented, such as the presence or absence of a / flash directory for the script to work. It is recommended that you use this solution only 1) if you can connect to the machine locally 2) if you have already had experience with your particular hardware and software version and know that it works. Otherwise, you will have to drive and shut down a plant that may be in the winter, far away and high.

  7. Hello.
    I cannot get this script to work. When checking the log, i see the following error:
    error while running run-after-reset script: failure: bad disk file name
    Please can you help me here?
    Thanks,
    Clive.

  8. This postings has helped me to confirm issues with the export import method to transfer a config to another system (same hw). Export does not entirely export everything needed for a successful import. This script proves it in its log files. Luckily you can connect with window to even a somewhat bricked router (hap ac2 in my case) to examine the logs. In order to export sensitive data use view-sensitive=true along with the export. But this does not export certificates if you added any and then certain import functions fail because certificates are not present. I had lengthy email exchanges with Mikrotik support without any success until I hit this contribution. My system uses os version 7.10.2 and my comments are limited to this release. I was able to demonstrate that exporting a config on one system and then reset with import of that config using a no-default on the same system does not restore it to the same functionality when following the instructions by Mikrotik and various other recommendations. Most of them were done on version 6.4x.x or so. It is clear that export import on Mikrotik has issues. I really wished they would use sort of running/startup config stored on flash like most routers and switches use. Even the so called default config that is enabled when pressing the reset button during power up is not configurable or stored on the flash. Thanks to the author of this restore script I was able to move forward. And I have let Mikrotik support know about it too.

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.