|
3 | 3 | # SCRIPT INFORMATION |
4 | 4 | # |
5 | 5 | # Script: Mikrotik RouterOS automatic backup & update |
6 | | -# Version: 25.04.15 |
| 6 | +# Version: 26.02.22 |
7 | 7 | # Created: 07/08/2018 |
8 | | -# Updated: 15/04/2025 |
| 8 | +# Updated: 22/02/2026 |
9 | 9 | # Author: Alexander Tebiev |
10 | 10 | # Website: https://github.com/beeyev |
11 | 11 | # You can contact me by e-mail at tebiev@mail.com |
|
16 | 16 | # --- MODIFY THIS SECTION AS NEEDED --- |
17 | 17 | # Notification e-mail |
18 | 18 | # (Make sure you have configured Email settings in Tools -> Email) |
19 | | -:local emailAddress "yourmail@example.com"; |
| 19 | +:local emailAddress "yourmail@example.com" |
20 | 20 |
|
21 | 21 | # Script mode, possible values: backup, osupdate, osnotify. |
22 | 22 | # backup - Only backup will be performed. (default value, if none provided) |
|
56 | 56 |
|
57 | 57 | # !!! DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU’RE DOING !!! |
58 | 58 |
|
59 | | -:local scriptVersion "25.04.15" |
| 59 | +:local scriptVersion "26.02.22" |
60 | 60 |
|
61 | 61 | # default and fallback public IP detection services |
62 | 62 | :local ipAddressDetectServiceDefault "https://ipv4.mikrotik.ovh/" |
|
112 | 112 | :local ver1 $1 |
113 | 113 | :local ver2 $2 |
114 | 114 |
|
115 | | - # Extract the major and minor components from a version |
116 | | - :local extractMajorMinor do={ |
117 | | - :local ver $1 |
118 | | - :local dot1 [:find $ver "."] |
119 | | - :if ($dot1 = -1) do={ :return $ver } |
| 115 | + # Extract "major.minor" prefix from each version by finding the second dot |
| 116 | + :local dot1 [:find $ver1 "."] |
| 117 | + :if ([:len $dot1] = 0) do={ :return ($ver1 = $ver2) } |
| 118 | + :local end1 [:find $ver1 "." ($dot1 + 1)] |
| 119 | + :if ([:len $end1] = 0) do={ :set end1 [:len $ver1] } |
120 | 120 |
|
121 | | - :local major [:pick $ver 0 $dot1] |
122 | | - :local rest [:pick $ver ($dot1 + 1) [:len $ver]] |
123 | | - :local dot2 [:find $rest "."] |
124 | | - :local minor $rest |
125 | | - :if ($dot2 >= 0) do={ :set minor [:pick $rest 0 $dot2] } |
| 121 | + :local dot2 [:find $ver2 "."] |
| 122 | + :if ([:len $dot2] = 0) do={ :return ($ver1 = $ver2) } |
| 123 | + :local end2 [:find $ver2 "." ($dot2 + 1)] |
| 124 | + :if ([:len $end2] = 0) do={ :set end2 [:len $ver2] } |
126 | 125 |
|
127 | | - :return ($major . "." . $minor) |
128 | | - } |
129 | | - |
130 | | - # Compare the major and minor components of both version strings |
131 | | - :if ([$extractMajorMinor $ver1] = [$extractMajorMinor $ver2]) do={ |
132 | | - :return true |
133 | | - } |
134 | | - :return false |
| 126 | + :return ([:pick $ver1 0 $end1] = [:pick $ver2 0 $end2]) |
135 | 127 | } |
136 | 128 |
|
137 | 129 | # Creates backups and returns array of names |
|
315 | 307 | :set emailServer [/tool e-mail get address] |
316 | 308 | } |
317 | 309 | :if ($emailServer = "0.0.0.0") do={ |
318 | | - :log error ("$SMP Email server address is not correct: `$emailServer`, check `Tools -> Email`. Script stopped."); |
| 310 | + :log error ("$SMP Email server address is not correct: `$emailServer`, check `Tools -> Email`. Script stopped.") |
319 | 311 | :error $exitErrorMessage |
320 | 312 | } |
321 | 313 | :if ([:len $emailFromAddress] < 3) do={ |
322 | | - :log error ("$SMP Email configuration FROM address is not correct: `$emailFromAddress`, check `Tools -> Email`. Script stopped."); |
| 314 | + :log error ("$SMP Email configuration FROM address is not correct: `$emailFromAddress`, check `Tools -> Email`. Script stopped.") |
323 | 315 | :error $exitErrorMessage |
324 | 316 | } |
325 | 317 |
|
|
376 | 368 | :local deviceBoardName [/system resource get board-name] |
377 | 369 |
|
378 | 370 | ## Check if it's a cloud hosted router |
379 | | -:local isCloudHostedRouter false; |
| 371 | +:local isCloudHostedRouter false |
380 | 372 | :if ([:pick $deviceBoardName 0 3] = "CHR" or [:pick $deviceBoardName 0 3] = "x86") do={ |
381 | | - :set isCloudHostedRouter true; |
382 | | -}; |
| 373 | + :set isCloudHostedRouter true |
| 374 | +} |
383 | 375 |
|
384 | | -:local deviceIdentityName [/system identity get name]; |
| 376 | +:local deviceIdentityName [/system identity get name] |
385 | 377 | :local deviceIdentityNameShort [:pick $deviceIdentityName 0 18] |
386 | 378 |
|
387 | | -:local deviceRbModel "CloudHostedRouter"; |
| 379 | +:local deviceRbModel "CloudHostedRouter" |
388 | 380 | :local deviceRbSerialNumber "--" |
389 | 381 | :local deviceRbCurrentFw "--" |
390 | 382 | :local deviceRbUpgradeFw "--" |
|
394 | 386 | :set deviceRbSerialNumber [/system routerboard get serial-number] |
395 | 387 | :set deviceRbCurrentFw [/system routerboard get current-firmware] |
396 | 388 | :set deviceRbUpgradeFw [/system routerboard get upgrade-firmware] |
397 | | -}; |
| 389 | +} |
398 | 390 |
|
399 | 391 | :local runningOsChannel [$FuncGetRunningOsChannel] |
400 | 392 | :local runningOsVersion [$FuncGetRunningOsVersion] |
|
481 | 473 |
|
482 | 474 | # Checking for new version |
483 | 475 | :if ($scriptMode = "osupdate" or $scriptMode = "osnotify") do={ |
484 | | - log info ("$SMP Setting update channel to `$updateChannel`") |
| 476 | + :log info ("$SMP Setting update channel to `$updateChannel`") |
485 | 477 | /system package update set channel=$updateChannel |
486 | | - log info ("$SMP Checking for new RouterOS version. Current installed version is: `$runningOsVersion`") |
| 478 | + :log info ("$SMP Checking for new RouterOS version. Current installed version is: `$runningOsVersion`") |
487 | 479 | /system package update check-for-updates |
488 | 480 |
|
489 | 481 | # Wait to allow the system to check for updates |
490 | | - :delay 5s; |
| 482 | + :delay 5s |
491 | 483 |
|
492 | 484 | :local packageUpdateStatus "undefined" |
493 | 485 |
|
|
557 | 549 | } |
558 | 550 |
|
559 | 551 | :do { |
560 | | - :set mailAttachments [$FuncCreateBackups $backupName $backupPassword $sensitiveDataInConfig]; |
| 552 | + :set mailAttachments [$FuncCreateBackups $backupName $backupPassword $sensitiveDataInConfig] |
561 | 553 |
|
562 | 554 | :set mailPtSubjectBackup "Backup created" |
563 | 555 | :set mailPtBodyBackup "System backups have been successfully created and attached to this email." |
|
596 | 588 |
|
597 | 589 | :if ([:len $mailAttachments] > 0) do={ |
598 | 590 | :log info "$SMP Cleaning up backup files from the file system..." |
599 | | - /file remove $mailAttachments; |
600 | | - :delay 2s; |
| 591 | + /file remove $mailAttachments |
| 592 | + :delay 2s |
601 | 593 | } |
602 | 594 |
|
603 | 595 | :if ($isOsNeedsToBeUpdated = true) do={ |
|
640 | 632 | /system routerboard upgrade |
641 | 633 | :delay 2s |
642 | 634 |
|
643 | | - :log info "$SMP routerboard upgrade process was completed, going to reboot in a moment!"; |
| 635 | + :log info "$SMP routerboard upgrade process was completed, going to reboot in a moment!" |
644 | 636 |
|
645 | 637 | ## Set task to send final report on the next boot |
646 | 638 | /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 |
647 | 639 |
|
648 | | - /system reboot; |
| 640 | + /system reboot |
649 | 641 | } |
650 | 642 |
|
651 | 643 | ## STEP 3: Final report (after second reboot, with delay). |
|
670 | 662 | :set mailStep3Body ($mailStep3Body . "\nNew RouterOS version: v.$targetOsVersion, routerboard firmware: v.$deviceRbCurrentFw") |
671 | 663 | :set mailStep3Body ($mailStep3Body . "\n$changelogUrl\nBackups of the upgraded system are in the attachment of this email.\n\n$mailBodyDeviceInfo\n\n$mailBodyCopyright") |
672 | 664 |
|
673 | | - :set mailAttachments [$FuncCreateBackups $backupNameAfterUpdate $backupPassword $sensitiveDataInConfig]; |
| 665 | + :set mailAttachments [$FuncCreateBackups $backupNameAfterUpdate $backupPassword $sensitiveDataInConfig] |
674 | 666 | } else={ |
675 | 667 | :log error "$SMP Failed to verify new RouterOS version: target: `$targetOsVersion`, current: `$runningOsVersion`" |
676 | 668 | :set mailStep3Subject ($mailStep3Subject . " - Update failed") |
|
682 | 674 |
|
683 | 675 | :if ([:len $mailAttachments] > 0) do={ |
684 | 676 | :log info "$SMP Cleaning up backup files from the file system..." |
685 | | - /file remove $mailAttachments; |
686 | | - :delay 2s; |
| 677 | + /file remove $mailAttachments |
| 678 | + :delay 2s |
687 | 679 | } |
688 | 680 |
|
689 | 681 | :log info "$SMP Final report email sent successfully, and the script has finished." |
|
0 commit comments