Compare commits

..

No commits in common. "2b4f233d5992f84104b32533c3ea8ae6d7c6dab5" and "d1cd2dff3d4d52471d619b5c62de423630691c0f" have entirely different histories.

2 changed files with 42 additions and 34 deletions

2
.gitignore vendored
View file

@ -1,3 +1,3 @@
.ignore/ ignore/
.idea/ .idea/
.vscode/ .vscode/

View file

@ -3,9 +3,9 @@
# SCRIPT INFORMATION # SCRIPT INFORMATION
# #
# Script: Mikrotik RouterOS automatic backup & update # Script: Mikrotik RouterOS automatic backup & update
# Version: 26.02.22 # Version: 25.04.15
# Created: 07/08/2018 # Created: 07/08/2018
# Updated: 22/02/2026 # Updated: 15/04/2025
# Author: Alexander Tebiev # Author: Alexander Tebiev
# Website: https://github.com/beeyev # Website: https://github.com/beeyev
# You can contact me by e-mail at tebiev@mail.com # You can contact me by e-mail at tebiev@mail.com
@ -16,7 +16,7 @@
# --- MODIFY THIS SECTION AS NEEDED --- # --- MODIFY THIS SECTION AS NEEDED ---
# Notification e-mail # Notification e-mail
# (Make sure you have configured Email settings in Tools -> Email) # (Make sure you have configured Email settings in Tools -> Email)
:local emailAddress "yourmail@example.com" :local emailAddress "yourmail@example.com";
# Script mode, possible values: backup, osupdate, osnotify. # Script mode, possible values: backup, osupdate, osnotify.
# backup - Only backup will be performed. (default value, if none provided) # backup - Only backup will be performed. (default value, if none provided)
@ -56,7 +56,7 @@
# !!! DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOURE DOING !!! # !!! DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOURE DOING !!!
:local scriptVersion "26.02.22" :local scriptVersion "25.04.15"
# default and fallback public IP detection services # default and fallback public IP detection services
:local ipAddressDetectServiceDefault "https://ipv4.mikrotik.ovh/" :local ipAddressDetectServiceDefault "https://ipv4.mikrotik.ovh/"
@ -112,18 +112,26 @@
:local ver1 $1 :local ver1 $1
:local ver2 $2 :local ver2 $2
# Extract "major.minor" prefix from each version by finding the second dot # Extract the major and minor components from a version
:local dot1 [:find $ver1 "."] :local extractMajorMinor do={
:if ([:len $dot1] = 0) do={ :return ($ver1 = $ver2) } :local ver $1
:local end1 [:find $ver1 "." ($dot1 + 1)] :local dot1 [:find $ver "."]
:if ([:len $end1] = 0) do={ :set end1 [:len $ver1] } :if ($dot1 = -1) do={ :return $ver }
:local dot2 [:find $ver2 "."] :local major [:pick $ver 0 $dot1]
:if ([:len $dot2] = 0) do={ :return ($ver1 = $ver2) } :local rest [:pick $ver ($dot1 + 1) [:len $ver]]
:local end2 [:find $ver2 "." ($dot2 + 1)] :local dot2 [:find $rest "."]
:if ([:len $end2] = 0) do={ :set end2 [:len $ver2] } :local minor $rest
:if ($dot2 >= 0) do={ :set minor [:pick $rest 0 $dot2] }
:return ([:pick $ver1 0 $end1] = [:pick $ver2 0 $end2]) :return ($major . "." . $minor)
}
# Compare the major and minor components of both version strings
:if ([$extractMajorMinor $ver1] = [$extractMajorMinor $ver2]) do={
:return true
}
:return false
} }
# Creates backups and returns array of names # Creates backups and returns array of names
@ -307,11 +315,11 @@
:set emailServer [/tool e-mail get address] :set emailServer [/tool e-mail get address]
} }
:if ($emailServer = "0.0.0.0") do={ :if ($emailServer = "0.0.0.0") do={
:log error ("$SMP Email server address is not correct: `$emailServer`, check `Tools -> Email`. Script stopped.") :log error ("$SMP Email server address is not correct: `$emailServer`, check `Tools -> Email`. Script stopped.");
:error $exitErrorMessage :error $exitErrorMessage
} }
:if ([:len $emailFromAddress] < 3) do={ :if ([:len $emailFromAddress] < 3) do={
:log error ("$SMP Email configuration FROM address is not correct: `$emailFromAddress`, check `Tools -> Email`. Script stopped.") :log error ("$SMP Email configuration FROM address is not correct: `$emailFromAddress`, check `Tools -> Email`. Script stopped.");
:error $exitErrorMessage :error $exitErrorMessage
} }
@ -368,15 +376,15 @@
:local deviceBoardName [/system resource get board-name] :local deviceBoardName [/system resource get board-name]
## Check if it's a cloud hosted router ## Check if it's a cloud hosted router
:local isCloudHostedRouter false :local isCloudHostedRouter false;
:if ([:pick $deviceBoardName 0 3] = "CHR" or [:pick $deviceBoardName 0 3] = "x86") do={ :if ([:pick $deviceBoardName 0 3] = "CHR" or [:pick $deviceBoardName 0 3] = "x86") do={
:set isCloudHostedRouter true :set isCloudHostedRouter true;
} };
:local deviceIdentityName [/system identity get name] :local deviceIdentityName [/system identity get name];
:local deviceIdentityNameShort [:pick $deviceIdentityName 0 18] :local deviceIdentityNameShort [:pick $deviceIdentityName 0 18]
:local deviceRbModel "CloudHostedRouter" :local deviceRbModel "CloudHostedRouter";
:local deviceRbSerialNumber "--" :local deviceRbSerialNumber "--"
:local deviceRbCurrentFw "--" :local deviceRbCurrentFw "--"
:local deviceRbUpgradeFw "--" :local deviceRbUpgradeFw "--"
@ -386,7 +394,7 @@
:set deviceRbSerialNumber [/system routerboard get serial-number] :set deviceRbSerialNumber [/system routerboard get serial-number]
:set deviceRbCurrentFw [/system routerboard get current-firmware] :set deviceRbCurrentFw [/system routerboard get current-firmware]
:set deviceRbUpgradeFw [/system routerboard get upgrade-firmware] :set deviceRbUpgradeFw [/system routerboard get upgrade-firmware]
} };
:local runningOsChannel [$FuncGetRunningOsChannel] :local runningOsChannel [$FuncGetRunningOsChannel]
:local runningOsVersion [$FuncGetRunningOsVersion] :local runningOsVersion [$FuncGetRunningOsVersion]
@ -473,13 +481,13 @@
# Checking for new version # Checking for new version
:if ($scriptMode = "osupdate" or $scriptMode = "osnotify") do={ :if ($scriptMode = "osupdate" or $scriptMode = "osnotify") do={
:log info ("$SMP Setting update channel to `$updateChannel`") log info ("$SMP Setting update channel to `$updateChannel`")
/system package update set channel=$updateChannel /system package update set channel=$updateChannel
:log info ("$SMP Checking for new RouterOS version. Current installed version is: `$runningOsVersion`") log info ("$SMP Checking for new RouterOS version. Current installed version is: `$runningOsVersion`")
/system package update check-for-updates /system package update check-for-updates
# Wait to allow the system to check for updates # Wait to allow the system to check for updates
:delay 5s :delay 5s;
:local packageUpdateStatus "undefined" :local packageUpdateStatus "undefined"
@ -549,7 +557,7 @@
} }
:do { :do {
:set mailAttachments [$FuncCreateBackups $backupName $backupPassword $sensitiveDataInConfig] :set mailAttachments [$FuncCreateBackups $backupName $backupPassword $sensitiveDataInConfig];
:set mailPtSubjectBackup "Backup created" :set mailPtSubjectBackup "Backup created"
:set mailPtBodyBackup "System backups have been successfully created and attached to this email." :set mailPtBodyBackup "System backups have been successfully created and attached to this email."
@ -588,8 +596,8 @@
:if ([:len $mailAttachments] > 0) do={ :if ([:len $mailAttachments] > 0) do={
:log info "$SMP Cleaning up backup files from the file system..." :log info "$SMP Cleaning up backup files from the file system..."
/file remove $mailAttachments /file remove $mailAttachments;
:delay 2s :delay 2s;
} }
:if ($isOsNeedsToBeUpdated = true) do={ :if ($isOsNeedsToBeUpdated = true) do={
@ -632,12 +640,12 @@
/system routerboard upgrade /system routerboard upgrade
:delay 2s :delay 2s
:log info "$SMP routerboard upgrade process was completed, going to reboot in a moment!" :log info "$SMP routerboard upgrade process was completed, going to reboot in a moment!";
## Set task to send final report on the next boot ## Set task to send final report on the next boot
/system scheduler add name=BKPUPD-NEXT-BOOT-TASK on-event=":delay 5s; /system scheduler remove BKPUPD-NEXT-BOOT-TASK; :global buGlobalVarScriptStep 3; :global buGlobalVarTargetOsVersion \"$buGlobalVarTargetOsVersion\"; :delay 10s; /system script run BackupAndUpdate;" start-time=startup interval=0 /system scheduler add name=BKPUPD-NEXT-BOOT-TASK on-event=":delay 5s; /system scheduler remove BKPUPD-NEXT-BOOT-TASK; :global buGlobalVarScriptStep 3; :global buGlobalVarTargetOsVersion \"$buGlobalVarTargetOsVersion\"; :delay 10s; /system script run BackupAndUpdate;" start-time=startup interval=0
/system reboot /system reboot;
} }
## STEP 3: Final report (after second reboot, with delay). ## STEP 3: Final report (after second reboot, with delay).
@ -662,7 +670,7 @@
:set mailStep3Body ($mailStep3Body . "\nNew RouterOS version: v.$targetOsVersion, routerboard firmware: v.$deviceRbCurrentFw") :set mailStep3Body ($mailStep3Body . "\nNew RouterOS version: v.$targetOsVersion, routerboard firmware: v.$deviceRbCurrentFw")
:set mailStep3Body ($mailStep3Body . "\n$changelogUrl\nBackups of the upgraded system are in the attachment of this email.\n\n$mailBodyDeviceInfo\n\n$mailBodyCopyright") :set mailStep3Body ($mailStep3Body . "\n$changelogUrl\nBackups of the upgraded system are in the attachment of this email.\n\n$mailBodyDeviceInfo\n\n$mailBodyCopyright")
:set mailAttachments [$FuncCreateBackups $backupNameAfterUpdate $backupPassword $sensitiveDataInConfig] :set mailAttachments [$FuncCreateBackups $backupNameAfterUpdate $backupPassword $sensitiveDataInConfig];
} else={ } else={
:log error "$SMP Failed to verify new RouterOS version: target: `$targetOsVersion`, current: `$runningOsVersion`" :log error "$SMP Failed to verify new RouterOS version: target: `$targetOsVersion`, current: `$runningOsVersion`"
:set mailStep3Subject ($mailStep3Subject . " - Update failed") :set mailStep3Subject ($mailStep3Subject . " - Update failed")
@ -674,8 +682,8 @@
:if ([:len $mailAttachments] > 0) do={ :if ([:len $mailAttachments] > 0) do={
:log info "$SMP Cleaning up backup files from the file system..." :log info "$SMP Cleaning up backup files from the file system..."
/file remove $mailAttachments /file remove $mailAttachments;
:delay 2s :delay 2s;
} }
:log info "$SMP Final report email sent successfully, and the script has finished." :log info "$SMP Final report email sent successfully, and the script has finished."