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,104 +9,109 @@
# 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; }
:global BackupPassword; :local Main do={
:global BackupRandomDelay; :local ScriptName [ :tostr $1 ];
:global BackupSendBinary;
:global BackupSendExport;
:global BackupSendGlobalConfig;
:global Domain;
:global Identity;
:global CleanName; :global BackupPassword;
:global DeviceInfo; :global BackupRandomDelay;
:global FormatLine; :global BackupSendBinary;
:global LogPrintExit2; :global BackupSendExport;
:global MkDir; :global BackupSendGlobalConfig;
:global RandomDelay; :global Domain;
:global ScriptFromTerminal; :global Identity;
:global ScriptLock;
:global SendEMail2;
:global SymbolForNotification;
:global WaitForFile;
:global WaitFullyConnected;
:if ([ :typeof $SendEMail2 ] = "nothing") do={ :global CleanName;
$LogPrintExit2 error $0 ("The module for sending notifications via e-mail is not installed.") true; :global DeviceInfo;
} :global FormatLine;
:global LogPrintExit2;
:global MkDir;
:global RandomDelay;
:global ScriptFromTerminal;
:global ScriptLock;
:global SendEMail2;
:global SymbolForNotification;
:global WaitForFile;
:global WaitFullyConnected;
:if ($BackupSendBinary != true && \ :if ([ :typeof $SendEMail2 ] = "nothing") do={
$BackupSendExport != true) do={ $LogPrintExit2 error $ScriptName ("The module for sending notifications via e-mail is not installed.") true;
$LogPrintExit2 error $0 ("Configured to send neither backup nor config export.") true; }
}
:if ($BackupSendBinary != true && \
$ScriptLock $0; $BackupSendExport != true) do={
$WaitFullyConnected; $LogPrintExit2 error $ScriptName ("Configured to send neither backup nor config export.") true;
}
:if ([ $ScriptFromTerminal $0 ] = false && $BackupRandomDelay > 0) do={
$RandomDelay $BackupRandomDelay; $ScriptLock $ScriptName;
} $WaitFullyConnected;
# filename based on identity :if ([ $ScriptFromTerminal $ScriptName ] = false && $BackupRandomDelay > 0) do={
:local DirName ("tmpfs/" . $0); $RandomDelay $BackupRandomDelay;
:local FileName [ $CleanName ($Identity . "." . $Domain) ]; }
:local FilePath ($DirName . "/" . $FileName);
:local BackupFile "none"; # filename based on identity
:local ExportFile "none"; :local DirName ("tmpfs/" . $ScriptName);
:local ConfigFile "none"; :local FileName [ $CleanName ($Identity . "." . $Domain) ];
:local Attach ({}); :local FilePath ($DirName . "/" . $FileName);
:local BackupFile "none";
:if ([ $MkDir $DirName ] = false) do={ :local ExportFile "none";
$LogPrintExit2 error $0 ("Failed creating directory!") true; :local ConfigFile "none";
} :local Attach ({});
# binary backup :if ([ $MkDir $DirName ] = false) do={
:if ($BackupSendBinary = true) do={ $LogPrintExit2 error $ScriptName ("Failed creating directory!") true;
/system/backup/save encryption=aes-sha256 name=$FilePath password=$BackupPassword; }
$WaitForFile ($FilePath . ".backup");
:set BackupFile ($FileName . ".backup"); # binary backup
:set Attach ($Attach, ($FilePath . ".backup")); :if ($BackupSendBinary = true) do={
} /system/backup/save encryption=aes-sha256 name=$FilePath password=$BackupPassword;
$WaitForFile ($FilePath . ".backup");
# create configuration export :set BackupFile ($FileName . ".backup");
:if ($BackupSendExport = true) do={ :set Attach ($Attach, ($FilePath . ".backup"));
/export terse show-sensitive file=$FilePath; }
$WaitForFile ($FilePath . ".rsc");
:set ExportFile ($FileName . ".rsc"); # create configuration export
:set Attach ($Attach, ($FilePath . ".rsc")); :if ($BackupSendExport = true) do={
} /export terse show-sensitive file=$FilePath;
$WaitForFile ($FilePath . ".rsc");
# global-config-overlay :set ExportFile ($FileName . ".rsc");
:if ($BackupSendGlobalConfig = true) do={ :set Attach ($Attach, ($FilePath . ".rsc"));
# Do *NOT* use '/file/add ...' here, as it is limited to 4095 bytes! }
:execute script={ :put [ /system/script/get global-config-overlay source ]; } \
file=($FilePath . ".conf\00"); # global-config-overlay
$WaitForFile ($FilePath . ".conf"); :if ($BackupSendGlobalConfig = true) do={
:set ConfigFile ($FileName . ".conf"); # Do *NOT* use '/file/add ...' here, as it is limited to 4095 bytes!
:set Attach ($Attach, ($FilePath . ".conf")); :execute script={ :put [ /system/script/get global-config-overlay source ]; } \
} file=($FilePath . ".conf\00");
$WaitForFile ($FilePath . ".conf");
# send email with status and files :set ConfigFile ($FileName . ".conf");
$SendEMail2 ({ origin=$0; \ :set Attach ($Attach, ($FilePath . ".conf"));
subject=([ $SymbolForNotification "floppy-disk,incoming-envelope" ] . \ }
"Backup & Config"); \
message=("See attached files for backup and config export for " . \ # send email with status and files
$Identity . ".\n\n" . \ $SendEMail2 ({ origin=$ScriptName; \
[ $DeviceInfo ] . "\n\n" . \ subject=([ $SymbolForNotification "floppy-disk,incoming-envelope" ] . \
[ $FormatLine "Backup file" $BackupFile ] . "\n" . \ "Backup & Config"); \
[ $FormatLine "Export file" $ExportFile ] . "\n" . \ message=("See attached files for backup and config export for " . \
[ $FormatLine "Config file" $ConfigFile ]); \ $Identity . ".\n\n" . \
attach=$Attach; remove-attach=true }); [ $DeviceInfo ] . "\n\n" . \
[ $FormatLine "Backup file" $BackupFile ] . "\n" . \
# wait for the mail to be sent [ $FormatLine "Export file" $ExportFile ] . "\n" . \
:local I 0; [ $FormatLine "Config file" $ConfigFile ]); \
:while ([ :len [ /file/find where name ~ ($FilePath . "\\.(backup|rsc)\$") ] ] > 0) do={ attach=$Attach; remove-attach=true });
:if ($I >= 120) do={
$LogPrintExit2 warning $0 ("Files are still available, sending e-mail failed.") true; # wait for the mail to be sent
:local I 0;
:while ([ :len [ /file/find where name ~ ($FilePath . "\\.(backup|rsc)\$") ] ] > 0) do={
:if ($I >= 120) do={
$LogPrintExit2 warning $ScriptName ("Files are still available, sending e-mail failed.") true;
}
:delay 1s;
:set I ($I + 1);
} }
:delay 1s;
:set I ($I + 1);
} }
$Main [ :jobname ];