Backup Automation
Backup Automation
Section titled “Backup Automation”RouterOS can automatically create binary backups and RSC configuration exports on a schedule, then deliver them off-router via email, FTP, or SFTP. This ensures a current copy of your configuration survives device failure.
For a reference on backup vs export format differences, see Backup.
Sub-menus
Section titled “Sub-menus”/system backup/export/system scheduler/system script/tool e-mail/tool fetchBackup Types
Section titled “Backup Types”RouterOS provides two complementary formats that serve different recovery scenarios. Both should be included in automation scripts.
Binary Backup (.backup) | Text Export (.rsc) | |
|---|---|---|
| Command | /system backup save | /export file= |
| Format | Binary | Human-readable script |
| Includes MAC addresses | Yes | No |
| Password protection | Optional | No |
| Restore to same device | Yes | Yes |
| Migrate to new hardware | Not recommended | Yes |
| Partial restore | No | Yes (edit file) |
| Git-friendly diffs | No | Yes |
Writing a Backup Script
Section titled “Writing a Backup Script”Keep logic in a script and call it from the scheduler. This separates concerns and makes testing easier.
Combined backup and export
Section titled “Combined backup and export”/system scriptadd name=auto-backup policy=read,write,policy,test,sensitive source={ :local id [/system identity get name] :local d [/system clock get date] :local t [/system clock get time] :local base ("$id-$d-$t")
/system backup save name=$base password=YourSecurePassword /export compact file=$base :log info ("Backup created: $base")}Schedule the script
Section titled “Schedule the script”/system scheduleradd name=daily-backup \ on-event=auto-backup \ start-time=03:30:00 \ interval=1d \ policy=read,write,policy,test,sensitiveThe scheduler calls auto-backup once daily at 03:30. If the router reboots, the next scheduled run occurs at 03:30 the following day.
Run once at startup
Section titled “Run once at startup”To additionally run a backup shortly after boot (useful for capturing state before automated changes):
/system scheduleradd name=startup-backup \ on-event=auto-backup \ start-time=startup \ interval=0start-time=startup runs the script 3 seconds after the console starts. Setting interval=0 means run once rather than repeatedly.
Script Policies
Section titled “Script Policies”The script and scheduler must share the same policy set. For a backup script that creates files and reads all settings:
| Policy | Required for |
|---|---|
read | Reading configuration |
write | Creating backup files |
policy | Accessing sensitive policy objects |
test | Running test commands |
sensitive | Exporting sensitive values (show-sensitive) |
Off-Router Delivery
Section titled “Off-Router Delivery”Backups stored only on the router are lost when the router fails. Deliver copies to external systems immediately after creation.
Email delivery
Section titled “Email delivery”Configure the SMTP client once:
/tool e-mailset address=smtp.example.com \ port=587 \ password=AppPassword \ tls=starttlsExtend the backup script to send the files as attachments:
/system scriptadd name=auto-backup policy=read,write,policy,test,sensitive source={ :local id [/system identity get name] :local d [/system clock get date] :local t [/system clock get time] :local base ("$id-$d-$t")
/system backup save name=$base password=YourSecurePassword /export compact file=$base :delay 5s
/tool e-mail send \ subject=("RouterOS backup: $id $d $t") \ body=("Automated backup from $id") \ file=("$base.backup,$base.rsc") :log info ("Backup emailed: $base")}The 5-second delay ensures the files are fully written before attaching. See Email for SMTP configuration details and provider-specific TLS settings.
FTP upload
Section titled “FTP upload”Use /tool fetch with upload=yes to push files to an FTP server:
/tool fetch url="ftp://backup.example.com/routers/$id/$base.backup" \ src-path="$base.backup" \ upload=yes \ user=ftpuser \ password=FtpPasswordSFTP upload
Section titled “SFTP upload”SFTP upload uses the same /tool fetch command with the sftp:// scheme:
/tool fetch url="sftp://backup.example.com/routers/$id/$base.backup" \ src-path="$base.backup" \ upload=yes \ user=sftpuser \ password=SftpPasswordTo verify server identity, provide the host key:
/tool fetch url="sftp://backup.example.com/routers/$id/$base.backup" \ src-path="$base.backup" \ upload=yes \ user=sftpuser \ password=SftpPassword \ host-key="AAAAB3NzaC1yc2EAAAA..."See Fetch for full parameter reference.
Version-Controlled Configuration
Section titled “Version-Controlled Configuration”RSC exports are plain text and integrate cleanly with Git. RouterOS has no native Git client, so the standard workflow is:
- RouterOS exports RSC to a file on schedule
- An external host (Linux server, CI runner) fetches the file via SCP/SFTP and commits it
On the router — export on schedule
Section titled “On the router — export on schedule”/system scriptadd name=export-config policy=read,write,policy,test source={ /export compact file=running-config}
/system scheduleradd name=nightly-export \ on-event=export-config \ start-time=02:00:00 \ interval=1dThis overwrites running-config.rsc with the current configuration each night, keeping a single canonical file rather than date-stamped copies.
On an external host — fetch and commit
Section titled “On an external host — fetch and commit”#!/bin/bash# Pull latest config export and commit to gitROUTER=192.168.1.1ROUTER_USER=adminREPO=/opt/router-configs
sftp ${ROUTER_USER}@${ROUTER}:running-config.rsc ${REPO}/running-config.rsc
cd ${REPO}git add running-config.rscgit commit -m "RouterOS config export $(date -Iseconds)" || truegit pushSchedule this script via cron on the external host to run after the RouterOS export completes.
Including sensitive values
Section titled “Including sensitive values”By default, /export omits passwords and private keys. To include them (required if the export is your sole recovery source):
/export compact show-sensitive file=running-config-fullFile Retention
Section titled “File Retention”Dated backup files accumulate on the router’s storage. Remove old files to prevent disk exhaustion:
/system scriptadd name=auto-backup policy=read,write,policy,test,sensitive source={ :local id [/system identity get name] :local d [/system clock get date] :local t [/system clock get time] :local base ("$id-$d-$t")
# Create backups /system backup save name=$base password=YourSecurePassword /export compact file=$base
# Clean up files older than 7 days (keep last 7 entries by name prefix) :local files [/file find where name~$id] :foreach f in=$files do={ :if ([:len $files] > 14) do={ /file remove $f } }
:log info ("Backup created: $base")}Check available storage before implementing long retention periods:
/system resource printRestore Procedures
Section titled “Restore Procedures”Restore from binary backup
Section titled “Restore from binary backup”/system backup load name=MyRouter-2026-03-22-030000.backup password=YourSecurePasswordRouterOS prompts for confirmation, then reboots and applies the configuration.
Restore from RSC export
Section titled “Restore from RSC export”/import file-name=running-config.rscFor partial restore, download the RSC file, edit it to retain only the desired sections, upload it back, and import.
Restore after factory reset
Section titled “Restore after factory reset”If the router is in a blank state (factory defaults):
- Connect via WinBox or serial console
- Upload the backup or RSC file via FTP/SFTP, WinBox Files panel, or Netinstall
- Import or load the file
# After uploading the file/system backup load name=router-config.backup password=YourSecurePasswordBest Practices
Section titled “Best Practices”| Practice | Reason |
|---|---|
Keep script logic in /system script, not inline in the scheduler | Easier to test and modify |
| Use both binary backup and RSC export | Covers different failure scenarios |
| Deliver backups off-router immediately | On-device copies are lost with the device |
| Test restoration periodically | Untested backups may be corrupted or incomplete |
Use compact export for version control | Reduces diff noise from default values |
Monitor scheduler run-count and logs | Confirms backups are actually running |
| Store backup passwords in a password manager | Encrypted backups are useless without the password |
Troubleshooting
Section titled “Troubleshooting”Backup file not created
Section titled “Backup file not created”# Check available disk space/system resource print
# Check scheduler is running/system scheduler print
# Run script manually to see errors/system script run auto-backup
# Check logs/log print where message~"backup"Email delivery fails
Section titled “Email delivery fails”- Confirm SMTP settings:
/tool e-mail print - Verify TLS mode matches the port (
starttls→ port 587, implicit TLS → port 465) - Check for provider-specific app passwords
- Test manually:
/tool e-mail send to="[email protected]" subject="test" body="test"
FTP/SFTP upload fails
Section titled “FTP/SFTP upload fails”- Verify credentials and URL format
- Check the destination directory exists and is writable
- Test fetch manually with the exact URL
- For SFTP, provide
host-keyto avoid identity verification failures
Scheduler not running script
Section titled “Scheduler not running script”- Verify script name matches
on-eventexactly - Check scheduler and script have matching
policysettings - Look for errors in logs:
/log print where topics~"script" - Run the script manually to confirm it works