diff --git a/.gitignore b/.gitignore index 8951906..1ea45ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -.ignore/ +ignore/ .idea/ .vscode/ diff --git a/BackupAndUpdate.rsc b/BackupAndUpdate.rsc index 48d74b1..8d3d5d3 100644 --- a/BackupAndUpdate.rsc +++ b/BackupAndUpdate.rsc @@ -3,9 +3,9 @@ # SCRIPT INFORMATION # # Script: Mikrotik RouterOS automatic backup & update -# Version: 26.02.22 +# Version: 25.04.15 # Created: 07/08/2018 -# Updated: 22/02/2026 +# Updated: 15/04/2025 # Author: Alexander Tebiev # Website: https://github.com/beeyev # You can contact me by e-mail at tebiev@mail.com @@ -16,7 +16,7 @@ # --- MODIFY THIS SECTION AS NEEDED --- # Notification e-mail # (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. # 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 YOU’RE DOING !!! -:local scriptVersion "26.02.22" +:local scriptVersion "25.04.15" # default and fallback public IP detection services :local ipAddressDetectServiceDefault "https://ipv4.mikrotik.ovh/" @@ -112,18 +112,26 @@ :local ver1 $1 :local ver2 $2 - # Extract "major.minor" prefix from each version by finding the second dot - :local dot1 [:find $ver1 "."] - :if ([:len $dot1] = 0) do={ :return ($ver1 = $ver2) } - :local end1 [:find $ver1 "." ($dot1 + 1)] - :if ([:len $end1] = 0) do={ :set end1 [:len $ver1] } + # Extract the major and minor components from a version + :local extractMajorMinor do={ + :local ver $1 + :local dot1 [:find $ver "."] + :if ($dot1 = -1) do={ :return $ver } - :local dot2 [:find $ver2 "."] - :if ([:len $dot2] = 0) do={ :return ($ver1 = $ver2) } - :local end2 [:find $ver2 "." ($dot2 + 1)] - :if ([:len $end2] = 0) do={ :set end2 [:len $ver2] } + :local major [:pick $ver 0 $dot1] + :local rest [:pick $ver ($dot1 + 1) [:len $ver]] + :local dot2 [:find $rest "."] + :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 @@ -307,11 +315,11 @@ :set emailServer [/tool e-mail get address] } :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 } :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 } @@ -368,15 +376,15 @@ :local deviceBoardName [/system resource get board-name] ## 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={ - :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 deviceRbModel "CloudHostedRouter" +:local deviceRbModel "CloudHostedRouter"; :local deviceRbSerialNumber "--" :local deviceRbCurrentFw "--" :local deviceRbUpgradeFw "--" @@ -386,7 +394,7 @@ :set deviceRbSerialNumber [/system routerboard get serial-number] :set deviceRbCurrentFw [/system routerboard get current-firmware] :set deviceRbUpgradeFw [/system routerboard get upgrade-firmware] -} +}; :local runningOsChannel [$FuncGetRunningOsChannel] :local runningOsVersion [$FuncGetRunningOsVersion] @@ -473,13 +481,13 @@ # Checking for new version :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 - :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 # Wait to allow the system to check for updates - :delay 5s + :delay 5s; :local packageUpdateStatus "undefined" @@ -549,7 +557,7 @@ } :do { - :set mailAttachments [$FuncCreateBackups $backupName $backupPassword $sensitiveDataInConfig] + :set mailAttachments [$FuncCreateBackups $backupName $backupPassword $sensitiveDataInConfig]; :set mailPtSubjectBackup "Backup created" :set mailPtBodyBackup "System backups have been successfully created and attached to this email." @@ -588,8 +596,8 @@ :if ([:len $mailAttachments] > 0) do={ :log info "$SMP Cleaning up backup files from the file system..." - /file remove $mailAttachments - :delay 2s + /file remove $mailAttachments; + :delay 2s; } :if ($isOsNeedsToBeUpdated = true) do={ @@ -632,12 +640,12 @@ /system routerboard upgrade :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 /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). @@ -662,7 +670,7 @@ :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 mailAttachments [$FuncCreateBackups $backupNameAfterUpdate $backupPassword $sensitiveDataInConfig] + :set mailAttachments [$FuncCreateBackups $backupNameAfterUpdate $backupPassword $sensitiveDataInConfig]; } else={ :log error "$SMP Failed to verify new RouterOS version: target: `$targetOsVersion`, current: `$runningOsVersion`" :set mailStep3Subject ($mailStep3Subject . " - Update failed") @@ -674,8 +682,8 @@ :if ([:len $mailAttachments] > 0) do={ :log info "$SMP Cleaning up backup files from the file system..." - /file remove $mailAttachments - :delay 2s + /file remove $mailAttachments; + :delay 2s; } :log info "$SMP Final report email sent successfully, and the script has finished."