2020-09-18 09:00:27 +00:00
|
|
|
#!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>
|
2020-06-19 20:17:42 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
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
|
|
|
|
2021-02-22 14:14:10 +00:00
|
|
|
:local 0 "upload-backup";
|
2021-02-18 13:52:47 +00:00
|
|
|
:global GlobalFunctionsReady;
|
|
|
|
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
|
|
|
|
2020-02-28 14:26:26 +00:00
|
|
|
:global BackupPassword;
|
2021-02-23 08:56:28 +00:00
|
|
|
: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;
|
2019-09-12 11:45:44 +00:00
|
|
|
:global DeviceInfo;
|
2020-07-17 10:00:59 +00:00
|
|
|
:global IfThenElse;
|
2021-02-22 14:14:10 +00:00
|
|
|
:global LogPrintExit2;
|
2021-04-29 14:07:33 +00:00
|
|
|
:global MkDir;
|
2021-02-23 08:56:28 +00:00
|
|
|
:global RandomDelay;
|
|
|
|
:global ScriptFromTerminal;
|
2021-04-27 18:44:34 +00:00
|
|
|
:global SendNotification2;
|
2020-07-17 09:23:17 +00:00
|
|
|
:global SymbolForNotification;
|
2020-09-01 09:19:20 +00:00
|
|
|
:global WaitForFile;
|
2021-02-18 08:12:44 +00:00
|
|
|
:global WaitFullyConnected;
|
|
|
|
|
2019-07-08 15:48:55 +00:00
|
|
|
:if ($BackupSendBinary != true && \
|
|
|
|
$BackupSendExport != true) do={
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 error $0 ("Configured to send neither backup nor config export.") true;
|
2019-07-08 15:48:55 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 08:56:28 +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";
|
2020-07-17 10:00:59 +00:00
|
|
|
: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={
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 error $0 ("Uploading backup file failed!") false;
|
2019-07-08 15:48:55 +00:00
|
|
|
:set BackupFile "failed";
|
2020-07-17 10:00:59 +00:00
|
|
|
:set Failed 1;
|
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={
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 error $0 ("Uploading configuration export failed!") false;
|
2019-07-08 15:48:55 +00:00
|
|
|
:set ConfigFile "failed";
|
2020-07-17 10:00:59 +00:00
|
|
|
:set Failed 1;
|
2019-07-08 15:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 19:49:39 +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" . \
|
2019-09-12 11:45:44 +00:00
|
|
|
[ $DeviceInfo ] . "\n\n" . \
|
|
|
|
"Backup file: " . $BackupFile . "\n" . \
|
2021-04-27 18:44:34 +00:00
|
|
|
"Config file: " . $ConfigFile); silent=true });
|
2020-10-07 13:29:18 +00:00
|
|
|
|
|
|
|
:if ($Failed = 1) do={
|
|
|
|
:error "An error occured!";
|
|
|
|
}
|