backup-cloud: move code into function

This commit is contained in:
Christian Hesse 2024-03-04 13:48:00 +01:00
parent efc2e75f01
commit 33d129496c

View file

@ -9,10 +9,12 @@
# upload backup to MikroTik cloud # upload backup to MikroTik cloud
# https://git.eworm.de/cgit/routeros-scripts/about/doc/backup-cloud.md # https://git.eworm.de/cgit/routeros-scripts/about/doc/backup-cloud.md
:local 0 [ :jobname ];
:global GlobalFunctionsReady; :global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; } :while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
:local Main do={
:local ScriptName [ :tostr $1 ];
:global BackupPassword; :global BackupPassword;
:global BackupRandomDelay; :global BackupRandomDelay;
:global Identity; :global Identity;
@ -28,10 +30,10 @@
:global SymbolForNotification; :global SymbolForNotification;
:global WaitFullyConnected; :global WaitFullyConnected;
$ScriptLock $0; $ScriptLock $ScriptName;
$WaitFullyConnected; $WaitFullyConnected;
:if ([ $ScriptFromTerminal $0 ] = false && $BackupRandomDelay > 0) do={ :if ([ $ScriptFromTerminal $ScriptName ] = false && $BackupRandomDelay > 0) do={
$RandomDelay $BackupRandomDelay; $RandomDelay $BackupRandomDelay;
} }
@ -48,7 +50,7 @@ $WaitFullyConnected;
} }
:local Cloud [ /system/backup/cloud/get ([ find ]->0) ]; :local Cloud [ /system/backup/cloud/get ([ find ]->0) ];
$SendNotification2 ({ origin=$0; \ $SendNotification2 ({ origin=$ScriptName; \
subject=([ $SymbolForNotification "floppy-disk,cloud" ] . "Cloud backup"); \ subject=([ $SymbolForNotification "floppy-disk,cloud" ] . "Cloud backup"); \
message=("Uploaded backup for " . $Identity . " to cloud.\n\n" . \ message=("Uploaded backup for " . $Identity . " to cloud.\n\n" . \
[ $DeviceInfo ] . "\n\n" . \ [ $DeviceInfo ] . "\n\n" . \
@ -56,8 +58,11 @@ $WaitFullyConnected;
[ $FormatLine "Size" ([ $HumanReadableNum ($Cloud->"size") 1024 ] . "iB") ] . "\n" . \ [ $FormatLine "Size" ([ $HumanReadableNum ($Cloud->"size") 1024 ] . "iB") ] . "\n" . \
[ $FormatLine "Download key" ($Cloud->"secret-download-key") ]); silent=true }); [ $FormatLine "Download key" ($Cloud->"secret-download-key") ]); silent=true });
} on-error={ } on-error={
$SendNotification2 ({ origin=$0; \ $SendNotification2 ({ origin=$ScriptName; \
subject=([ $SymbolForNotification "floppy-disk,warning-sign" ] . "Cloud backup failed"); \ subject=([ $SymbolForNotification "floppy-disk,warning-sign" ] . "Cloud backup failed"); \
message=("Failed uploading backup for " . $Identity . " to cloud!\n\n" . [ $DeviceInfo ]) }); message=("Failed uploading backup for " . $Identity . " to cloud!\n\n" . [ $DeviceInfo ]) });
$LogPrintExit2 error $0 ("Failed uploading backup for " . $Identity . " to cloud!") true; $LogPrintExit2 error $ScriptName ("Failed uploading backup for " . $Identity . " to cloud!") true;
} }
}
$Main [ :jobname ];