mirror of
https://github.com/beeyev/Mikrotik-RouterOS-automatic-backup-and-update.git
synced 2024-11-23 08:03:06 +01:00
New version 20.01.20
Script was completely rewritten and became even better!
This commit is contained in:
parent
a7734dfcf9
commit
17c9c9b3ae
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/ignore
|
315
BackupAndUpdate.rsc
Normal file
315
BackupAndUpdate.rsc
Normal file
@ -0,0 +1,315 @@
|
|||||||
|
# Script name: BackupAndUpdate
|
||||||
|
#
|
||||||
|
#----------SCRIPT INFORMATION---------------------------------------------------
|
||||||
|
#
|
||||||
|
# Script: Mikrotik RouterOS automatic backup & update
|
||||||
|
# Version: 20.01.20
|
||||||
|
# Created: 07/08/2018
|
||||||
|
# Updated: 20/01/2020
|
||||||
|
# Author: Alexander Tebiev
|
||||||
|
# Website: https://github.com/beeyev
|
||||||
|
# You can contact me by e-mail at tebiev@mail.com
|
||||||
|
#
|
||||||
|
# IMPORTANT!
|
||||||
|
# Minimum supported RouterOS version is v6.43.7
|
||||||
|
#
|
||||||
|
#----------MODIFY THIS SECTION AS NEEDED----------------------------------------
|
||||||
|
## Notification e-mail
|
||||||
|
## (Make sure you have configurated Email settings in Tools -> Email)
|
||||||
|
:local emailAddress "yourmail@example.com";
|
||||||
|
|
||||||
|
## Script mode, possible values: backup, osupdate, osnotify.
|
||||||
|
# backup - only backup will be perfomed. (default value, if none provided)
|
||||||
|
# osupdate - script will make a backup and install new RouterOS if it is available.
|
||||||
|
# osnotify - script will make a backup and notify about new RouterOS version.
|
||||||
|
:local scriptMode "osnotify";
|
||||||
|
|
||||||
|
## Backup encryption password, no encryption if no password.
|
||||||
|
:local backupPassword ""
|
||||||
|
|
||||||
|
## If true, passwords will be included in exported config.
|
||||||
|
:local sensetiveDataInConfig false;
|
||||||
|
|
||||||
|
## Update channel. Possible values: stable, long-term, testing, development
|
||||||
|
:local updateChannel "stable";
|
||||||
|
|
||||||
|
## Install only patch versions of RouterOS updates.
|
||||||
|
## Works only if you set scriptMode to "osupdate"
|
||||||
|
## Means that new update will be installed only if MAJOR and MINOR version numbers remained the same as currently installed RouterOS.
|
||||||
|
## Example: v6.43.6 => major.minor.PATCH
|
||||||
|
## Script will send information if new version is greater than just patch.
|
||||||
|
:local installOnlyPatchUpdates false;
|
||||||
|
##------------------------------------------------------------------------------------------##
|
||||||
|
# !!!! DO NOT CHANGE ANYTHING BELOW THIS LINE, IF YOU ARE NOT SURE WHAT YOU ARE DOING !!!! #
|
||||||
|
##------------------------------------------------------------------------------------------##
|
||||||
|
|
||||||
|
:log info "\r\nBkp&Upd: script \"Mikrotik RouterOS automatic backup & update\" started.";
|
||||||
|
|
||||||
|
#Check proper email config
|
||||||
|
:if ([:len $emailAddress] = 0 or [:len [/tool e-mail get address]] = 0 or [:len [/tool e-mail get from]] = 0) do={
|
||||||
|
:log error ("Bkp&Upd: Email configuration is not correct, please check Tools -> Email. Script stopped.");
|
||||||
|
:error "Bkp&Upd: bye!";
|
||||||
|
}
|
||||||
|
|
||||||
|
############### vvvvvvvvv GLOBALS vvvvvvvvv ###############
|
||||||
|
# Function converts standard mikrotik build versions to the number.
|
||||||
|
# Possible arguments: osVer
|
||||||
|
# Example:
|
||||||
|
# :put [$brGlobalFuncGetOsVerNum osVer=[/system routerboard get current-RouterOS]];
|
||||||
|
# result will be: 64301, because current RouterOS version is: 6.43.1
|
||||||
|
:global brGlobalFuncGetOsVerNum do={
|
||||||
|
:local osVerNum;
|
||||||
|
:local osVerMicroPart;
|
||||||
|
:local zro 0;
|
||||||
|
:local tmp;
|
||||||
|
|
||||||
|
:local dotPos1 [:find $osVer "." 0];
|
||||||
|
|
||||||
|
:if ($dotPos1 > 0) do={
|
||||||
|
|
||||||
|
# AA
|
||||||
|
:set osVerNum [:pick $osVer 0 $dotPos1];
|
||||||
|
|
||||||
|
:local dotPos2 [:find $osVer "." $dotPos1];
|
||||||
|
#Taking minor version, everything after first dot
|
||||||
|
:if ([:len $dotPos2] = 0) do={:set tmp [:pick $osVer ($dotPos1+1) [:len $osVer]];}
|
||||||
|
#Taking minor version, everything between first and second dots
|
||||||
|
:if ($dotPos2 > 0) do={:set tmp [:pick $osVer ($dotPos1+1) $dotPos2];}
|
||||||
|
|
||||||
|
# AA 0B
|
||||||
|
:if ([:len $tmp] = 1) do={:set osVerNum "$osVerNum$zro$tmp";}
|
||||||
|
# AA BB
|
||||||
|
:if ([:len $tmp] = 2) do={:set osVerNum "$osVerNum$tmp";}
|
||||||
|
|
||||||
|
:if ($dotPos2 > 0) do={
|
||||||
|
:set tmp [:pick $osVer ($dotPos2+1) [:len $osVer]];
|
||||||
|
# AA BB 0C
|
||||||
|
:if ([:len $tmp] = 1) do={:set osVerNum "$osVerNum$zro$tmp";}
|
||||||
|
# AA BB CC
|
||||||
|
:if ([:len $tmp] = 2) do={:set osVerNum "$osVerNum$tmp";}
|
||||||
|
} else={
|
||||||
|
# AA BB 00
|
||||||
|
:set osVerNum "$osVerNum$zro$zro";
|
||||||
|
}
|
||||||
|
} else={
|
||||||
|
# AA 00 00
|
||||||
|
:set osVerNum "$osVer$zro$zro$zro$zro";
|
||||||
|
}
|
||||||
|
|
||||||
|
:return $osVerNum;
|
||||||
|
}
|
||||||
|
|
||||||
|
:global brGlobalVarUpdateStep;
|
||||||
|
############### ^^^^^^^^^ GLOBALS ^^^^^^^^^ ###############
|
||||||
|
|
||||||
|
#Current date time in format: 2020jan15-221324
|
||||||
|
:local dateTime ([:pick [/system clock get date] 7 11] . [:pick [/system clock get date] 0 3] . [:pick [/system clock get date] 4 6] . "-" . [:pick [/system clock get time] 0 2] . [:pick [/system clock get time] 3 5] . [:pick [/system clock get time] 6 8]);
|
||||||
|
|
||||||
|
:local deviceOsVerInst [/system package update get installed-version];
|
||||||
|
:local deviceOsVerInstNum [$brGlobalFuncGetOsVerNum osVer=$deviceOsVerInst];
|
||||||
|
:local deviceOsVerAvail "";
|
||||||
|
:local deviceOsVerAvailNum 0;
|
||||||
|
:local deviceRbModel [/system routerboard get model];
|
||||||
|
:local deviceRbSerialNumber [/system routerboard get serial-number];
|
||||||
|
:local deviceRbCurrentFw [/system routerboard get current-firmware];
|
||||||
|
:local deviceRbUpgradeFw [/system routerboard get upgrade-firmware];
|
||||||
|
:local deviceIdentityName [/system identity get name];
|
||||||
|
:local deviceUpdateChannel [/system package update get channel];
|
||||||
|
|
||||||
|
:local isOsUpdateAvailable false;
|
||||||
|
:local isOsNeedsToBeUpdated false;
|
||||||
|
|
||||||
|
:local mailSubject "";
|
||||||
|
:local mailBody "";
|
||||||
|
|
||||||
|
:local mailBodyDeviceInfo "\r\n\r\nDevice information: \r\nIdentity: $deviceIdentityName \r\nModel: $deviceRbModel \r\nSerial number: $deviceRbSerialNumber \r\nCurrent RouterOS: $deviceOsVerInst ($[/system package update get channel]) $[/system resource get build-time] \r\nCurrent routerboard FW: $deviceRbCurrentFw \r\nDevice uptime: $[/system resource get uptime]";
|
||||||
|
:local mailBodyCopyright "\r\n\r\nMikrotik RouterOS automatic backup & update \r\nhttps://github.com/beeyev/Mikrotik-RouterOS-automatic-backup-and-update";
|
||||||
|
:local changelogUrl ("Check RouterOS changelog: https://mikrotik.com/download/changelogs/" . $updateChannel . "-release-tree");
|
||||||
|
|
||||||
|
:local mailAttachments [:toarray ""];
|
||||||
|
|
||||||
|
:local updateStep $brGlobalVarUpdateStep;
|
||||||
|
:do {/system script environment remove brGlobalVarUpdateStep;} on-error={}
|
||||||
|
:if ([:len $updateStep] = 0) do={
|
||||||
|
:set updateStep 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
## STEP ONE: Creating backups, checking for new RouterOs version and sending email with backups,
|
||||||
|
## steps 2 and 3 are fired only if script is set to automatically update device and if new RouterOs is available.
|
||||||
|
:if ($updateStep = 1) do={
|
||||||
|
:log info ("Bkp&Upd: Performing the first step.");
|
||||||
|
|
||||||
|
# Set email subject and body for the first step
|
||||||
|
:set mailSubject "Bkp&Upd: Backup completed - $[:pick $deviceIdentityName 0 18].";
|
||||||
|
:set mailBody "\"$deviceIdentityName\" system backups were created and attached to this email.";
|
||||||
|
|
||||||
|
## PREPARE BACKUP DATA
|
||||||
|
:log info ("Bkp&Upd: Creating system backups.");
|
||||||
|
|
||||||
|
:local bname "$deviceIdentityName.$deviceBoardModel.$deviceSerialNumber.v$deviceOsVerInst.$deviceUpdateChannel.$dateTime";
|
||||||
|
:local backupFileSys "$bname.backup";
|
||||||
|
:local backupFileConfig "$bname.rsc";
|
||||||
|
|
||||||
|
:set mailAttachments {$backupFileSys;$backupFileConfig};
|
||||||
|
|
||||||
|
## Make system backup
|
||||||
|
:if ([:len $backupPassword] = 0) do={
|
||||||
|
/system backup save dont-encrypt=yes name=$bname;
|
||||||
|
} else={
|
||||||
|
/system backup save password=$backupPassword name=$bname;
|
||||||
|
}
|
||||||
|
:log info ("Bkp&Upd: System backup created. $backupFileSys");
|
||||||
|
|
||||||
|
## Export config file
|
||||||
|
:if ($sensetiveDataInConfig = true) do={
|
||||||
|
/export compact file=$bname;
|
||||||
|
} else={
|
||||||
|
/export compact hide-sensitive file=$bname;
|
||||||
|
}
|
||||||
|
:log info ("Bkp&Upd: Config file was exported. $backupFileConfig");
|
||||||
|
|
||||||
|
# Checking for new RouterOS version
|
||||||
|
if ($scriptMode = "osupdate" or $scriptMode = "osnotify") do={
|
||||||
|
log info ("Bkp&Upd: Checking for new RouterOS version. Current version is: $deviceOsVerInst");
|
||||||
|
/system package update set channel=$updateChannel;
|
||||||
|
/system package update check-for-updates;
|
||||||
|
:delay 5s;
|
||||||
|
:set deviceOsVerAvail [/system package update get latest-version];
|
||||||
|
|
||||||
|
# If there is a problem getting information about available RouterOS from server
|
||||||
|
:if ([:len $deviceOsVerAvail] = 0) do={
|
||||||
|
:log warning ("Bkp&Upd: There is a problem getting information about new RouterOS from server.");
|
||||||
|
:set mailSubject ($mailSubject . " Error: No data about new RouterOS!")
|
||||||
|
:set mailBody ($mailBody . "\r\n\r\nError occured! \r\nMikrotik couldn't get any information about new RouterOS from server! \r\nWatch additional information in device logs.")
|
||||||
|
} else={
|
||||||
|
#Get numeric version of OS
|
||||||
|
:set deviceOsVerAvailNum [$brGlobalFuncGetOsVerNum osVer=$deviceOsVerAvail];
|
||||||
|
|
||||||
|
# Checking if OS on server is greater than installed one.
|
||||||
|
:if ($deviceOsVerAvailNum > $deviceOsVerInstNum) do={
|
||||||
|
:set isOsUpdateAvailable true;
|
||||||
|
:log info ("Bkp&Upd: New RouterOS is available! $deviceOsVerAvail");
|
||||||
|
} else={
|
||||||
|
:set mailSubject ($mailSubject . " No new OS updates.")
|
||||||
|
:set mailBody ($mailBody . "\r\nYour system is up to date.")
|
||||||
|
:log info ("Bkp&Upd: System is already up to date.")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} else={
|
||||||
|
#Delay after creating backups
|
||||||
|
:delay 5s;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
# if new OS version is available to install
|
||||||
|
if ($isOsUpdateAvailable = true) do={
|
||||||
|
# If we only need to notify about new available version
|
||||||
|
if ($scriptMode = "osnotify") do={
|
||||||
|
:set mailSubject ($mailSubject . " New RouterOS is available! v.$deviceOsVerAvail")
|
||||||
|
:set mailBody ($mailBody . "\r\n\r\nNew RouterOS version is available to install: v.$deviceOsVerAvail ($updateChannel) \r\n$changelogUrl")
|
||||||
|
}
|
||||||
|
|
||||||
|
# if we need to initiate RouterOs update process
|
||||||
|
if ($scriptMode = "osupdate") do={
|
||||||
|
:set isOsNeedsToBeUpdated true;
|
||||||
|
# if we need to install only patch updates
|
||||||
|
:if ($installOnlyPatchUpdates = true) do={
|
||||||
|
#Check if Major and Minor builds are the same.
|
||||||
|
:if ([:pick $deviceOsVerInstNum 0 ([:len $deviceOsVerInstNum]-2)] = [:pick $deviceOsVerAvailNum 0 ([:len $deviceOsVerAvailNum]-2)]) do={
|
||||||
|
:log info ("Bkp&Upd: New patch version of RouterOS firmware is available.");
|
||||||
|
} else={
|
||||||
|
:log info ("Bkp&Upd: New minor version of RouterOS firmware is available. You need to update it manually.");
|
||||||
|
:set mailSubject ($mailSubject . " New major RouterOS is available: v.$deviceOsVerAvail!");
|
||||||
|
:set mailBody ($mailBody . "\r\n\r\nNew major RouterOS version is available to install: v.$deviceOsVerAvail ($updateChannel). \r\nYou chose to automatically install only patch updates, so this major update you need to install manually. \r\n$changelogUrl");
|
||||||
|
:set isOsNeedsToBeUpdated false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#Check again, because this variable could be changed during checking for installing only patch updats
|
||||||
|
if ($isOsNeedsToBeUpdated = true) do={
|
||||||
|
:set mailSubject ($mailSubject . " New RouterOS is going to be installed! v.$deviceOsVerInst -> v.$deviceOsVerAvail");
|
||||||
|
:set mailBody ($mailBody . "\r\n\r\nYour Mikrotik will be updated to the new RouterOS version from v.$deviceOsVerInst to v.$deviceOsVerAvail (Update channel: $updateChannel) \r\nFinal report with detailed information will be sent when update process is completed. \r\nIf you have not received second email in the next 5 minutes, then probably something went wrong.");
|
||||||
|
#!! There is more code connected to this part and first step at the end of the script.
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Combine fisrst step email
|
||||||
|
:set mailBody ($mailBody . $mailBodyDeviceInfo . $mailBodyCopyright);
|
||||||
|
:log info ("Bkp&Upd: Sending email with backups in attachment.");
|
||||||
|
}
|
||||||
|
|
||||||
|
# Remove function brGlobalFuncGetOsVerNum from global environment to keep it fresh and clean.
|
||||||
|
:do {/system script environment remove brGlobalFuncGetOsVerNum;} on-error={}
|
||||||
|
|
||||||
|
## STEP TWO: (after first reboot) routerboard firmware upgrade
|
||||||
|
## steps 2 and 3 are fired only if script is set to automatically update device and if new RouterOs is available.
|
||||||
|
:if ($updateStep = 2) do={
|
||||||
|
:log info ("Bkp&Upd: Performing the second step.");
|
||||||
|
## RouterOS is the latest, let's check for upgraded routerboard firmware
|
||||||
|
if ($deviceRbCurrentFw != $deviceRbUpgradeFw) do={
|
||||||
|
:log info "Bkp&Upd: Upgrading routerboard firmware from v.$deviceRbCurrentFw to v.$deviceRbUpgradeFw";
|
||||||
|
## Start the upgrading process
|
||||||
|
/system routerboard upgrade;
|
||||||
|
## Wait until the upgrade is completed
|
||||||
|
:delay 5s;
|
||||||
|
:log info "Bkp&Upd: routerboard upgrading process was completed, going to reboot in a moment!";
|
||||||
|
## Set scheduled task to send final report on the next boot, task will be deleted when is is done. (That is why you should keep original script name)
|
||||||
|
/system schedule add name=BKPUPD-FINAL-REPORT-ON-NEXT-BOOT on-event=":delay 5s; /system scheduler remove BKPUPD-FINAL-REPORT-ON-NEXT-BOOT; :global brGlobalVarUpdateStep 3; :delay 10s; /system script run BackupAndUpdate;" start-time=startup interval=0;
|
||||||
|
## Reboot system to boot with new firmware
|
||||||
|
/system reboot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
## STEP THREE: Last step (after second reboot) sending final report
|
||||||
|
## steps 2 and 3 are fired only if script is set to automatically update device and if new RouterOs is available.
|
||||||
|
:if ($updateStep = 3) do={
|
||||||
|
:log info "Bkp&Upd: RouterOS and routerboard upgrading process of was completed. New RouterOS version: v.$deviceOsVerInst, routerboard firmware: v.$deviceRbCurrentFw.";
|
||||||
|
:set mailSubject "Bkp&Upd: Router - $[:pick $deviceIdentityName 0 18] has been upgraded to the new RouterOS v.$deviceOsVerInst!";
|
||||||
|
:set mailBody "RouterOS and routerboard upgrading process was completed. \r\nNew RouterOS version: v.$deviceOsVerInst, routerboard firmware: v.$deviceRbCurrentFw. \r\n$changelogUrl $mailBodyDeviceInfo $mailBodyCopyright";
|
||||||
|
}
|
||||||
|
|
||||||
|
##
|
||||||
|
## SENDING EMAIL
|
||||||
|
##
|
||||||
|
# Trying to send email with backups in attachment.
|
||||||
|
:if ($updateStep = 1 or $updateStep = 3) do={
|
||||||
|
:do {/tool e-mail send to=$emailAddress subject=$mailSubject body=$mailBody file=$mailAttachments;} on-error={
|
||||||
|
:log error "Bkp&Upd: could not send email message ($[/tool e-mail get last-status]). Going to try it again in a while."
|
||||||
|
:delay 5m;
|
||||||
|
:do {/tool e-mail send to=$emailAddress subject=$mailSubject body=$mailBody file=$mailAttachments;} on-error={
|
||||||
|
:log error "Bkp&Upd: could not send email message ($[/tool e-mail get last-status]) for the second time."
|
||||||
|
|
||||||
|
if ($isOsNeedsToBeUpdated = true) do={
|
||||||
|
:log warning "Bkp&Upd: script didn't initialise update process due to inability to send backups to email."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
:delay 5s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Remove backups which we have already sent during first step
|
||||||
|
:if ($updateStep = 1 and [/tool e-mail get last-status] = "succeeded") do={
|
||||||
|
:log info "Bkp&Upd: File system cleanup."
|
||||||
|
/file remove $mailAttachments;
|
||||||
|
:delay 2s;
|
||||||
|
|
||||||
|
# Fire update process only if backups were successfully sent
|
||||||
|
if ($isOsNeedsToBeUpdated = true) do={
|
||||||
|
|
||||||
|
## Set scheduled task to upgrade routerboard firmware on the next boot, task will be deleted when upgrade is done. (That is why you should keep original script name)
|
||||||
|
/system schedule add name=BKPUPD-UPGRADE-ON-NEXT-BOOT on-event=":delay 5s; /system scheduler remove BKPUPD-UPGRADE-ON-NEXT-BOOT; :global brGlobalVarUpdateStep 2; :delay 10s; /system script run BackupAndUpdate;" start-time=startup interval=0;
|
||||||
|
|
||||||
|
:log info "Bkp&Upd: everything is ready to install new RouterOS, going to reboot in a moment!"
|
||||||
|
## command is reincarnation of the "upgrade" command - doing exactly the same but under a different name
|
||||||
|
/system package update install;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
:log info "Bkp&Upd: script \"Mikrotik RouterOS automatic backup & update\" completed it's job.\r\n";
|
@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2018 Aleksandr Tebiev
|
Copyright (c) Alexander Tebiev tebiev@mail.com
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
71
README.md
71
README.md
@ -1,29 +1,66 @@
|
|||||||
# Mikrotik Firmware Auto Updater
|
# Mikrotik RouterOS automatic backup and update
|
||||||
The script automatically updates the router to the latest firmware.
|
|
||||||
When script finds new update it sends an email notification that upgrade process has started, system backup and config file are in the attachment. After firmware and routerboard got updated, it sends the second email which tells that upgrade process finished.
|
This script provides an ability to create Mikrotik's daily backups to email. You can also enable automatic RouterOS upgrade or leave only notifications about new firmware versions.
|
||||||
|
|
||||||
|
## Features:
|
||||||
|
- Ability to choose script operating mode according to your needs. *(Read below)*
|
||||||
|
- Script creates backups of the whole system and exported config.
|
||||||
|
- You can set a preferred update channel.
|
||||||
|
- If automatic updates are enabled, you can set script to install only patch versions of RouterOS updates. *This means if the current RouterOS version is v6.43.6, the script will automatically install v6.43.7 (new patch version) but not v6.44.0 (new minor version), for example.*
|
||||||
|
- Script includes primary information about the device into the email message. So you can easily find the backup you need among multiple devices.
|
||||||
|
- For safety purposes, an automatic update process will not be started if script could not send backups to email.
|
||||||
|
- Routerboard firmware can be automatically upgraded according to the installed RouterOS version.
|
||||||
|
|
||||||
|
|
||||||
|
## Script operating modes:
|
||||||
|
**Backups only** - script creates system and config backups and sends them to specified email as an attachment. Using email account as storage for your backups.
|
||||||
|
**Backups and notifications about new RouterOS release** - Except backups, script also checks for new RouterOS firmware release and provides this information in the email.
|
||||||
|
**Backups and automatic RouterOS upgrade** - Script makes a backup, then checks for new RouterOS version, and if new firmware released, script will initiate upgrade process. By the end, you receive two emails. The first one contains system backups of the previous RouterOS version, the second message will be sent when the upgrade process is done.
|
||||||
|
|
||||||
## How to use
|
## How to use
|
||||||
##### 1. Create new script
|
##### 1. Configure parameters
|
||||||
|
Take the [script](https://github.com/beeyev/Mikrotik-RouterOS-automatic-backup-and-update/raw/master/BackupAndUpdate.rsc) and configure it's parameters at the begining of the file.
|
||||||
|
This is not difficult because all parameters are well commented.
|
||||||
|
**Imprtant!** Don't forget to provide correct email address for backups and pay attention a `scriptMode` variable.
|
||||||
|
|
||||||
|
##### 2. Create new script
|
||||||
System -> Scripts [Add]
|
System -> Scripts [Add]
|
||||||
|
|
||||||
**Imprtant!** Script name has to be `firmware-updater`
|
**Imprtant!** Script name has to be `BackupAndUpdate`
|
||||||
Put script source and **set your email address** to the variable `emailAddress`
|
Put the script which you configured earlier into the source area.
|
||||||
![](https://github.com/beeyev/Mikrotik-Firmware-Auto-Updater/raw/master/howto/script-name.png)
|
![](https://github.com/beeyev/Mikrotik-RouterOS-automatic-backup-and-update/raw/master/howto/script-name.png)
|
||||||
|
|
||||||
##### 2. Configure mail server
|
##### 3. Configure mail server
|
||||||
Tools -> Email
|
Tools -> Email
|
||||||
Set your email server parameters. If you don't have one, i recommend to use [smtp2go.com](https://smtp2go.com "smtp2go.com") service, it allows sending a thousand emails per month for free.
|
Set your email server parameters. If you don't have one, i recommend to use [smtp2go.com](https://smtp2go.com "smtp2go.com") service, it allows sending a thousand emails per month for free.
|
||||||
![](https://github.com/beeyev/Mikrotik-Firmware-Auto-Updater/raw/master/howto/email-config.png)
|
![](https://github.com/beeyev/Mikrotik-RouterOS-automatic-backup-and-update/raw/master/howto/email-config.png)
|
||||||
|
|
||||||
##### 3. Create scheduled task
|
To check email settings, send a test message by running the following command in terminal:
|
||||||
|
```
|
||||||
|
/tool e-mail send to="yourMail@example.com" subject="backup & update test!" body="It works!";
|
||||||
|
```
|
||||||
|
|
||||||
|
##### 4. Create scheduled task
|
||||||
System -> Scheduler [Add]
|
System -> Scheduler [Add]
|
||||||
Name: Firmware Updater
|
Name: `Backup And Update`
|
||||||
Start Time: 03:00:00
|
Start Time: `03:10:00`
|
||||||
Interval: 1d 00:00:00
|
Interval: `1d 00:00:00`
|
||||||
On Event: /system script run firmware-updater;
|
On Event: `/system script run BackupAndUpdate;`
|
||||||
![](https://github.com/beeyev/Mikrotik-Firmware-Auto-Updater/raw/master/howto/scheduler-task.png)
|
![](https://github.com/beeyev/Mikrotik-RouterOS-automatic-backup-and-update/raw/master/howto/scheduler-task.png)
|
||||||
|
|
||||||
Or you can use this command to create the task:
|
Or you can use this command to create the task:
|
||||||
```
|
```
|
||||||
/system scheduler add name="Firmware Updater" on-event="/system script run firmware-updater;" start-time=03:00:00 interval=1d comment="" disabled=no
|
/system scheduler add name="Firmware Updater" on-event="/system script run BackupAndUpdate;" start-time=03:10:00 interval=1d comment="" disabled=no
|
||||||
```
|
```
|
||||||
|
##### 5. Test the script
|
||||||
|
When everything is done, you need to make sure that the script is working correctly.
|
||||||
|
To do so, open a New Terminal and Log window in your WinBox, then run the script manually by executing this command `/system script run BackupAndUpdate;` in Terminal.
|
||||||
|
You will see the script working process in the log window. If the script finished without errors, check your email, there is a fresh message with backups from your MikroTik waiting for you 🎉
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
|
||||||
|
|
||||||
|
---
|
||||||
|
![](https://visitor-count-badge.herokuapp.com/total.svg?repo_id=beeyev.Mikrotik-RouterOS-automatic-backup-and-update)
|
||||||
|
![](https://visitor-count-badge.herokuapp.com/today.svg?repo_id=beeyev.Mikrotik-RouterOS-automatic-backup-and-update)
|
||||||
|
0
docs/index.html
Normal file
0
docs/index.html
Normal file
@ -1,254 +0,0 @@
|
|||||||
# Script name: firmware-updater
|
|
||||||
|
|
||||||
########## Set variables
|
|
||||||
## Notification e-mail
|
|
||||||
:local emailEnabled true;
|
|
||||||
:local emailAddress "yourmail@example.com";
|
|
||||||
:local sendBackupToEmail true;
|
|
||||||
|
|
||||||
## Backup encryption password, no encryption if no password.
|
|
||||||
:local backupPassword ""
|
|
||||||
## If true, passwords will be included in exported config
|
|
||||||
:local sensetiveDataInConfig false;
|
|
||||||
|
|
||||||
## Update channel. Possible values: stable, long-term
|
|
||||||
:local updateChannel "stable";
|
|
||||||
|
|
||||||
## Install only patch versions of firmware updates.
|
|
||||||
## Means that new update will be installed only if major and minor version numbers are the same as currently installed firmware.
|
|
||||||
## Example: v6.43.6 => major.minor.PATCH
|
|
||||||
:local onlyPatchUpdates false;
|
|
||||||
##########
|
|
||||||
|
|
||||||
## !!!! DO NOT CHANGE ANYTHING BELOW THIS LINE, IF YOU ARE NOT SURE WHAT YOU ARE DOING !!!! ##
|
|
||||||
|
|
||||||
############### vvvvvvvvv GLOBAL FUNCTIONS vvvvvvvvv ###############
|
|
||||||
# Function converts standard mikrotik build versions to the number.
|
|
||||||
# Possible arguments: osVer
|
|
||||||
# Example:
|
|
||||||
# :put [$getOsVerNum osVer=[/system routerboard get current-firmware]];
|
|
||||||
# result will be: 64301, because current-firmware value is: 6.43.1
|
|
||||||
:global getOsVerNum do={
|
|
||||||
:local osVerNum;
|
|
||||||
:local osVerMicroPart;
|
|
||||||
:local zro 0;
|
|
||||||
:local tmp;
|
|
||||||
|
|
||||||
:local dotPos1 [:find $osVer "." 0];
|
|
||||||
|
|
||||||
:if ($dotPos1 > 0) do={
|
|
||||||
|
|
||||||
# AA
|
|
||||||
:set osVerNum [:pick $osVer 0 $dotPos1];
|
|
||||||
|
|
||||||
:local dotPos2 [:find $osVer "." $dotPos1];
|
|
||||||
|
|
||||||
#Taking minor version, everything after first dot
|
|
||||||
:if ([:len $dotPos2] = 0) do={:set tmp [:pick $osVer ($dotPos1+1) [:len $osVer]];}
|
|
||||||
#Taking minor version, everything between first and second dots
|
|
||||||
:if ($dotPos2 > 0) do={:set tmp [:pick $osVer ($dotPos1+1) $dotPos2];}
|
|
||||||
|
|
||||||
# AA 0B
|
|
||||||
:if ([:len $tmp] = 1) do={:set osVerNum "$osVerNum$zro$tmp";}
|
|
||||||
# AA BB
|
|
||||||
:if ([:len $tmp] = 2) do={:set osVerNum "$osVerNum$tmp";}
|
|
||||||
|
|
||||||
:if ($dotPos2 > 0) do={
|
|
||||||
:set tmp [:pick $osVer ($dotPos2+1) [:len $osVer]];
|
|
||||||
# AA BB 0C
|
|
||||||
:if ([:len $tmp] = 1) do={:set osVerNum "$osVerNum$zro$tmp";}
|
|
||||||
# AA BB CC
|
|
||||||
:if ([:len $tmp] = 2) do={:set osVerNum "$osVerNum$tmp";}
|
|
||||||
} else={
|
|
||||||
# AA BB 00
|
|
||||||
:set osVerNum "$osVerNum$zro$zro";
|
|
||||||
}
|
|
||||||
} else={
|
|
||||||
# AA 00 00
|
|
||||||
:set osVerNum "$osVer$zro$zro$zro$zro";
|
|
||||||
}
|
|
||||||
|
|
||||||
:return $osVerNum;
|
|
||||||
}
|
|
||||||
############### ^^^^^^^^^ GLOBAL FUNCTIONS ^^^^^^^^^ ###############
|
|
||||||
|
|
||||||
#Check proper email config
|
|
||||||
:if ($emailEnabled = true and ([:len $emailAddress] = 0 or [:len [/tool e-mail get address]] = 0 or [:len [/tool e-mail get from]] = 0)) do={
|
|
||||||
:log warning ("Email notifications switched off, you need to check your e-mail configuration. Tools -> Email");
|
|
||||||
:set emailEnabled false;
|
|
||||||
}
|
|
||||||
|
|
||||||
:log info ("Firmware checking and upgrade process has started");
|
|
||||||
#load global var
|
|
||||||
:global beePatchUpdateInapplicable;
|
|
||||||
## Set global var to the local one, then delete it form the global environment
|
|
||||||
:global beeGlobalUpdateStep;
|
|
||||||
:local updateStep $beeGlobalUpdateStep;
|
|
||||||
/system script environment remove beeGlobalUpdateStep;
|
|
||||||
|
|
||||||
:log info ("Update step: $updateStep");
|
|
||||||
|
|
||||||
## if it is a very first step
|
|
||||||
:if ([:len $updateStep] = 0) do={
|
|
||||||
|
|
||||||
# Convert current version to numeric (6.43.8 => 64308)
|
|
||||||
:local osVerCurrent [/system package update get installed-version];
|
|
||||||
:local osVerCurrentNum [$getOsVerNum osVer=$osVerCurrent];
|
|
||||||
|
|
||||||
## We need this part to keep compatibility with firmware older than 6.43.7
|
|
||||||
:if ($osVerCurrentNum < 64307 and [:len [:find "bugfix current release-candidate" $updateChannel 0]] = 0) do={
|
|
||||||
:if ($updateChannel = "stable") do={
|
|
||||||
:set updateChannel "current";
|
|
||||||
} else={
|
|
||||||
:if ($updateChannel = "long-term") do={
|
|
||||||
:set updateChannel "bugfix";
|
|
||||||
} else={
|
|
||||||
:if ($updateChannel = "testing") do={
|
|
||||||
:set updateChannel "release-candidate";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log info ("Checking for new firmware version. Current version is: $osVerCurrent");
|
|
||||||
/system package update set channel=$updateChannel;
|
|
||||||
/system package update check-for-updates;
|
|
||||||
:delay 5s;
|
|
||||||
|
|
||||||
# Getting info about new available firmware version
|
|
||||||
:local osVerNew [/system package update get latest-version];
|
|
||||||
:delay 5s;
|
|
||||||
|
|
||||||
# If there is a problem getting information about available firmware from server
|
|
||||||
:if ([:len $osVerNew] = 0) do={
|
|
||||||
:log info ("There is a problem getting information about available firmware from server. No internet connection \?");
|
|
||||||
:error "Error during firmware-updater script execution, please see log.";
|
|
||||||
}
|
|
||||||
|
|
||||||
# Convert new version to numeric (6.43.8 => 64308)
|
|
||||||
:local osVerNewNum [$getOsVerNum osVer=$osVerNew];
|
|
||||||
# Remove function getOsVerNum from global environment to keep it fresh and clean.
|
|
||||||
:set getOsVerNum;
|
|
||||||
|
|
||||||
# Compare new and current versions
|
|
||||||
:local isUpdateAvailable false;
|
|
||||||
:if ($osVerNewNum > $osVerCurrentNum) do={
|
|
||||||
:set isUpdateAvailable true;
|
|
||||||
:log info ("New firmware version found: $osVerNew");
|
|
||||||
} else={
|
|
||||||
:log info ("New firmware version not found.")
|
|
||||||
}
|
|
||||||
|
|
||||||
# If only patch updates are allowed
|
|
||||||
:if ($onlyPatchUpdates = true and $isUpdateAvailable = true) do={
|
|
||||||
#Check if Major and Minor builds are the same.
|
|
||||||
:if ([:pick $osVerCurrentNum 0 ([:len $osVerCurrentNum]-2)] = [:pick $osVerNewNum 0 ([:len $osVerNewNum]-2)]) do={
|
|
||||||
:log info ("New patch version of RouterOS firmware is available.");
|
|
||||||
} else={
|
|
||||||
:log info ("New major version of RouterOS firmware is available. You have to update it manually.");
|
|
||||||
:set isUpdateAvailable false;
|
|
||||||
#Send email just once
|
|
||||||
:if ($beePatchUpdateInapplicable != true and $emailEnabled = true) do={
|
|
||||||
:log info ("Email was sent just once.");
|
|
||||||
/tool e-mail send to="$emailAddress" subject="Router: $[/system identity get name], new major version of RouterOS firmware is available: v$osVerNew" body="New major version of RouterOS firmware is available. You have to update it manually.. \r\n\r\nRouter name: $[/system identity get name]\r\nCurrent RouterOS version: $osVerCurrent; Routerboard firmware: $[/system routerboard get current-firmware]; Update channel: $[/system package update get channel]; \r\nBoard name: $[/system resource get board-name]; Serial number: $[/system routerboard get serial-number]; \r\n\r\n Changelog: https://mikrotik.com/download/changelogs/current-release-tree";
|
|
||||||
:delay 5s;
|
|
||||||
# If notification was sent, we no longer bother with it.
|
|
||||||
:if ([/tool e-mail get last-status] = "succeeded") do={
|
|
||||||
:global beePatchUpdateInapplicable true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#Keep environment clean
|
|
||||||
:if ($isUpdateAvailable = true and $beePatchUpdateInapplicable = true) do={
|
|
||||||
/system script environment remove beePatchUpdateInapplicable;
|
|
||||||
}
|
|
||||||
|
|
||||||
# If we found some updates
|
|
||||||
:if ($isUpdateAvailable = true) do={
|
|
||||||
## New version of RouterOS available, let's upgrade
|
|
||||||
:log info ("Going to update RouterOS firmware from $osVerCurrent to $osVerNew (channel:$updateChannel)");
|
|
||||||
|
|
||||||
:if ($emailEnabled = true) do={
|
|
||||||
:local attachments;
|
|
||||||
|
|
||||||
#M# acking system backups to attach them in email.
|
|
||||||
:if ($sendBackupToEmail = true) do={
|
|
||||||
:log info ("Making system backups.");
|
|
||||||
## date and time in format: 2018aug06-215139
|
|
||||||
:local dtame ([:pick [/system clock get date] 7 11] . [:pick [/system clock get date] 0 3] . [:pick [/system clock get date] 4 6] . "-" . [:pick [/system clock get time] 0 2] . [:pick [/system clock get time] 3 5] . [:pick [/system clock get time] 6 8]);
|
|
||||||
## unified backup file name without extension
|
|
||||||
:local bname "$[/system identity get name].$[/system routerboard get serial-number].v$[/system package update get installed-version]_$dtame";
|
|
||||||
:local sysFileBackup "$bname.backup";
|
|
||||||
:local configFileBackup "$bname.rsc";
|
|
||||||
:set attachments {$sysFileBackup;$configFileBackup}
|
|
||||||
|
|
||||||
## Make system backup
|
|
||||||
:if ([:len $backupPassword] = 0) do={
|
|
||||||
/system backup save dont-encrypt=yes name=$bname;
|
|
||||||
} else={
|
|
||||||
/system backup save password=$backupPassword name=$bname;
|
|
||||||
}
|
|
||||||
|
|
||||||
## Export config file
|
|
||||||
:if ($sensetiveDataInConfig = true) do={
|
|
||||||
/export compact file=$bname;
|
|
||||||
} else={
|
|
||||||
/export compact hide-sensitive file=$bname;
|
|
||||||
}
|
|
||||||
:delay 5s;
|
|
||||||
}
|
|
||||||
|
|
||||||
:log info ("Sending email");
|
|
||||||
/tool e-mail send to=$emailAddress subject="Upgrade router: $[/system identity get name] FW has been started" body="Upgrading RouterOS on router $[/system identity get name] from $osVerCurrent to $osVerNew \r\nYou will recieve final report with detailed information when upgrade process is finished. If you have not got second email in next 5 minutes, then probably something went wrong." file=$attachments;
|
|
||||||
:delay 5s;
|
|
||||||
|
|
||||||
## Remove backups which we have already sent
|
|
||||||
:if ($sendBackupToEmail = true && [/tool e-mail get last-status] = "succeeded") do={
|
|
||||||
/file remove $attachments;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
## Set scheduled task to upgrade routerboard firmware on the next boot, task will be deleted when upgrade is done. (That is why you should keep original script name)
|
|
||||||
/system schedule add name=BEE-UPGRADE-NEXT-BOOT on-event="/system scheduler remove BEE-UPGRADE-NEXT-BOOT; :global beeGlobalUpdateStep \"routerboardUpgrade\"; :delay 15s; /system script run firmware-updater;" start-time=startup interval=0;
|
|
||||||
|
|
||||||
## command is reincarnation of the "upgrade" command - doing exactly the same but under a different name
|
|
||||||
/system package update install;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
## Second step (after first reboot) routerboard firmware upgrade
|
|
||||||
:if ( $updateStep = "routerboardUpgrade") do={
|
|
||||||
|
|
||||||
## RouterOS latest, let's check for updated firmware
|
|
||||||
|
|
||||||
:if ( [/system routerboard get current-firmware] != [/system routerboard get upgrade-firmware]) do={
|
|
||||||
## New version of firmware available, let's upgrade
|
|
||||||
:log info ("Upgrading firmware on router $[/system identity get name], board name: $[/system resource get board-name], serial number: $[/system routerboard get serial-number] | From $[/system routerboard get current-firmware] to $[/system routerboard get upgrade-firmware]");
|
|
||||||
|
|
||||||
## Start the upgrading process
|
|
||||||
/system routerboard upgrade;
|
|
||||||
|
|
||||||
## Wait until the upgrade is finished
|
|
||||||
:delay 5s;
|
|
||||||
|
|
||||||
## Set scheduled task to send final report on the next boot, task will be deleted when is is done. (That is why you should keep original script name)
|
|
||||||
/system schedule add name=BEE-FINAL-REPORT-NEXT-BOOT on-event="/system scheduler remove BEE-FINAL-REPORT-NEXT-BOOT; :global beeGlobalUpdateStep \"finalReport\"; :delay 15s; /system script run firmware-updater;" start-time=startup interval=0;
|
|
||||||
|
|
||||||
## Reboot system to boot with new firmware
|
|
||||||
/system reboot;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
## Last step (after second reboot) sending final report
|
|
||||||
:if ( $updateStep = "finalReport") do={
|
|
||||||
:log info "Upgrading RouterOS and routerboard firmware finished. Current RouterOS version: $[/system package update get installed-version], routerboard firmware: $[/system routerboard get current-firmware].";
|
|
||||||
|
|
||||||
:if ($emailEnabled = true) do={
|
|
||||||
/tool e-mail send to="$emailAddress" subject="Router: $[/system identity get name] has been upgraded with new FW!" body="Upgrading RouterOS and routerboard firmware finished. \r\n\r\nRouter name: $[/system identity get name]\r\nCurrent RouterOS version: $[/system package update get installed-version]; Routerboard firmware: $[/system routerboard get current-firmware]; Update channel: $[/system package update get channel]; \r\nBoard name: $[/system resource get board-name]; Serial number: $[/system routerboard get serial-number]; \r\n\r\n Changelog: https://mikrotik.com/download/changelogs/current-release-tree";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
:log info ("Firmware updater has finished it's job");
|
|
Binary file not shown.
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 20 KiB |
Binary file not shown.
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 25 KiB |
Loading…
Reference in New Issue
Block a user