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