edufortes

Automating Backups on MikroTik Router with FTP

MikroTik routers provide an excellent way to automate backups using scripts. In this guide, we’ll walk through creating a backup script that saves configurations and uploads them via FTP to a remote server. Additionally, we’ll discuss how to schedule this script to run periodically using MikroTik’s scheduler (cron-like feature).

Overview of the Script

The script performs the following tasks:

  1. Creates a backup of the router configuration.
  2. Exports the configuration to a .rsc file for later restoration.
  3. Uploads both files to an FTP server, including the backup .backup and configuration .rsc files.
  4. Uses the current system date to generate a unique filename for each backup.

Here is the final working version of the script:

MikroTik Backup Script

## Environment-specific configuration
:local ftpServer    "192.168.1.2"
:local ftpUser      "mikrotik"
:local ftpPassword  "1ns3cur3p4ssw0rt"
:local ftpPath      "/backups/mikrotik/"

:local dstFile      "backup"
:local srcFile $dstFile
:local myVer value=[/system package update get installed-version]
:local id value=[/system identity get value-name=name]
:local date [/system clock get date]

## Construct destination file name
:set dstFile ($dstFile . "-" . $id . "-" . $myVer . "-" . $date)

## Perform the actual backup / export
/system backup save name="$srcFile"
/export file="$srcFile"

## Upload both files via FTP
:foreach i in=(".backup", ".rsc") do={
  /tool fetch address=$ftpServer src-path=($srcFile . $i) user=$ftpUser mode=ftp password=$ftpPassword dst-path=($ftpPath . $dstFile . $i) upload=yes port=21
}

## Log
:log info ("Configuration backup created on router $[/system identity get name].")

Script Explanation

  1. Environment-specific configuration:

    • FTP Server Details: The IP address (10.0.1.2), username (backups-mkt), and password (324zf_ucd4Q) are configured for the FTP server. The backup files will be uploaded to the /backup-infra/mikrotik/ directory.
  2. Backup File Naming:

    • The script constructs a unique filename for the backup by combining:
      • backup (base name),
      • Router identity name ($id),
      • Router firmware version ($myVer),
      • Current date in YYYY-MM-DD format ($date).
  3. Backup and Export:

    • The backup is saved using the system backup save command, and the configuration is exported to a .rsc file using the export command.
  4. FTP Upload:

    • The script uploads both the .backup and .rsc files to the FTP server using the tool fetch command. These files are uploaded to the ftpPath directory.
  5. Logging:

    • A log message is generated to confirm the backup was successfully created, providing feedback in the system log.

Scheduling the Script to Run Periodically

To automate the backup process, you can schedule the script to run at specific intervals using MikroTik’s built-in scheduler. The scheduler is similar to cron jobs in Linux.

Steps to Schedule the Backup Script:

  1. Create the Script:

    • First, copy the backup script into MikroTik’s system script section:
      • Navigate to SystemScripts.
      • Create a new script and paste the backup script code.
  2. Add the Script to the Scheduler:

    • Navigate to SystemScheduler.
    • Add a new scheduled task:
      • Name: BackupConfig
      • Start Time: Set the desired start time, e.g., 00:00:00 for midnight.
      • Interval: Set the interval to the desired frequency, e.g., 1d for daily backups.
      • On Event: Add the command to run your script:
        /system script run backup-config
  3. Save the Scheduler:

    • Click Apply to schedule the backup script.

Example: Scheduling a Daily Backup at Midnight

To schedule the script to run daily at midnight, the configuration in the scheduler will look like this:

Name Start Time Interval On Event
BackupConfig 00:00:00 1d /system script run backup-config

This will ensure the script runs once every day at midnight and performs a backup, saving the configuration and uploading the files to the FTP server.


Conclusion

With this script and scheduling setup, your MikroTik router will automatically create backups and upload them to your remote FTP server at the scheduled time. It’s a simple yet powerful way to ensure your network configuration is safely backed up without manual intervention.

By using MikroTik’s scheduler, you can tailor the frequency of backups to meet your needs, whether it’s daily, weekly, or on any other interval.

Let me know if you need any adjustments or have further questions! Happy networking! 🚀


Feel free to adjust the FTP server details, backup folder, and timing according to your setup.

#infra

Reply to this post by email ↪