backup-upload: move code into function

This commit is contained in:
Christian Hesse 2024-03-04 13:48:00 +01:00
parent 0ded98c9e2
commit 1c26dde356

View file

@ -9,10 +9,12 @@
# create and upload backup and config file # create and upload backup and config file
# https://git.eworm.de/cgit/routeros-scripts/about/doc/backup-upload.md # https://git.eworm.de/cgit/routeros-scripts/about/doc/backup-upload.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;
@ -39,18 +41,18 @@
: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";
@ -59,7 +61,7 @@ $WaitFullyConnected;
:local Failed 0; :local Failed 0;
: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
@ -73,7 +75,7 @@ $WaitFullyConnected;
:set BackupFile [ /file/get ($FilePath . ".backup") ]; :set BackupFile [ /file/get ($FilePath . ".backup") ];
:set ($BackupFile->"name") ($FileName . ".backup"); :set ($BackupFile->"name") ($FileName . ".backup");
} on-error={ } on-error={
$LogPrintExit2 error $0 ("Uploading backup file failed!") false; $LogPrintExit2 error $ScriptName ("Uploading backup file failed!") false;
:set BackupFile "failed"; :set BackupFile "failed";
:set Failed 1; :set Failed 1;
} }
@ -92,7 +94,7 @@ $WaitFullyConnected;
:set ExportFile [ /file/get ($FilePath . ".rsc") ]; :set ExportFile [ /file/get ($FilePath . ".rsc") ];
:set ($ExportFile->"name") ($FileName . ".rsc"); :set ($ExportFile->"name") ($FileName . ".rsc");
} on-error={ } on-error={
$LogPrintExit2 error $0 ("Uploading configuration export failed!") false; $LogPrintExit2 error $ScriptName ("Uploading configuration export failed!") false;
:set ExportFile "failed"; :set ExportFile "failed";
:set Failed 1; :set Failed 1;
} }
@ -113,7 +115,7 @@ $WaitFullyConnected;
:set ConfigFile [ /file/get ($FilePath . ".conf") ]; :set ConfigFile [ /file/get ($FilePath . ".conf") ];
:set ($ConfigFile->"name") ($FileName . ".conf"); :set ($ConfigFile->"name") ($FileName . ".conf");
} on-error={ } on-error={
$LogPrintExit2 error $0 ("Uploading global-config-overlay failed!") false; $LogPrintExit2 error $ScriptName ("Uploading global-config-overlay failed!") false;
:set ConfigFile "failed"; :set ConfigFile "failed";
:set Failed 1; :set Failed 1;
} }
@ -136,7 +138,7 @@ $WaitFullyConnected;
[ $FormatLine $Name $File ] ]; [ $FormatLine $Name $File ] ];
} }
$SendNotification2 ({ origin=$0; \ $SendNotification2 ({ origin=$ScriptName; \
subject=[ $IfThenElse ($Failed > 0) \ subject=[ $IfThenElse ($Failed > 0) \
([ $SymbolForNotification "floppy-disk,warning-sign" ] . "Backup & Config upload with failure") \ ([ $SymbolForNotification "floppy-disk,warning-sign" ] . "Backup & Config upload with failure") \
([ $SymbolForNotification "floppy-disk,arrow-up" ] . "Backup & Config upload") ]; \ ([ $SymbolForNotification "floppy-disk,arrow-up" ] . "Backup & Config upload") ]; \
@ -149,3 +151,6 @@ $SendNotification2 ({ origin=$0; \
:if ($Failed = 1) do={ :if ($Failed = 1) do={
:error "An error occured!"; :error "An error occured!";
} }
}
$Main [ :jobname ];