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
|
2024-04-04 19:45:02 +00:00
|
|
|
# requires RouterOS, version=7.13
|
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;
|
2024-03-08 08:25:56 +00:00
|
|
|
: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;
|
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-08 08:25:56 +00:00
|
|
|
:set PackagesUpdateBackupFailure true;
|
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={
|
2024-03-08 11:45:37 +00:00
|
|
|
$LogPrint error $ScriptName ("Failed creating directory!");
|
|
|
|
:error false;
|
2024-03-04 19:57:34 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 15:37:28 +00:00
|
|
|
:local I 5;
|
|
|
|
:do {
|
|
|
|
:execute {
|
|
|
|
:global BackupPassword;
|
2024-04-23 11:53:04 +00:00
|
|
|
|
2024-04-29 15:37:28 +00:00
|
|
|
: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);
|
2024-03-04 19:57:34 +00:00
|
|
|
|
|
|
|
: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" . \
|
2024-04-07 19:11:03 +00:00
|
|
|
[ $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 });
|
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 ]) });
|
2024-03-08 11:45:37 +00:00
|
|
|
$LogPrint error $ScriptName ("Failed uploading backup for " . $Identity . " to cloud!");
|
2024-03-08 08:25:56 +00:00
|
|
|
:set PackagesUpdateBackupFailure 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={ }
|