Automatically restore 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 configuration is rolled after a reboot. The technology is ruthless in something (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 omen has long been known: "remote configuration of the router is a long road." And even a router at your side is very easy to turn into a "brick to a rezet".



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 configuration backup script. In the future, we will “save” the state with this particular script.



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



system backup save dont-encrypt=yes name=Backup_full







We will not use the password, because otherwise it will have to be specified explicitly in the neighboring script, I see no reason for such "protection".



Create a second script that will restore the configuration every time it starts. Let's call it full_restore.



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



The control mechanism turned out to be slightly "oak", but reliable. Each time the script is run, first a check is made for the presence of the file "restore_on_reboot.txt".

If such a file exists, then recovery from backup is required. We delete the file and do a recovery followed by a reboot.



If there is no such file, just create this file and do nothing (i.e., this is the second download, which, after recovery from the 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 point before adding the task to the scheduler.



If everything is ok - proceed to the third and last action - add to the scheduler the task of running the script at each load.



Go to System -> Scheduler and add a new task.

In the Start time field, specify startup (yes, we write in letters)

In the field On Event we write

/system script run full_restore







Next, run the script saving the config! We don’t want to do all this again?



We add some kind of “garbage” to the settings for verification, or delete something important and finally, try to reboot 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 enable this mode.



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



All Articles