From 51331ea2acd5f5563ed3641090bbb7f2104d3e96 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 22 May 2024 09:26:13 +0200 Subject: backup-partition: support RouterOS copy-over... ... when run interactively from CLI. --- backup-partition.rsc | 17 +++++++++++++++++ doc/backup-partition.md | 11 ++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/backup-partition.rsc b/backup-partition.rsc index fc186c0..9eeb1ae 100644 --- a/backup-partition.rsc +++ b/backup-partition.rsc @@ -18,6 +18,7 @@ :global PackagesUpdateBackupFailure; :global LogPrint; + :global ScriptFromTerminal; :global ScriptLock; :if ([ $ScriptLock $ScriptName ] = false) do={ @@ -41,6 +42,22 @@ :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={ + :do { + /partitions/copy-to $FallbackTo; + $LogPrint info $ScriptName ("Copied RouterOS to partition '" . $FallbackTo . "'."); + } on-error={ + $LogPrint error $ScriptName ("Failed copying RouterOS to partition '" . $FallbackTo . "'!"); + :set PackagesUpdateBackupFailure true; + :error false; + } + } + } + } + :do { /system/scheduler/add start-time=startup name="running-from-backup-partition" \ on-event=(":log warning (\"Running from partition '\" . " . \ diff --git a/doc/backup-partition.md b/doc/backup-partition.md index ba20657..8996203 100644 --- a/doc/backup-partition.md +++ b/doc/backup-partition.md @@ -18,6 +18,7 @@ Description This script saves the current configuration to fallback [partition](https://wiki.mikrotik.com/wiki/Manual:Partitions). +It can also copy-over the RouterOS installation when run interactively. For this to work you need a device with sufficient flash storage that is properly partitioned. @@ -26,9 +27,10 @@ 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 [log-forward](log-forward.md) to be notified. -> ⚠️ **Warning**: Only the configuration is saved to backup partition. -> Every now and then you should copy your installation over for a recent -> RouterOS version! +> ⚠️ **Warning**: By default only the configuration is saved to backup +> partition. Every now and then you should copy your installation over +> for a recent RouterOS version! For that run the script from terminal +> manually. Requirements and installation ----------------------------- @@ -44,6 +46,9 @@ Just run the script: /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: /system/scheduler/add interval=1w name=backup-partition on-event="/system/script/run backup-partition;" start-time=09:30:00; -- cgit v1.2.3-70-g09d2 From 6cbe13a135fe39c097b6a7078506d99eedde2e06 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 22 May 2024 11:58:26 +0200 Subject: backup-partition: move code to a local function --- backup-partition.rsc | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/backup-partition.rsc b/backup-partition.rsc index 9eeb1ae..a72dd0b 100644 --- a/backup-partition.rsc +++ b/backup-partition.rsc @@ -21,6 +21,22 @@ :global ScriptFromTerminal; :global ScriptLock; + :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={ :set PackagesUpdateBackupFailure true; :error false; @@ -46,11 +62,7 @@ :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={ - :do { - /partitions/copy-to $FallbackTo; - $LogPrint info $ScriptName ("Copied RouterOS to partition '" . $FallbackTo . "'."); - } on-error={ - $LogPrint error $ScriptName ("Failed copying RouterOS to partition '" . $FallbackTo . "'!"); + :if ([ $CopyTo $ScriptName $FallbackTo ] = false) do={ :set PackagesUpdateBackupFailure true; :error false; } -- cgit v1.2.3-70-g09d2 From affa11816182fa2bff454b3e82df3166c6cdd744 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 22 May 2024 12:03:53 +0200 Subject: backup-partition: support copy before feature update --- backup-partition.rsc | 13 +++++++++++++ doc/backup-partition.md | 18 +++++++++++++++--- global-config.rsc | 2 ++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/backup-partition.rsc b/backup-partition.rsc index a72dd0b..3c883f7 100644 --- a/backup-partition.rsc +++ b/backup-partition.rsc @@ -15,11 +15,13 @@ :do { :local ScriptName [ :jobname ]; + :global BackupPartitionCopyBeforeFeatureUpdate; :global PackagesUpdateBackupFailure; :global LogPrint; :global ScriptFromTerminal; :global ScriptLock; + :global VersionToNum; :local CopyTo do={ :local ScriptName [ :tostr $1 ]; @@ -67,6 +69,17 @@ :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; + } + } } } diff --git a/doc/backup-partition.md b/doc/backup-partition.md index 8996203..1cef2c9 100644 --- a/doc/backup-partition.md +++ b/doc/backup-partition.md @@ -18,7 +18,8 @@ Description This script saves the current configuration to fallback [partition](https://wiki.mikrotik.com/wiki/Manual:Partitions). -It can also copy-over the RouterOS installation when run interactively. +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 properly partitioned. @@ -29,8 +30,7 @@ added in the backup partition's configuration. You may want to use > ⚠️ **Warning**: By default only the configuration is saved to backup > partition. Every now and then you should copy your installation over -> for a recent RouterOS version! For that run the script from terminal -> manually. +> for a recent RouterOS version! See below for options. Requirements and installation ----------------------------- @@ -39,6 +39,18 @@ Just install the script: $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 -------------------- diff --git a/global-config.rsc b/global-config.rsc index 646eddb..8a42b81 100644 --- a/global-config.rsc +++ b/global-config.rsc @@ -85,6 +85,8 @@ :global BackupUploadUrl "sftp://example.com/backup/"; :global BackupUploadUser "mikrotik"; :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). :global FwAddrLists { -- cgit v1.2.3-70-g09d2 From 33a495beb76f438ba79e56ab8e4872d2df5c4da8 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 23 May 2024 12:18:13 +0200 Subject: backup-partition: news on support for copy-over --- global-functions.rsc | 2 +- news-and-changes.rsc | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/global-functions.rsc b/global-functions.rsc index 3800ad5..96ed71d 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -12,7 +12,7 @@ :local ScriptName [ :jobname ]; # expected configuration version -:global ExpectedConfigVersion 128; +:global ExpectedConfigVersion 129; # global variables not to be changed by user :global GlobalFunctionsReady false; diff --git a/news-and-changes.rsc b/news-and-changes.rsc index 13a358f..b20bbaf 100644 --- a/news-and-changes.rsc +++ b/news-and-changes.rsc @@ -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."; 127="Added support for authentication to Ntfy notification module."; 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 -- cgit v1.2.3-70-g09d2