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,61 +9,63 @@
# 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; }
:global BackupPassword; :local Main do={
:global BackupRandomDelay; :local ScriptName [ :tostr $1 ];
:global BackupSendBinary;
:global BackupSendExport;
:global BackupSendGlobalConfig;
:global BackupUploadPass;
:global BackupUploadUrl;
:global BackupUploadUser;
:global Domain;
:global Identity;
:global CleanName; :global BackupPassword;
:global DeviceInfo; :global BackupRandomDelay;
:global IfThenElse; :global BackupSendBinary;
:global LogPrintExit2; :global BackupSendExport;
:global MkDir; :global BackupSendGlobalConfig;
:global RandomDelay; :global BackupUploadPass;
:global ScriptFromTerminal; :global BackupUploadUrl;
:global ScriptLock; :global BackupUploadUser;
:global SendNotification2; :global Domain;
:global SymbolForNotification; :global Identity;
:global WaitForFile;
:global WaitFullyConnected;
:if ($BackupSendBinary != true && \ :global CleanName;
:global DeviceInfo;
:global IfThenElse;
:global LogPrintExit2;
:global MkDir;
:global RandomDelay;
:global ScriptFromTerminal;
:global ScriptLock;
:global SendNotification2;
:global SymbolForNotification;
:global WaitForFile;
:global WaitFullyConnected;
: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";
:local ExportFile "none"; :local ExportFile "none";
:local ConfigFile "none"; :local ConfigFile "none";
: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
:if ($BackupSendBinary = true) do={ :if ($BackupSendBinary = true) do={
/system/backup/save encryption=aes-sha256 name=$FilePath password=$BackupPassword; /system/backup/save encryption=aes-sha256 name=$FilePath password=$BackupPassword;
$WaitForFile ($FilePath . ".backup"); $WaitForFile ($FilePath . ".backup");
@ -73,16 +75,16 @@ $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;
} }
/file/remove ($FilePath . ".backup"); /file/remove ($FilePath . ".backup");
} }
# create configuration export # create configuration export
:if ($BackupSendExport = true) do={ :if ($BackupSendExport = true) do={
/export terse show-sensitive file=$FilePath; /export terse show-sensitive file=$FilePath;
$WaitForFile ($FilePath . ".rsc"); $WaitForFile ($FilePath . ".rsc");
@ -92,16 +94,16 @@ $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;
} }
/file/remove ($FilePath . ".rsc"); /file/remove ($FilePath . ".rsc");
} }
# global-config-overlay # global-config-overlay
:if ($BackupSendGlobalConfig = true) do={ :if ($BackupSendGlobalConfig = true) do={
# Do *NOT* use '/file/add ...' here, as it is limited to 4095 bytes! # Do *NOT* use '/file/add ...' here, as it is limited to 4095 bytes!
:execute script={ :put [ /system/script/get global-config-overlay source ]; } \ :execute script={ :put [ /system/script/get global-config-overlay source ]; } \
file=($FilePath . ".conf\00"); file=($FilePath . ".conf\00");
@ -113,15 +115,15 @@ $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;
} }
/file/remove ($FilePath . ".conf"); /file/remove ($FilePath . ".conf");
} }
:local FileInfo do={ :local FileInfo do={
:local Name $1; :local Name $1;
:local File $2; :local File $2;
@ -134,9 +136,9 @@ $WaitFullyConnected;
($Name . ":\n" . [ $FormatLine " name" ($File->"name") ] . "\n" . \ ($Name . ":\n" . [ $FormatLine " name" ($File->"name") ] . "\n" . \
[ $FormatLine " size" ([ $HumanReadableNum ($File->"size") 1024 ] . "iB") ]) \ [ $FormatLine " size" ([ $HumanReadableNum ($File->"size") 1024 ] . "iB") ]) \
[ $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") ]; \
@ -146,6 +148,9 @@ $SendNotification2 ({ origin=$0; \
[ $FileInfo "Export file" $ExportFile ] . "\n" . \ [ $FileInfo "Export file" $ExportFile ] . "\n" . \
[ $FileInfo "Config file" $ConfigFile ]); silent=true }); [ $FileInfo "Config file" $ConfigFile ]); silent=true });
:if ($Failed = 1) do={ :if ($Failed = 1) do={
:error "An error occured!"; :error "An error occured!";
}
} }
$Main [ :jobname ];