Merge branch 'backup-partition' into next

This commit is contained in:
Christian Hesse 2024-05-23 12:52:33 +02:00
commit 96ea39b7cd
5 changed files with 66 additions and 4 deletions

View file

@ -15,10 +15,29 @@
:do { :do {
:local ScriptName [ :jobname ]; :local ScriptName [ :jobname ];
:global BackupPartitionCopyBeforeFeatureUpdate;
:global PackagesUpdateBackupFailure; :global PackagesUpdateBackupFailure;
:global LogPrint; :global LogPrint;
:global ScriptFromTerminal;
:global ScriptLock; :global ScriptLock;
:global VersionToNum;
:local CopyTo do={
:local ScriptName [ :tostr $1 ];
:local FallbackTo [ :tostr $2 ];
:global LogPrint;
:do {
/partitions/copy-to $FallbackTo;
$LogPrint info $ScriptName ("Copied RouterOS to partition '" . $FallbackTo . "'.");
:return true;
} on-error={
$LogPrint error $ScriptName ("Failed copying RouterOS to partition '" . $FallbackTo . "'!");
:return false;
}
}
:if ([ $ScriptLock $ScriptName ] = false) do={ :if ([ $ScriptLock $ScriptName ] = false) do={
:set PackagesUpdateBackupFailure true; :set PackagesUpdateBackupFailure true;
@ -41,6 +60,29 @@
:local FallbackTo [ /partitions/get $ActiveRunning fallback-to ]; :local FallbackTo [ /partitions/get $ActiveRunning fallback-to ];
:if ([ /partitions/get $ActiveRunning version ] != [ /partitions/get $FallbackTo version]) do={
:if ([ $ScriptFromTerminal $ScriptName ] = true) do={
:put ("The partitions have different RouterOS versions. Copy over to '" . $FallbackTo . "'? [y/N]");
:if (([ /terminal/inkey timeout=60 ] % 32) = 25) do={
:if ([ $CopyTo $ScriptName $FallbackTo ] = false) do={
:set PackagesUpdateBackupFailure true;
:error false;
}
}
} else={
:local Update [ /system/package/update/get ];
:local NumInstalled [ $VersionToNum ($Update->"installed-version") ];
:local NumLatest [ $VersionToNum ($Update->"latest-version") ];
:if ($BackupPartitionCopyBeforeFeatureUpdate = true && $NumLatest > 0 && \
($NumInstalled & 0xffff0000) != ($NumLatest & 0xffff0000)) do={
:if ([ $CopyTo $ScriptName $FallbackTo ] = false) do={
:set PackagesUpdateBackupFailure true;
:error false;
}
}
}
}
:do { :do {
/system/scheduler/add start-time=startup name="running-from-backup-partition" \ /system/scheduler/add start-time=startup name="running-from-backup-partition" \
on-event=(":log warning (\"Running from partition '\" . " . \ on-event=(":log warning (\"Running from partition '\" . " . \

View file

@ -18,6 +18,8 @@ Description
This script saves the current configuration to fallback This script saves the current configuration to fallback
[partition](https://wiki.mikrotik.com/wiki/Manual:Partitions). [partition](https://wiki.mikrotik.com/wiki/Manual:Partitions).
It can also copy-over the RouterOS installation when run interactively
or just before a feature update.
For this to work you need a device with sufficient flash storage that is For this to work you need a device with sufficient flash storage that is
properly partitioned. properly partitioned.
@ -26,9 +28,9 @@ To make you aware of a possible issue a scheduler logging a warning is
added in the backup partition's configuration. You may want to use added in the backup partition's configuration. You may want to use
[log-forward](log-forward.md) to be notified. [log-forward](log-forward.md) to be notified.
> ⚠️ **Warning**: Only the configuration is saved to backup partition. > ⚠️ **Warning**: By default only the configuration is saved to backup
> Every now and then you should copy your installation over for a recent > partition. Every now and then you should copy your installation over
> RouterOS version! > for a recent RouterOS version! See below for options.
Requirements and installation Requirements and installation
----------------------------- -----------------------------
@ -37,6 +39,18 @@ Just install the script:
$ScriptInstallUpdate backup-partition; $ScriptInstallUpdate backup-partition;
Configuration
-------------
The configuration goes to `global-config-overlay`, the only parameter is:
* `BackupPartitionCopyBeforeFeatureUpdate`: copy-over the RouterOS
installation when a feature update is pending
> **Info**: Copy relevant configuration from
> [`global-config`](../global-config.rsc) (the one without `-overlay`) to
> your local `global-config-overlay` and modify it to your specific needs.
Usage and invocation Usage and invocation
-------------------- --------------------
@ -44,6 +58,9 @@ Just run the script:
/system/script/run backup-partition; /system/script/run backup-partition;
When run interactively from terminal it supports to copy-over the RouterOS
installation when versions differ.
Creating a scheduler may be an option: Creating a scheduler may be an option:
/system/scheduler/add interval=1w name=backup-partition on-event="/system/script/run backup-partition;" start-time=09:30:00; /system/scheduler/add interval=1w name=backup-partition on-event="/system/script/run backup-partition;" start-time=09:30:00;

View file

@ -85,6 +85,8 @@
:global BackupUploadUrl "sftp://example.com/backup/"; :global BackupUploadUrl "sftp://example.com/backup/";
:global BackupUploadUser "mikrotik"; :global BackupUploadUser "mikrotik";
:global BackupUploadPass "v3ry-s3cr3t"; :global BackupUploadPass "v3ry-s3cr3t";
# Copy the RouterOS installation to backup partition before feature update.
:global BackupPartitionCopyBeforeFeatureUpdate false;
# This defines the settings for firewall address-lists (fw-addr-lists). # This defines the settings for firewall address-lists (fw-addr-lists).
:global FwAddrLists { :global FwAddrLists {

View file

@ -12,7 +12,7 @@
:local ScriptName [ :jobname ]; :local ScriptName [ :jobname ];
# expected configuration version # expected configuration version
:global ExpectedConfigVersion 128; :global ExpectedConfigVersion 129;
# global variables not to be changed by user # global variables not to be changed by user
:global GlobalFunctionsReady false; :global GlobalFunctionsReady false;

View file

@ -53,6 +53,7 @@
126="Made 'telegram-chat' capable of handling large command output. Telegram messages still limit the size, so it is truncated now."; 126="Made 'telegram-chat' capable of handling large command output. Telegram messages still limit the size, so it is truncated now.";
127="Added support for authentication to Ntfy notification module."; 127="Added support for authentication to Ntfy notification module.";
128="Added another list from blocklist.de to default configuration for 'fw-addr-lists'."; 128="Added another list from blocklist.de to default configuration for 'fw-addr-lists'.";
129="Extended 'backup-partition' to support RouterOS copy-over - interactively or before feature update.";
}; };
# Migration steps to be applied on script updates # Migration steps to be applied on script updates