routeros-scripts/upload-backup

107 lines
3.1 KiB
Text
Raw Normal View History

#!rsc by RouterOS
2019-07-08 15:48:55 +00:00
# RouterOS script: upload-backup
2021-01-01 20:33:52 +00:00
# Copyright (c) 2013-2021 Christian Hesse <mail@eworm.de>
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
2019-07-08 15:48:55 +00:00
#
# provides: backup-script
#
2019-07-08 15:48:55 +00:00
# create and upload backup and config file
2020-03-27 20:52:17 +00:00
# https://git.eworm.de/cgit/routeros-scripts/about/doc/upload-backup.md
2019-07-08 15:48:55 +00:00
:local 0 "upload-backup";
:global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
2020-02-28 14:26:26 +00:00
:global BackupPassword;
:global BackupRandomDelay;
2019-07-08 15:48:55 +00:00
:global BackupSendBinary;
:global BackupSendExport;
2020-02-28 14:26:26 +00:00
:global BackupUploadPass;
:global BackupUploadUrl;
:global BackupUploadUser;
:global Domain;
:global Identity;
2019-07-08 15:48:55 +00:00
:global CharacterReplace;
:global DeviceInfo;
:global IfThenElse;
:global LogPrintExit2;
2021-04-29 14:07:33 +00:00
:global MkDir;
:global RandomDelay;
:global ScriptFromTerminal;
2021-04-27 18:44:34 +00:00
:global SendNotification2;
:global SymbolForNotification;
2020-09-01 09:19:20 +00:00
:global WaitForFile;
:global WaitFullyConnected;
2019-07-08 15:48:55 +00:00
:if ($BackupSendBinary != true && \
$BackupSendExport != true) do={
$LogPrintExit2 error $0 ("Configured to send neither backup nor config export.") true;
2019-07-08 15:48:55 +00:00
}
$WaitFullyConnected;
:if ([ $ScriptFromTerminal $0 ] = false && $BackupRandomDelay > 0) do={
$RandomDelay $BackupRandomDelay;
}
2021-04-29 14:07:33 +00:00
:if ([ $MkDir $0 ] = false) do={
$LogPrintExit2 error $0 ("Failed creating directory!") true;
}
2019-07-08 15:48:55 +00:00
# filename based on identity
:local FileName [ $CharacterReplace ($Identity . "." . $Domain) "." "_" ];
2021-04-29 14:07:33 +00:00
:local FilePath ($0 . "/" . $FileName);
2019-07-08 15:48:55 +00:00
:local BackupFile "none";
:local ConfigFile "none";
:local Failed 0;
2019-07-08 15:48:55 +00:00
# binary backup
:if ($BackupSendBinary = true) do={
2021-04-29 14:07:33 +00:00
/ system backup save encryption=aes-sha256 name=$FilePath password=$BackupPassword;
$WaitForFile ($FilePath . ".backup");
2019-07-08 15:48:55 +00:00
:do {
/ tool fetch upload=yes url=($BackupUploadUrl . "/" . $FileName . ".backup") \
2021-04-29 14:07:33 +00:00
user=$BackupUploadUser password=$BackupUploadPass src-path=($FilePath . ".backup");
2019-07-08 15:48:55 +00:00
:set BackupFile ($FileName . ".backup");
} on-error={
$LogPrintExit2 error $0 ("Uploading backup file failed!") false;
2019-07-08 15:48:55 +00:00
:set BackupFile "failed";
:set Failed 1;
2019-07-08 15:48:55 +00:00
}
/ file remove ($FilePath . ".backup");
2019-07-08 15:48:55 +00:00
}
# create configuration export
:if ($BackupSendExport = true) do={
2021-04-29 14:07:33 +00:00
/ export terse file=$FilePath;
$WaitForFile ($FilePath . ".rsc");
2019-07-08 15:48:55 +00:00
:do {
/ tool fetch upload=yes url=($BackupUploadUrl . "/" . $FileName . ".rsc") \
2021-04-29 14:07:33 +00:00
user=$BackupUploadUser password=$BackupUploadPass src-path=($FilePath . ".rsc");
2019-07-08 15:48:55 +00:00
:set ConfigFile ($FileName . ".rsc");
} on-error={
$LogPrintExit2 error $0 ("Uploading configuration export failed!") false;
2019-07-08 15:48:55 +00:00
:set ConfigFile "failed";
:set Failed 1;
2019-07-08 15:48:55 +00:00
}
/ file remove ($FilePath . ".rsc");
2019-07-08 15:48:55 +00:00
}
$SendNotification2 ({ origin=$0; \
subject=[ $IfThenElse ($Failed > 0) \
2020-10-07 21:18:48 +00:00
([ $SymbolForNotification "warning-sign" ] . "Backup & Config upload with failure") \
2021-04-29 20:27:29 +00:00
([ $SymbolForNotification "floppy-disk,up-arrow" ] . "Backup & Config upload") ]; \
2021-04-27 18:44:34 +00:00
message=("Backup and config export upload for " . $Identity . ".\n\n" . \
[ $DeviceInfo ] . "\n\n" . \
"Backup file: " . $BackupFile . "\n" . \
2021-04-27 18:44:34 +00:00
"Config file: " . $ConfigFile); silent=true });
:if ($Failed = 1) do={
:error "An error occured!";
}