Automatic restoration of the last saved configuration in Mikrotik routers

Many have come across a wonderful feature, for example, on HPE switches - if the config is not saved manually for some reason, the previous saved config is rolled after a reboot. The technology is somewhat ruthless (I forgot to save it - do it in a new way), but fair and reliable.

But in Mikrotik, there is no such function in the database, although the sign has long been known: "remote configuration of the router - to a long road." And even a router at hand is very easy to turn into a "brick to reset."

Oddly enough - I did not find a single manual on this subject, I had to do it with pens.

The first thing we do is create a script for creating a backup copy of the configuration. In the future, we will “save” the state with this script.

Go to System -> Scripts and create a script, say, “fullbackup” (of course, without quotes).

system backup save dont-encrypt=yes name=Backup_full

We will not use the password, because otherwise it will have to be explicitly specified in the adjacent script, I don’t see the point of such “protection”.

We create a second script that will restore the configuration on each start. Let's call it "full_restore".

This script is somewhat more complicated. The fact is that when restoring the configuration, a reboot also occurs. Without using any control mechanism, we will get a cyclic reboot.

The control mechanism turned out to be slightly “oak”, but reliable. Each time the script is run, it first checks for the presence of the "restore_on_reboot.txt" file.
If there is such a file, it means that you need to restore from a backup. Delete the file and do a restore followed by a reboot.

If there is no such file, we simply create this file and do nothing (i.e., this means that this is the second download, which is after restoring from a backup).

:if ([/file find name=restore_on_reboot.txt] != "") do={ /file rem restore_on_reboot.txt; system backup load name=Backup_full password=""} else={ /file print file=restore_on_reboot.txt }

It is best to test the scripts at this stage before adding the task to the scheduler.

If everything is OK, let's proceed to the third and last step - add the script launch task to the scheduler at each boot.

Go to System -> Scheduler and add a new task.
In the Start time indicate startup (yes, we write like that, in letters)
In the On Event write
/system script run full_restore

Further, run the script that saves the config! We don't want to do it all over again, do we?

We add some “garbage” to the settings for verification, or delete something important, and finally, we try to restart the router.

Yes, many will probably say: “There is a safe mode!”. However, it will not work if, as a result of the work, you have to reconnect to the router (for example, if you change the address or parameters of the wifi network through which you are connected). And you should not forget about the possibility of “forgetting” to turn on this mode.

PS The main thing now is not to forget to "save".

Source: habr.com

Add a comment