aboutsummaryrefslogtreecommitdiffstats
path: root/backup-partition.rsc
diff options
context:
space:
mode:
Diffstat (limited to 'backup-partition.rsc')
-rw-r--r--backup-partition.rsc52
1 files changed, 36 insertions, 16 deletions
diff --git a/backup-partition.rsc b/backup-partition.rsc
index 51df454..ae7ad03 100644
--- a/backup-partition.rsc
+++ b/backup-partition.rsc
@@ -1,18 +1,20 @@
#!rsc by RouterOS
# RouterOS script: backup-partition
-# Copyright (c) 2022-2024 Christian Hesse <mail@eworm.de>
-# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
+# Copyright (c) 2022-2025 Christian Hesse <mail@eworm.de>
+# https://rsc.eworm.de/COPYING.md
#
# provides: backup-script, order=70
-# requires RouterOS, version=7.14
+# requires RouterOS, version=7.15
+# requires device-mode, scheduler
#
# save configuration to fallback partition
-# https://git.eworm.de/cgit/routeros-scripts/about/doc/backup-partition.md
+# https://rsc.eworm.de/doc/backup-partition.md
-:global GlobalFunctionsReady;
-:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
-
-:do {
+:local ExitOK false;
+:onerror Err {
+ :global GlobalConfigReady; :global GlobalFunctionsReady;
+ :retry { :if ($GlobalConfigReady != true || $GlobalFunctionsReady != true) \
+ do={ :error ("Global config and/or functions not ready."); }; } delay=500ms max=50;
:local ScriptName [ :jobname ];
:global BackupPartitionCopyBeforeFeatureUpdate;
@@ -30,24 +32,34 @@
:global LogPrint;
- :do {
+ :onerror Err {
/partitions/copy-to $FallbackTo;
$LogPrint info $ScriptName ("Copied RouterOS to partition '" . $FallbackToName . "'.");
- :return true;
- } on-error={
- $LogPrint error $ScriptName ("Failed copying RouterOS to partition '" . $FallbackToName . "'!");
+ } do={
+ $LogPrint error $ScriptName ("Failed copying RouterOS to partition '" . \
+ $FallbackToName . "': " . $Err);
:return false;
}
+ :return true;
}
:if ([ $ScriptLock $ScriptName ] = false) do={
:set PackagesUpdateBackupFailure true;
+ :set ExitOK true;
+ :error false;
+ }
+
+ :if ([ :len [ /system/scheduler/find where name="running-from-backup-partition" ] ] > 0) do={
+ $LogPrint warning $ScriptName ("Running from backup partition, refusing to act.");
+ :set PackagesUpdateBackupFailure true;
+ :set ExitOK true;
:error false;
}
:if ([ :len [ /partitions/find ] ] < 2) do={
$LogPrint error $ScriptName ("Device does not have a fallback partition.");
:set PackagesUpdateBackupFailure true;
+ :set ExitOK true;
:error false;
}
@@ -56,6 +68,7 @@
:if ([ :len $ActiveRunning ] < 1) do={
$LogPrint error $ScriptName ("Device is not running from active partition.");
:set PackagesUpdateBackupFailure true;
+ :set ExitOK true;
:error false;
}
@@ -65,6 +78,7 @@
:if ([ :len $FallbackTo ] < 1) do={
$LogPrint error $ScriptName ("There is no inactive partition named '" . $FallbackToName . "'.");
:set PackagesUpdateBackupFailure true;
+ :set ExitOK true;
:error false;
}
@@ -74,6 +88,7 @@
:if (([ /terminal/inkey timeout=60 ] % 32) = 25) do={
:if ([ $CopyTo $ScriptName $FallbackTo $FallbackToName ] = false) do={
:set PackagesUpdateBackupFailure true;
+ :set ExitOK true;
:error false;
}
}
@@ -86,23 +101,28 @@
($NumInstalled & $BitMask) != ($NumLatest & $BitMask)) do={
:if ([ $CopyTo $ScriptName $FallbackTo $FallbackToName ] = false) do={
:set PackagesUpdateBackupFailure true;
+ :set ExitOK true;
:error false;
}
}
}
}
- :do {
+ :onerror Err {
/system/scheduler/add start-time=startup name="running-from-backup-partition" \
on-event=(":log warning (\"Running from partition '\" . " . \
"[ /partitions/get [ find where running ] name ] . \"'!\")");
/partitions/save-config-to $FallbackTo;
/system/scheduler/remove "running-from-backup-partition";
$LogPrint info $ScriptName ("Saved configuration to partition '" . $FallbackToName . "'.");
- } on-error={
+ } do={
/system/scheduler/remove [ find where name="running-from-backup-partition" ];
- $LogPrint error $ScriptName ("Failed saving configuration to partition '" . $FallbackToName . "'!");
+ $LogPrint error $ScriptName ("Failed saving configuration to partition '" . \
+ $FallbackToName . "': " . $Err);
:set PackagesUpdateBackupFailure true;
+ :set ExitOK true;
:error false;
}
-} on-error={ }
+} do={
+ :global ExitError; $ExitError $ExitOK [ :jobname ] $Err;
+}