> For the complete documentation index, see [llms.txt](https://en.itmatic101.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://en.itmatic101.com/networking/automating-mikrotik-routeros-v7-updates-and-backups.md).

# 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](https://gitlab.com/-/snippets/6067155)

## The idea: automate the boring stuff

The workflow is deliberately simple:

1. Check whether a new RouterOS version is available.
2. Back up the current configuration before upgrading.
3. Install the RouterOS update and allow the router to reboot.
4. Check the RouterBOARD firmware after startup.
5. 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.

```
/system/package/update/set channel=stable
/system/package/update/check-for-updates
:delay 10s

:local updateStatus [/system/package/update/get status]
```

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:

```
:if ($updateStatus = "New version is available") do={

    :log warning "Auto-update: new version available"

    :log info "Auto-backup: backing up router config before upgrade"

    /system/script/run backup-before-upgrade

    :delay 5s

    :log warning "Auto-update: installing RouterOS update"

    /system/package/update/install
}
```

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:

```
:local routerName [/system identity get name]
:local backupName ($routerName . "_config")
```

For a router named `mtr`, the resulting files are:

```
mtr_config.backup
mtr_config.rsc
```

The script creates two different types of backup.

### Binary backup

```
/system backup save name=$backupName
```

The binary backup is useful for restoring the router configuration to a compatible MikroTik environment.

### Readable configuration export

```
/export file=$backupName
```

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:

```
:if ([:len [/file find name=($backupName . ".backup")]] > 0) do={
    :if ([:len [/file find name=($backupName . ".rsc")]] > 0) do={
        :log info ("Backup successful: " . $backupName)
    } else={
        :log error "Backup failed: export file missing"
        :error "Backup failed: export file missing"
    }
}
```

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:

```
:local currentFirmware [/system/routerboard/get current-firmware]
:local upgradeFirmware [/system/routerboard/get upgrade-firmware]

:if ($currentFirmware != $upgradeFirmware) do={
    # Back up before upgrading
    /system/script/run backup-before-upgrade

    :delay 5s

    /system/routerboard/upgrade

    :delay 5s

    :log warning "RouterBOARD: rebooting to apply firmware"

    /system/reboot
}
```

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.

```
/system scheduler
add name=routeros-weekly-update \
    interval=1w \
    start-time=03:00:00 \
    on-event="/system script run routeros-auto-update"
```

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.

```
/system scheduler
add name=routerboard-startup-update \
    start-time=startup \
    on-event="/system script run routerboard-auto-update"
```

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:

```
/log print
```

I can also run each script manually before relying on the scheduler.

For example, to test the backup process:

```
/system script run backup-before-upgrade
```

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](https://gitlab.com/-/snippets/6067155)
