Skip to content

Commit b149ed5

Browse files
authored
v23.11.25 (#46)
v23.11.25 (#46)
1 parent 4fde387 commit b149ed5

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

BackupAndUpdate.rsc

Lines changed: 49 additions & 6 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: 23.11.11
6+
# Version: 23.11.25
77
# Created: 07/08/2018
8-
# Updated: 11/11/2023
8+
# Updated: 25/11/2023
99
# Author: Alexander Tebiev
1010
# Website: https://github.com/beeyev
1111
# You can contact me by e-mail at tebiev@mail.com
@@ -50,6 +50,12 @@
5050
## Script will send information if new version is greater than just patch.
5151
:local installOnlyPatchUpdates false;
5252

53+
## If true, device public IP address information will be included into the email message
54+
:local detectPublicIpAddress true;
55+
56+
## Allow anonymous statistics collection. (script mode, device model, OS version)
57+
:local allowAnonymousStatisticsCollection true;
58+
5359
##------------------------------------------------------------------------------------------##
5460
# !!!! DO NOT CHANGE ANYTHING BELOW THIS LINE, IF YOU ARE NOT SURE WHAT YOU ARE DOING !!!! #
5561
##------------------------------------------------------------------------------------------##
@@ -192,7 +198,7 @@ if ([:len [/system identity get name]] = 0 or [/system identity get name] = "Mik
192198
:global buGlobalVarUpdateStep;
193199
############### ^^^^^^^^^ GLOBALS ^^^^^^^^^ ###############
194200

195-
:local scriptVersion "23.11.11";
201+
:local scriptVersion "23.11.25";
196202

197203
# Current time `hh-mm-ss`
198204
:local currentTime ([:pick [/system clock get time] 0 2] . "-" . [:pick [/system clock get time] 3 5] . "-" . [:pick [/system clock get time] 6 8]);
@@ -253,12 +259,49 @@ if ([:len [/system identity get name]] = 0 or [/system identity get name] = "Mik
253259
:local mailAttachments [:toarray ""];
254260

255261

262+
:local ipAddressDetectServiceDefault "https://ipv4.mikrotik.ovh/"
263+
:local ipAddressDetectServiceFallback "https://api.ipify.org/"
264+
:local publicIpAddress "not detected";
265+
:local telemetryDataQuery "";
266+
256267
:local updateStep $buGlobalVarUpdateStep;
257268
:do {/system script environment remove buGlobalVarUpdateStep;} on-error={}
258269
:if ([:len $updateStep] = 0) do={
259270
:set updateStep 1;
260271
}
261272

273+
## IP address detection & anonymous statistics collection
274+
:if ($updateStep = 1 or $updateStep = 3) do={
275+
:if ($updateStep = 3) do={
276+
:log info ("$SMP Waiting for one minute before continuing to the final step.");
277+
:delay 1m;
278+
}
279+
280+
:if ($detectPublicIpAddress = true or $allowAnonymousStatisticsCollection = true) do={
281+
:if ($allowAnonymousStatisticsCollection = true) do={
282+
:set telemetryDataQuery ("\?mode=" . $scriptMode . "&osver=" . $deviceOsVerInst . "&model=" . $deviceRbModel);
283+
}
284+
285+
:do {:set publicIpAddress ([/tool fetch http-method="get" url=($ipAddressDetectServiceDefault . $telemetryDataQuery) output=user as-value]->"data");} on-error={
286+
287+
:if ($detectPublicIpAddress = true) do={
288+
:log warning "$SMP Could not detect public IP address using default detection service."
289+
:log warning "$SMP Trying to detect public ip using fallback detection service."
290+
291+
:do {:set publicIpAddress ([/tool fetch http-method="get" url=$ipAddressDetectServiceFallback output=user as-value]->"data");} on-error={
292+
:log warning "$SMP Could not detect public IP address using fallback detection service."
293+
}
294+
}
295+
}
296+
297+
:if ($detectPublicIpAddress = true) do={
298+
# Always truncate the string for safety measures
299+
:set publicIpAddress ([:pick $publicIpAddress 0 15])
300+
:set mailBodyDeviceInfo ($mailBodyDeviceInfo . "\r\nPublic IP address: " . $publicIpAddress);
301+
}
302+
}
303+
}
304+
262305

263306
## STEP ONE: Creating backups, checking for new RouterOs version and sending email with backups,
264307
## Steps 2 and 3 are fired only if script is set to automatically update device and if a new RouterOs version is available.
@@ -330,7 +373,7 @@ if ([:len [/system identity get name]] = 0 or [/system identity get name] = "Mik
330373
if ($isOsNeedsToBeUpdated = true) do={
331374
:log info ("$SMP New RouterOS is going to be installed! v.$deviceOsVerInst -> v.$deviceOsVerAvail");
332375
:set mailSubject ($mailSubject . " New RouterOS is going to be installed! v.$deviceOsVerInst -> v.$deviceOsVerAvail.");
333-
:set mailBody ($mailBody . "Your Mikrotik will be updated to the new RouterOS version from v.$deviceOsVerInst to v.$deviceOsVerAvail (Update channel: $updateChannel) \r\nFinal report with the detailed information will be sent when update process is completed. \r\nIf you have not received second email in the next 10 minutes, then probably something went wrong. (Check your device logs)");
376+
:set mailBody ($mailBody . "Your Mikrotik will be updated to the new RouterOS version from v.$deviceOsVerInst to v.$deviceOsVerAvail (Update channel: $updateChannel) \r\nA final report with detailed information will be sent once the update process is completed. \r\nIf you do not receive a second email within the next 10 minutes, there may be an issue. Please check your device logs for further information.");
334377
#!! There is more code connected to this part and first step at the end of the script.
335378
}
336379

@@ -386,12 +429,12 @@ if ([:len [/system identity get name]] = 0 or [/system identity get name] = "Mik
386429

387430
## STEP THREE: Last step (after second reboot) sending final report
388431
## Steps 2 and 3 are fired only if script is set to automatically update device and if new RouterOs is available.
432+
## This step is executed after some delay
389433
:if ($updateStep = 3) do={
390434
:log info ("$SMP Performing the third step.");
391435
:log info "Bkp&Upd: RouterOS and routerboard upgrade process was completed. New RouterOS version: v.$deviceOsVerInst, routerboard firmware: v.$deviceRbCurrentFw.";
392436
## Small delay in case mikrotik needs some time to initialize connections
393-
:log info "$SMP The final email with report and backups of upgraded system will be sent in a minute.";
394-
:delay 1m;
437+
:log info "$SMP Sending the final email with report and backups.";
395438
:set mailSubject ($mailSubject . " RouterOS Upgrade is completed, new version: v.$deviceOsVerInst!");
396439
:set mailBody "RouterOS and routerboard upgrade process was completed. \r\nNew RouterOS version: v.$deviceOsVerInst, routerboard firmware: v.$deviceRbCurrentFw. \r\n$changelogUrl \r\n\r\nBackups of the upgraded system are in the attachment of this email. $mailBodyDeviceInfo $mailBodyCopyright";
397440
:set mailAttachments [$buGlobalFuncCreateBackups backupName=$backupNameAfterUpd backupPassword=$backupPassword sensitiveDataInConfig=$sensitiveDataInConfig];

0 commit comments

Comments
 (0)