backup-email: move code into function

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

View file

@ -9,10 +9,12 @@
# create and email backup and config file # create and email backup and config file
# https://git.eworm.de/cgit/routeros-scripts/about/doc/backup-email.md # https://git.eworm.de/cgit/routeros-scripts/about/doc/backup-email.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 BackupSendBinary; :global BackupSendBinary;
@ -35,23 +37,23 @@
:global WaitFullyConnected; :global WaitFullyConnected;
:if ([ :typeof $SendEMail2 ] = "nothing") do={ :if ([ :typeof $SendEMail2 ] = "nothing") do={
$LogPrintExit2 error $0 ("The module for sending notifications via e-mail is not installed.") true; $LogPrintExit2 error $ScriptName ("The module for sending notifications via e-mail is not installed.") true;
} }
:if ($BackupSendBinary != true && \ :if ($BackupSendBinary != true && \
$BackupSendExport != true) do={ $BackupSendExport != true) do={
$LogPrintExit2 error $0 ("Configured to send neither backup nor config export.") true; $LogPrintExit2 error $ScriptName ("Configured to send neither backup nor config export.") true;
} }
$ScriptLock $0; $ScriptLock $ScriptName;
$WaitFullyConnected; $WaitFullyConnected;
:if ([ $ScriptFromTerminal $0 ] = false && $BackupRandomDelay > 0) do={ :if ([ $ScriptFromTerminal $ScriptName ] = false && $BackupRandomDelay > 0) do={
$RandomDelay $BackupRandomDelay; $RandomDelay $BackupRandomDelay;
} }
# filename based on identity # filename based on identity
:local DirName ("tmpfs/" . $0); :local DirName ("tmpfs/" . $ScriptName);
:local FileName [ $CleanName ($Identity . "." . $Domain) ]; :local FileName [ $CleanName ($Identity . "." . $Domain) ];
:local FilePath ($DirName . "/" . $FileName); :local FilePath ($DirName . "/" . $FileName);
:local BackupFile "none"; :local BackupFile "none";
@ -60,7 +62,7 @@ $WaitFullyConnected;
:local Attach ({}); :local Attach ({});
:if ([ $MkDir $DirName ] = false) do={ :if ([ $MkDir $DirName ] = false) do={
$LogPrintExit2 error $0 ("Failed creating directory!") true; $LogPrintExit2 error $ScriptName ("Failed creating directory!") true;
} }
# binary backup # binary backup
@ -90,7 +92,7 @@ $WaitFullyConnected;
} }
# send email with status and files # send email with status and files
$SendEMail2 ({ origin=$0; \ $SendEMail2 ({ origin=$ScriptName; \
subject=([ $SymbolForNotification "floppy-disk,incoming-envelope" ] . \ subject=([ $SymbolForNotification "floppy-disk,incoming-envelope" ] . \
"Backup & Config"); \ "Backup & Config"); \
message=("See attached files for backup and config export for " . \ message=("See attached files for backup and config export for " . \
@ -105,8 +107,11 @@ $SendEMail2 ({ origin=$0; \
:local I 0; :local I 0;
:while ([ :len [ /file/find where name ~ ($FilePath . "\\.(backup|rsc)\$") ] ] > 0) do={ :while ([ :len [ /file/find where name ~ ($FilePath . "\\.(backup|rsc)\$") ] ] > 0) do={
:if ($I >= 120) do={ :if ($I >= 120) do={
$LogPrintExit2 warning $0 ("Files are still available, sending e-mail failed.") true; $LogPrintExit2 warning $ScriptName ("Files are still available, sending e-mail failed.") true;
} }
:delay 1s; :delay 1s;
:set I ($I + 1); :set I ($I + 1);
} }
}
$Main [ :jobname ];