i want to copy daily the config from the primary device to a standby device, which is connected only with a dedicated ethernet port to the primary.
no active or automatic failover, only manual plug the main uplinks in case of an error onto the second device, with already running the latest config from the primary.
Hardware is the same, perhaps binary backups can to the job as well. (What is with the different MAC from Ports?)
i want to share my ready to use script. It’s a bit quick and dirty, but tested successfully
### Infos
# be sure that the firewall didn't block ftp/ssh to the device! (Add Port to OOB Interface Group)
# run the script with the scheduler
# Connect SFP+12 on both Devices
# Admin User on Primary = primary
# Admin User on Standby = standby
# Set strong Passwords
# Devices should have a similar or the same RouterOS Version
# Tested on RouterOS 7.14.3
#
# Initial Setup on the Primary Device
# Create SSH Keys on the Primary Device
## /ip ssh export-host-key key-file-prefix=primary
## /user/ssh-keys/private/import user=primary private-key-file=primary_rsa.pem
##
## Add Scheduler on primary (Sync every day on 03 am )
## /system scheduler add interval=1d name=update-standby-device on-event="/system/script/run update-standby-device" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=2024-01-01 start-time=03:00:00
## Save this Script as update-standby-device
#
# Setup the initial User on the Standby Device
## /user/add group=full name=standby password=secure
# Setup the Standby Interface IP
## /ip/address/add address=192.168.127.2/24 interface=sfp-sfpplus12
#
# Script:
# copy Public RSA Key to Standby Device
/tool/fetch address=192.168.127.2 src-path=primary_rsa.pem.pub user=standby mode=ftp password=secure dst-path=primary_rsa.pem.pub upload=yes
# create Config to Import SSH Key for user standby, to run ssh-exec later in this script
/file print file="rsa2standby.rsc"
# first remove all old public keys for the user standby
/file set "rsa2standby.rsc" contents="/user/ssh-keys/remove numbers=[find where username = standby ]\n/user/ssh-keys/import public-key-file=primary_rsa.pem.pub user=standby\n"
# copy an run automatic import with .auto.rsc
/tool/fetch address=192.168.127.2 src-path=rsa2standby.rsc.txt user=standby mode=ftp password=secure dst-path=rsa2standby.auto.rsc upload=yes
/log info "Standby public RSA Key imported";
# create the export from the primary device
:local configFileName "standby.rsc"
/export file=$configFileName show-sensitive terse
:delay 2
# modify the config, to have still access to the standby device after import primary config
#
:local configContent [/file get $configFileName contents]
:local add1 "\n/ip/address/set [find where interface =sfp-sfpplus12 ] address=192.168.127.2/24"
:local add2 "\n/system/identity/set name=router-standby"
:local add3 "\n/user/add group=full name=standby password=secure"
:local add4 "\n/user set [ find name=admin ] disabled=yes"
:local addlast "\n"
# build the config
/file set $configFileName contents=($configContent . $add1 . $add2 . $add3 . $add4 . $addlast)
# upload the config to the standby device with ftp
/tool/fetch address=192.168.127.2 src-path=standby.rsc user=standby mode=ftp password=secure dst-path=standby.rsc upload=yes
:delay 2
# run via ssh-exec on the remote device the reset and import
/system ssh-exec address=192.168.127.2 user=standby command="system/reset-configuration run-after-reset=standby.rsc"
:delay 2
/log info "Standby Config updated, Standby Device rebooting";
Until everything remains within 4096 bytes[1] you are fine, the issues may come with larger configurations.
Semi-random idea, but maybe you want to reset to the exported configuration without any changes to it and put a script to run at next reboot that sets that IP (and the other changes)?
I.e. you reset the configuration, import the exported configuration from the other router, then reboot and have a script running once for correcting the IP(s) re-configured for the “spare” router?
This would imply two reboots, but I would be not so sure about the second reboot (after the configuration has been reset to standby.rsc) not being anyway a good idea.
[1] as msatter noted below, you can go up to 32768 and even much more using chunks on very recent Ros7 version (7.15)
I have seen it, but it seems to me that it only moves the limit from 4096 to 32768 (for “simple” commands), or you need the (more complex) “multi-chunk” approach discussed in that topic.
My semi-random proposal was to not read the file at all, the limit is moved to whatever the export or fetch limits (if any) are.
I believe (maybe wrongly or being over-cautious) that a second reboot is needed anyway, after having reset configuration, to make sure that all settings in the restored configuration are taken (while the small modification needed should need not a further reboot and take effect immediately).
In a perfect world there would be some sort of copy or dd or cat command operating at file level needing not to read the file to memory and re-save it.
bugfix #1, i have to disable the scheduler and/or the script on the standby device…
it’s not that important, because the wrong rsa keys and IPs a login from the standby to primary is not possible.
Still in this case, there is no actual need to read a file (or parse its contents), only to concatenate two files, in Linux there are a few commands that can do that, if Ros were based on Linux …
No, wait …