Skip to content

Commit 2b4f233

Browse files
authored
Merge pull request #77 from beeyev/ver-26.02.22
2 parents d1cd2df + 795932f commit 2b4f233

2 files changed

Lines changed: 34 additions & 42 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
ignore/
1+
.ignore/
22
.idea/
33
.vscode/

BackupAndUpdate.rsc

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
# SCRIPT INFORMATION
44
#
55
# Script: Mikrotik RouterOS automatic backup & update
6-
# Version: 25.04.15
6+
# Version: 26.02.22
77
# Created: 07/08/2018
8-
# Updated: 15/04/2025
8+
# Updated: 22/02/2026
99
# Author: Alexander Tebiev
1010
# Website: https://github.com/beeyev
1111
# You can contact me by e-mail at tebiev@mail.com
@@ -16,7 +16,7 @@
1616
# --- MODIFY THIS SECTION AS NEEDED ---
1717
# Notification e-mail
1818
# (Make sure you have configured Email settings in Tools -> Email)
19-
:local emailAddress "yourmail@example.com";
19+
:local emailAddress "yourmail@example.com"
2020

2121
# Script mode, possible values: backup, osupdate, osnotify.
2222
# backup - Only backup will be performed. (default value, if none provided)
@@ -56,7 +56,7 @@
5656

5757
# !!! DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU’RE DOING !!!
5858

59-
:local scriptVersion "25.04.15"
59+
:local scriptVersion "26.02.22"
6060

6161
# default and fallback public IP detection services
6262
:local ipAddressDetectServiceDefault "https://ipv4.mikrotik.ovh/"
@@ -112,26 +112,18 @@
112112
:local ver1 $1
113113
:local ver2 $2
114114

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] }
120120

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] }
126125

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])
135127
}
136128

137129
# Creates backups and returns array of names
@@ -315,11 +307,11 @@
315307
:set emailServer [/tool e-mail get address]
316308
}
317309
: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.")
319311
:error $exitErrorMessage
320312
}
321313
: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.")
323315
:error $exitErrorMessage
324316
}
325317

@@ -376,15 +368,15 @@
376368
:local deviceBoardName [/system resource get board-name]
377369

378370
## Check if it's a cloud hosted router
379-
:local isCloudHostedRouter false;
371+
:local isCloudHostedRouter false
380372
:if ([:pick $deviceBoardName 0 3] = "CHR" or [:pick $deviceBoardName 0 3] = "x86") do={
381-
:set isCloudHostedRouter true;
382-
};
373+
:set isCloudHostedRouter true
374+
}
383375

384-
:local deviceIdentityName [/system identity get name];
376+
:local deviceIdentityName [/system identity get name]
385377
:local deviceIdentityNameShort [:pick $deviceIdentityName 0 18]
386378

387-
:local deviceRbModel "CloudHostedRouter";
379+
:local deviceRbModel "CloudHostedRouter"
388380
:local deviceRbSerialNumber "--"
389381
:local deviceRbCurrentFw "--"
390382
:local deviceRbUpgradeFw "--"
@@ -394,7 +386,7 @@
394386
:set deviceRbSerialNumber [/system routerboard get serial-number]
395387
:set deviceRbCurrentFw [/system routerboard get current-firmware]
396388
:set deviceRbUpgradeFw [/system routerboard get upgrade-firmware]
397-
};
389+
}
398390

399391
:local runningOsChannel [$FuncGetRunningOsChannel]
400392
:local runningOsVersion [$FuncGetRunningOsVersion]
@@ -481,13 +473,13 @@
481473

482474
# Checking for new version
483475
: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`")
485477
/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`")
487479
/system package update check-for-updates
488480

489481
# Wait to allow the system to check for updates
490-
:delay 5s;
482+
:delay 5s
491483

492484
:local packageUpdateStatus "undefined"
493485

@@ -557,7 +549,7 @@
557549
}
558550

559551
:do {
560-
:set mailAttachments [$FuncCreateBackups $backupName $backupPassword $sensitiveDataInConfig];
552+
:set mailAttachments [$FuncCreateBackups $backupName $backupPassword $sensitiveDataInConfig]
561553

562554
:set mailPtSubjectBackup "Backup created"
563555
:set mailPtBodyBackup "System backups have been successfully created and attached to this email."
@@ -596,8 +588,8 @@
596588

597589
:if ([:len $mailAttachments] > 0) do={
598590
: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
601593
}
602594

603595
:if ($isOsNeedsToBeUpdated = true) do={
@@ -640,12 +632,12 @@
640632
/system routerboard upgrade
641633
:delay 2s
642634

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!"
644636

645637
## Set task to send final report on the next boot
646638
/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
647639

648-
/system reboot;
640+
/system reboot
649641
}
650642

651643
## STEP 3: Final report (after second reboot, with delay).
@@ -670,7 +662,7 @@
670662
:set mailStep3Body ($mailStep3Body . "\nNew RouterOS version: v.$targetOsVersion, routerboard firmware: v.$deviceRbCurrentFw")
671663
:set mailStep3Body ($mailStep3Body . "\n$changelogUrl\nBackups of the upgraded system are in the attachment of this email.\n\n$mailBodyDeviceInfo\n\n$mailBodyCopyright")
672664

673-
:set mailAttachments [$FuncCreateBackups $backupNameAfterUpdate $backupPassword $sensitiveDataInConfig];
665+
:set mailAttachments [$FuncCreateBackups $backupNameAfterUpdate $backupPassword $sensitiveDataInConfig]
674666
} else={
675667
:log error "$SMP Failed to verify new RouterOS version: target: `$targetOsVersion`, current: `$runningOsVersion`"
676668
:set mailStep3Subject ($mailStep3Subject . " - Update failed")
@@ -682,8 +674,8 @@
682674

683675
:if ([:len $mailAttachments] > 0) do={
684676
: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
687679
}
688680

689681
:log info "$SMP Final report email sent successfully, and the script has finished."

0 commit comments

Comments
 (0)