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
# https://git.eworm.de/cgit/routeros-scripts/about/doc/backup-email.md
:local 0 [ :jobname ];
:global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
:local Main do={
:local ScriptName [ :tostr $1 ];
:global BackupPassword;
:global BackupRandomDelay;
:global BackupSendBinary;
@ -35,23 +37,23 @@
:global WaitFullyConnected;
: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 && \
$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;
:if ([ $ScriptFromTerminal $0 ] = false && $BackupRandomDelay > 0) do={
:if ([ $ScriptFromTerminal $ScriptName ] = false && $BackupRandomDelay > 0) do={
$RandomDelay $BackupRandomDelay;
}
# filename based on identity
:local DirName ("tmpfs/" . $0);
:local DirName ("tmpfs/" . $ScriptName);
:local FileName [ $CleanName ($Identity . "." . $Domain) ];
:local FilePath ($DirName . "/" . $FileName);
:local BackupFile "none";
@ -60,7 +62,7 @@ $WaitFullyConnected;
:local Attach ({});
:if ([ $MkDir $DirName ] = false) do={
$LogPrintExit2 error $0 ("Failed creating directory!") true;
$LogPrintExit2 error $ScriptName ("Failed creating directory!") true;
}
# binary backup
@ -90,7 +92,7 @@ $WaitFullyConnected;
}
# send email with status and files
$SendEMail2 ({ origin=$0; \
$SendEMail2 ({ origin=$ScriptName; \
subject=([ $SymbolForNotification "floppy-disk,incoming-envelope" ] . \
"Backup & Config"); \
message=("See attached files for backup and config export for " . \
@ -105,8 +107,11 @@ $SendEMail2 ({ origin=$0; \
:local I 0;
:while ([ :len [ /file/find where name ~ ($FilePath . "\\.(backup|rsc)\$") ] ] > 0) 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;
:set I ($I + 1);
}
}
$Main [ :jobname ];