Automating MikroTik RouterOS v7 Updates and Backups
Maintaining a home network is one of those jobs that is easy to forget when everything is working as expected.
My MikroTik router can run for months without needing much attention. However, just because the network is working doesn't mean I should ignore software updates, firmware maintenance, or configuration backups.
I wanted a simple solution that would handle these routine tasks automatically, without introducing another platform or a complicated automation stack. Since RouterOS already has scripting and scheduling built in, I decided to use those features to automate the process.
The result is a small collection of RouterOS scripts that check for updates, back up the configuration before making changes, and upgrade RouterBOARD firmware when required.
The complete configuration is available in my GitLab snippet:
MikroTik RouterOS Automated Update and Backup
The idea: automate the boring stuff
The workflow is deliberately simple:
Check whether a new RouterOS version is available.
Back up the current configuration before upgrading.
Install the RouterOS update and allow the router to reboot.
Check the RouterBOARD firmware after startup.
Upgrade the firmware if it differs from the installed firmware version.
Rather than putting everything into one large script, I split the configuration into three smaller scripts and use two schedulers to run them.
This makes the setup easier to understand, troubleshoot, and maintain.
1. RouterOS package updates
The routeros-auto-update script checks for updates using the stable release channel.
After requesting an update check, the script waits ten seconds and reads the update status.
If a new version is available, it calls the backup script before initiating the installation:
The important part is the order of operations. The backup is requested before the package installation begins.
If no update is available, the script records the current status in the RouterOS log instead of attempting an installation.
I use the stable channel as a reasonable starting point for a home network, although I would still review release notes and test significant upgrades before deploying them on a router that supports critical services.
2. Backing up before making changes
The backup-before-upgrade script is shared by both the RouterOS package updater and the RouterBOARD firmware updater.
It uses the router's identity to generate a consistent filename:
For a router named mtr, the resulting files are:
The script creates two different types of backup.
Binary backup
The binary backup is useful for restoring the router configuration to a compatible MikroTik environment.
Readable configuration export
The export produces an .rsc file containing configuration commands. This is useful when I want to inspect settings, compare configurations, or recover individual configuration sections.
I prefer having both formats because they serve different recovery and troubleshooting purposes.
Verify the backup
After creating the files, the script checks that both exist:
This is a basic verification step. It confirms that the expected files exist, although it does not prove that they are valid or can be restored.
Using a fixed filename also prevents the router from accumulating timestamped backup files on every run. For a small home router, keeping the latest local backup can be a practical way to avoid unnecessary storage usage.
One detail worth remembering is that an existing .rsc file may need to be removed before creating a new export with the same name. The backup workflow should account for that so repeated scheduled runs behave correctly.
For better protection, I would also copy the backup files to external storage. A backup stored only on the router cannot help if the router's storage becomes inaccessible.
3. RouterBOARD firmware updates
Updating RouterOS does not necessarily mean the RouterBOARD firmware has also been updated.
The routerboard-auto-update script checks the current firmware against the upgrade firmware version:
If the versions differ, the script creates a backup, initiates the firmware upgrade, and reboots the router.
If the versions already match, there is no reason to perform another upgrade.
The script waits two minutes after startup before checking the firmware. This gives the router some time to finish booting before the maintenance task begins.
Because this process can interrupt network connectivity, I would test it carefully and make sure I have a local recovery method available before enabling unattended firmware upgrades.
4. Scheduling the automation
RouterOS includes a scheduler, so there is no need to run a separate automation service just to execute these scripts periodically.
I use two scheduled tasks.
Weekly RouterOS update
The routeros-weekly-update scheduler runs the package update script every week at 03:00.
The intention is to perform maintenance during a quieter period, when a short network interruption is less likely to be disruptive.
RouterBOARD update at startup
The routerboard-startup-update scheduler runs the firmware update script at startup.
This means the firmware check can run after the router reboots following a RouterOS package upgrade.
The startup task is also useful after a manual reboot, although it should only install firmware when an upgrade is actually required.
The complete scheduler definitions, script policies, and comments are included in the GitLab snippet.
5. Logging and troubleshooting
Automation is useful only if I can tell what it is doing.
I use RouterOS logging to record significant events, such as:
Checking for RouterOS updates.
Detecting a new software version.
Starting a configuration backup.
Completing or failing backup verification.
Detecting a RouterBOARD firmware mismatch.
Starting an upgrade and rebooting.
When troubleshooting, I can inspect the router's log through WinBox or the terminal:
I can also run each script manually before relying on the scheduler.
For example, to test the backup process:
Testing each component independently makes it easier to find problems before they affect the entire update workflow.
6. A few things to consider before using it
This is a small homelab automation example, not a replacement for a full device-management platform. There are a few things I would check before using it on a production or remotely managed router.
Backup failure handling: The update scripts should stop if the backup fails. The current backup script uses :error when a required file is missing, but the caller should also be tested to confirm that a backup failure prevents the upgrade from proceeding.
Existing export files: Because the backup uses a fixed filename, the old .rsc file should be handled before generating the new export. Otherwise, a repeated run may fail to create the export.
Permissions: RouterOS script and scheduler policies must be sufficient for the required operations. Avoid granting broader permissions than necessary, particularly when scripts can access sensitive configuration data.
Recovery: Automatic updates can cause downtime, and a script cannot guarantee recovery from every upgrade failure. Keep a known-good backup outside the router and ensure you can access the device locally if the network does not return.
Testing: Start by running the backup script manually, verifying the output files, and testing the update logic on a device where a reboot is acceptable.
Conclusion
I like this approach because it uses the tools already available in RouterOS. There is no additional controller, container, or external service to maintain just to perform basic router maintenance.
The scripts separate the main responsibilities into manageable pieces: package updates, firmware updates, and backups. The scheduler ties them together, while logging makes the process easier to troubleshoot.
For a small homelab, this is a useful step towards treating network infrastructure as something that can be maintained consistently rather than relying on manual reminders.
It is also a good example of a principle I try to follow in my lab: automate repetitive tasks, keep the implementation simple, and make sure there is a recovery plan before automating changes that can take the network offline.
Source code: MikroTik RouterOS Automated Update and Backup — GitLab Snippet
Last updated