2023-03-07 09:57:42 +00:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: backup-cloud
|
2024-01-01 14:25:25 +00:00
|
|
|
# Copyright (c) 2013-2024 Christian Hesse <mail@eworm.de>
|
2023-03-07 09:57:42 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
|
|
|
#
|
2023-09-12 23:01:34 +00:00
|
|
|
# provides: backup-script, order=40
|
2023-11-15 20:31:18 +00:00
|
|
|
# requires RouterOS, version=7.12
|
2023-03-07 09:57:42 +00:00
|
|
|
#
|
|
|
|
# upload backup to MikroTik cloud
|
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/backup-cloud.md
|
|
|
|
|
|
|
|
:global GlobalFunctionsReady;
|
|
|
|
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
|
|
|
|
2024-03-06 14:28:55 +00:00
|
|
|
:do {
|
|
|
|
:local ScriptName [ :jobname ];
|
2024-03-04 12:48:00 +00:00
|
|
|
|
|
|
|
:global BackupRandomDelay;
|
|
|
|
:global Identity;
|
|
|
|
|
|
|
|
:global DeviceInfo;
|
|
|
|
:global FormatLine;
|
|
|
|
:global HumanReadableNum;
|
|
|
|
:global LogPrintExit2;
|
2024-03-04 19:57:34 +00:00
|
|
|
:global MkDir;
|
2024-03-04 12:48:00 +00:00
|
|
|
:global RandomDelay;
|
|
|
|
:global ScriptFromTerminal;
|
|
|
|
:global ScriptLock;
|
|
|
|
:global SendNotification2;
|
|
|
|
:global SymbolForNotification;
|
2024-03-04 19:57:34 +00:00
|
|
|
:global WaitForFile;
|
2024-03-04 12:48:00 +00:00
|
|
|
:global WaitFullyConnected;
|
|
|
|
|
2024-03-05 15:12:36 +00:00
|
|
|
:if ([ $ScriptLock $ScriptName ] = false) do={
|
2024-03-06 14:28:55 +00:00
|
|
|
:error false;
|
2024-03-05 15:12:36 +00:00
|
|
|
}
|
2024-03-04 12:48:00 +00:00
|
|
|
$WaitFullyConnected;
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2024-03-04 12:48:00 +00:00
|
|
|
:if ([ $ScriptFromTerminal $ScriptName ] = false && $BackupRandomDelay > 0) do={
|
|
|
|
$RandomDelay $BackupRandomDelay;
|
|
|
|
}
|
|
|
|
|
2024-03-04 19:57:34 +00:00
|
|
|
:if ([ $MkDir ("tmpfs/backup-cloud") ] = false) do={
|
|
|
|
$LogPrintExit2 error $ScriptName ("Failed creating directory!") true;
|
|
|
|
}
|
|
|
|
|
|
|
|
:execute {
|
|
|
|
:global BackupPassword;
|
2024-03-04 12:48:00 +00:00
|
|
|
# we are not interested in output, but print is
|
|
|
|
# required to fetch information from cloud
|
|
|
|
/system/backup/cloud/print as-value;
|
2024-03-05 09:31:33 +00:00
|
|
|
:delay 20ms;
|
2024-03-04 12:48:00 +00:00
|
|
|
:if ([ :len [ /system/backup/cloud/find ] ] > 0) do={
|
|
|
|
/system/backup/cloud/upload-file action=create-and-upload \
|
|
|
|
password=$BackupPassword replace=[ get ([ find ]->0) name ];
|
|
|
|
} else={
|
|
|
|
/system/backup/cloud/upload-file action=create-and-upload \
|
|
|
|
password=$BackupPassword;
|
|
|
|
}
|
2024-03-04 19:57:34 +00:00
|
|
|
/file/add name="tmpfs/backup-cloud/done";
|
|
|
|
} as-string;
|
|
|
|
|
|
|
|
:if ([ $WaitForFile "tmpfs/backup-cloud/done" ] = true) do={
|
2024-03-04 12:48:00 +00:00
|
|
|
:local Cloud [ /system/backup/cloud/get ([ find ]->0) ];
|
|
|
|
|
|
|
|
$SendNotification2 ({ origin=$ScriptName; \
|
|
|
|
subject=([ $SymbolForNotification "floppy-disk,cloud" ] . "Cloud backup"); \
|
|
|
|
message=("Uploaded backup for " . $Identity . " to cloud.\n\n" . \
|
|
|
|
[ $DeviceInfo ] . "\n\n" . \
|
|
|
|
[ $FormatLine "Name" ($Cloud->"name") ] . "\n" . \
|
|
|
|
[ $FormatLine "Size" ([ $HumanReadableNum ($Cloud->"size") 1024 ] . "iB") ] . "\n" . \
|
|
|
|
[ $FormatLine "Download key" ($Cloud->"secret-download-key") ]); silent=true });
|
2024-03-04 19:57:34 +00:00
|
|
|
} else={
|
2024-03-04 12:48:00 +00:00
|
|
|
$SendNotification2 ({ origin=$ScriptName; \
|
|
|
|
subject=([ $SymbolForNotification "floppy-disk,warning-sign" ] . "Cloud backup failed"); \
|
|
|
|
message=("Failed uploading backup for " . $Identity . " to cloud!\n\n" . [ $DeviceInfo ]) });
|
|
|
|
$LogPrintExit2 error $ScriptName ("Failed uploading backup for " . $Identity . " to cloud!") true;
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
2024-03-04 19:57:34 +00:00
|
|
|
/file/remove "tmpfs/backup-cloud";
|
2024-03-06 14:28:55 +00:00
|
|
|
} on-error={ }
|