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,55 +9,60 @@
# 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; }
:global BackupPassword; :local Main do={
:global BackupRandomDelay; :local ScriptName [ :tostr $1 ];
:global Identity;
:global DeviceInfo; :global BackupPassword;
:global FormatLine; :global BackupRandomDelay;
:global HumanReadableNum; :global Identity;
:global LogPrintExit2;
:global RandomDelay;
:global ScriptFromTerminal;
:global ScriptLock;
:global SendNotification2;
:global SymbolForNotification;
:global WaitFullyConnected;
$ScriptLock $0; :global DeviceInfo;
$WaitFullyConnected; :global FormatLine;
:global HumanReadableNum;
:global LogPrintExit2;
:global RandomDelay;
:global ScriptFromTerminal;
:global ScriptLock;
:global SendNotification2;
:global SymbolForNotification;
:global WaitFullyConnected;
:if ([ $ScriptFromTerminal $0 ] = false && $BackupRandomDelay > 0) do={ $ScriptLock $ScriptName;
$RandomDelay $BackupRandomDelay; $WaitFullyConnected;
}
:do { :if ([ $ScriptFromTerminal $ScriptName ] = false && $BackupRandomDelay > 0) do={
# we are not interested in output, but print is $RandomDelay $BackupRandomDelay;
# required to fetch information from cloud
/system/backup/cloud/print as-value;
:if ([ :len [ /system/backup/cloud/find ] ] > 0) do={
/system/backup/cloud/upload-file action=create-and-upload \
password=$BackupPassword replace=[ get ([ find ]->0) name ];
} else={
/system/backup/cloud/upload-file action=create-and-upload \
password=$BackupPassword;
} }
:local Cloud [ /system/backup/cloud/get ([ find ]->0) ];
$SendNotification2 ({ origin=$0; \ :do {
subject=([ $SymbolForNotification "floppy-disk,cloud" ] . "Cloud backup"); \ # we are not interested in output, but print is
message=("Uploaded backup for " . $Identity . " to cloud.\n\n" . \ # required to fetch information from cloud
[ $DeviceInfo ] . "\n\n" . \ /system/backup/cloud/print as-value;
[ $FormatLine "Name" ($Cloud->"name") ] . "\n" . \ :if ([ :len [ /system/backup/cloud/find ] ] > 0) do={
[ $FormatLine "Size" ([ $HumanReadableNum ($Cloud->"size") 1024 ] . "iB") ] . "\n" . \ /system/backup/cloud/upload-file action=create-and-upload \
[ $FormatLine "Download key" ($Cloud->"secret-download-key") ]); silent=true }); password=$BackupPassword replace=[ get ([ find ]->0) name ];
} on-error={ } else={
$SendNotification2 ({ origin=$0; \ /system/backup/cloud/upload-file action=create-and-upload \
subject=([ $SymbolForNotification "floppy-disk,warning-sign" ] . "Cloud backup failed"); \ password=$BackupPassword;
message=("Failed uploading backup for " . $Identity . " to cloud!\n\n" . [ $DeviceInfo ]) }); }
$LogPrintExit2 error $0 ("Failed uploading backup for " . $Identity . " to cloud!") true; :local Cloud [ /system/backup/cloud/get ([ find ]->0) ];
$SendNotification2 ({ origin=$ScriptName; \
subject=([ $SymbolForNotification "floppy-disk,cloud" ] . "Cloud backup"); \
message=("Uploaded backup for " . $Identity . " to cloud.\n\n" . \
[ $DeviceInfo ] . "\n\n" . \
[ $FormatLine "Name" ($Cloud->"name") ] . "\n" . \
[ $FormatLine "Size" ([ $HumanReadableNum ($Cloud->"size") 1024 ] . "iB") ] . "\n" . \
[ $FormatLine "Download key" ($Cloud->"secret-download-key") ]); silent=true });
} on-error={
$SendNotification2 ({ origin=$ScriptName; \
subject=([ $SymbolForNotification "floppy-disk,warning-sign" ] . "Cloud backup failed"); \
message=("Failed uploading backup for " . $Identity . " to cloud!\n\n" . [ $DeviceInfo ]) });
$LogPrintExit2 error $ScriptName ("Failed uploading backup for " . $Identity . " to cloud!") true;
}
} }
$Main [ :jobname ];