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:
- Creates a backup of the router configuration.
- Exports the configuration to a
.rsc
file for later restoration. - Uploads both files to an FTP server, including the backup
.backup
and configuration.rsc
files. - 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
-
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.
- FTP Server Details: The IP address (
-
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
).
- The script constructs a unique filename for the backup by combining:
-
Backup and Export:
- The backup is saved using the
system backup save
command, and the configuration is exported to a.rsc
file using theexport
command.
- The backup is saved using the
-
FTP Upload:
- The script uploads both the
.backup
and.rsc
files to the FTP server using thetool fetch
command. These files are uploaded to theftpPath
directory.
- The script uploads both the
-
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:
-
Create the Script:
- First, copy the backup script into MikroTik’s system script section:
- Navigate to System → Scripts.
- Create a new script and paste the backup script code.
- First, copy the backup script into MikroTik’s system script section:
-
Add the Script to the Scheduler:
- Navigate to System → Scheduler.
- 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
- Name:
-
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.