routeros-scripts/backup-cloud.rsc

87 lines
2.8 KiB
Text
Raw Normal View History

#!rsc by RouterOS
# RouterOS script: backup-cloud
2024-01-01 14:25:25 +00:00
# Copyright (c) 2013-2024 Christian Hesse <mail@eworm.de>
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
#
# provides: backup-script, order=40
# requires RouterOS, version=7.13
#
# 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; }
:do {
:local ScriptName [ :jobname ];
2024-03-04 12:48:00 +00:00
:global BackupRandomDelay;
:global Identity;
:global PackagesUpdateBackupFailure;
2024-03-04 12:48:00 +00:00
:global DeviceInfo;
:global FormatLine;
:global HumanReadableNum;
2024-03-08 11:45:37 +00:00
:global LogPrint;
:global MkDir;
2024-03-04 12:48:00 +00:00
:global RandomDelay;
:global ScriptFromTerminal;
:global ScriptLock;
:global SendNotification2;
:global SymbolForNotification;
:global WaitForFile;
2024-03-04 12:48:00 +00:00
:global WaitFullyConnected;
:if ([ $ScriptLock $ScriptName ] = false) do={
:set PackagesUpdateBackupFailure true;
:error false;
}
2024-03-04 12:48:00 +00:00
$WaitFullyConnected;
2024-03-04 12:48:00 +00:00
:if ([ $ScriptFromTerminal $ScriptName ] = false && $BackupRandomDelay > 0) do={
$RandomDelay $BackupRandomDelay;
}
:if ([ $MkDir ("tmpfs/backup-cloud") ] = false) do={
2024-03-08 11:45:37 +00:00
$LogPrint error $ScriptName ("Failed creating directory!");
:error false;
}
:local I 5;
:do {
:execute {
:global BackupPassword;
:local Backup ([ /system/backup/cloud/find ]->0);
:if ([ :typeof $Backup ] = "id") do={
/system/backup/cloud/upload-file action=create-and-upload \
password=$BackupPassword replace=$Backup;
} else={
/system/backup/cloud/upload-file action=create-and-upload \
password=$BackupPassword;
}
/file/add name="tmpfs/backup-cloud/done";
} as-string;
:set I ($I - 1);
} while=([ $WaitForFile "tmpfs/backup-cloud/done" 200ms ] = false && $I > 0);
: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 ] . "B") ] . "\n" . \
2024-03-04 12:48:00 +00:00
[ $FormatLine "Download key" ($Cloud->"secret-download-key") ]); silent=true });
} 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 ]) });
2024-03-08 11:45:37 +00:00
$LogPrint error $ScriptName ("Failed uploading backup for " . $Identity . " to cloud!");
:set PackagesUpdateBackupFailure true;
}
/file/remove "tmpfs/backup-cloud";
} on-error={ }