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
|
|
|
|
|
2023-11-15 20:31:18 +00:00
|
|
|
:local 0 [ :jobname ];
|
2023-03-07 09:57:42 +00:00
|
|
|
:global GlobalFunctionsReady;
|
|
|
|
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
|
|
|
|
|
|
|
:global LogPrintExit2;
|
2023-06-12 17:02:41 +00:00
|
|
|
:global ScriptLock;
|
|
|
|
|
|
|
|
$ScriptLock $0;
|
2023-03-07 09:57:42 +00:00
|
|
|
|
|
|
|
:if ([ :len [ /partitions/find ] ] < 2) do={
|
|
|
|
$LogPrintExit2 error $0 ("Device does not have a fallback partition.") true;
|
|
|
|
}
|
|
|
|
|
|
|
|
:local ActiveRunning [ /partitions/find where active running ];
|
|
|
|
|
|
|
|
:if ([ :len $ActiveRunning ] < 1) do={
|
|
|
|
$LogPrintExit2 error $0 ("Device is not running from active partition.") true;
|
|
|
|
}
|
|
|
|
|
2024-02-02 09:40:59 +00:00
|
|
|
:local FallbackTo [ /partitions/get $ActiveRunning fallback-to ];
|
2023-03-07 09:57:42 +00:00
|
|
|
|
|
|
|
:do {
|
2024-02-02 09:53:31 +00:00
|
|
|
/system/scheduler/add start-time=startup name="running-from-backup-partition" \
|
|
|
|
on-event=(":log warning (\"Running from partition '\" . " . \
|
|
|
|
"[ /partitions/get [ find where running ] name ] . \"'!\")");
|
2024-02-02 09:40:59 +00:00
|
|
|
/partitions/save-config-to $FallbackTo;
|
2024-02-02 09:53:31 +00:00
|
|
|
/system/scheduler/remove "running-from-backup-partition";
|
2023-03-07 09:57:42 +00:00
|
|
|
$LogPrintExit2 info $0 ("Saved configuration to partition '" . \
|
2024-02-02 09:40:59 +00:00
|
|
|
$FallbackTo . "'.") false;
|
2023-03-07 09:57:42 +00:00
|
|
|
} on-error={
|
2024-02-02 10:07:36 +00:00
|
|
|
/system/scheduler/remove [ find where name="running-from-backup-partition" ];
|
2023-03-07 09:57:42 +00:00
|
|
|
$LogPrintExit2 error $0 ("Failed saving configuration to partition '" . \
|
2024-02-02 09:40:59 +00:00
|
|
|
$FallbackTo . "'!") true;
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|