2023-03-07 09:57:42 +00:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: backup-partition
|
2024-01-01 14:25:25 +00:00
|
|
|
# Copyright (c) 2022-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=70
|
2023-11-15 20:31:18 +00:00
|
|
|
# requires RouterOS, version=7.12
|
2023-03-07 09:57:42 +00:00
|
|
|
#
|
|
|
|
# save configuration to fallback partition
|
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/backup-partition.md
|
|
|
|
|
|
|
|
:global GlobalFunctionsReady;
|
|
|
|
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
|
|
|
|
2024-03-06 14:28:55 +00:00
|
|
|
:do {
|
|
|
|
:local ScriptName [ :jobname ];
|
2023-06-12 17:02:41 +00:00
|
|
|
|
2024-03-08 08:26:39 +00:00
|
|
|
:global PackagesUpdateBackupFailure;
|
|
|
|
|
2024-03-04 12:48:00 +00:00
|
|
|
:global LogPrintExit2;
|
|
|
|
:global ScriptLock;
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2024-03-05 15:12:36 +00:00
|
|
|
:if ([ $ScriptLock $ScriptName ] = false) do={
|
2024-03-08 08:26:39 +00:00
|
|
|
:set PackagesUpdateBackupFailure true;
|
2024-03-06 14:28:55 +00:00
|
|
|
:error false;
|
2024-03-05 15:12:36 +00:00
|
|
|
}
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2024-03-04 12:48:00 +00:00
|
|
|
:if ([ :len [ /partitions/find ] ] < 2) do={
|
2024-03-08 08:26:39 +00:00
|
|
|
$LogPrintExit2 error $ScriptName ("Device does not have a fallback partition.") false;
|
|
|
|
:set PackagesUpdateBackupFailure true;
|
|
|
|
:error false;
|
2024-03-04 12:48:00 +00:00
|
|
|
}
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2024-03-04 12:48:00 +00:00
|
|
|
:local ActiveRunning [ /partitions/find where active running ];
|
|
|
|
|
|
|
|
:if ([ :len $ActiveRunning ] < 1) do={
|
2024-03-08 08:26:39 +00:00
|
|
|
$LogPrintExit2 error $ScriptName ("Device is not running from active partition.") false;
|
|
|
|
:set PackagesUpdateBackupFailure true;
|
|
|
|
:error false;
|
2024-03-04 12:48:00 +00:00
|
|
|
}
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2024-03-04 12:48:00 +00:00
|
|
|
:local FallbackTo [ /partitions/get $ActiveRunning fallback-to ];
|
|
|
|
|
|
|
|
:do {
|
|
|
|
/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";
|
|
|
|
$LogPrintExit2 info $ScriptName ("Saved configuration to partition '" . \
|
|
|
|
$FallbackTo . "'.") false;
|
|
|
|
} on-error={
|
|
|
|
/system/scheduler/remove [ find where name="running-from-backup-partition" ];
|
|
|
|
$LogPrintExit2 error $ScriptName ("Failed saving configuration to partition '" . \
|
2024-03-08 08:26:39 +00:00
|
|
|
$FallbackTo . "'!") false;
|
|
|
|
:set PackagesUpdateBackupFailure true;
|
|
|
|
:error false;
|
2024-03-04 12:48:00 +00:00
|
|
|
}
|
2024-03-06 14:28:55 +00:00
|
|
|
} on-error={ }
|