A script for centralized backup of configurations of Mikrotik routers on Powershell

After the overgrowth of the infrastructure, a large number of routers of this manufacturer faced the issue of backup configurations in one storage. There were solutions to scripts executed on routers with unloading to ftp, but this is somewhat inconvenient, because requires the configuration of scripts on all routers is identical.
I decided to do this centrally by running a backup on the router using the ssh command to a temporary temp.backup file and uploading it via ftp.
# set the policy right away so that the command is not flushed during the transfer of
Set-ExecutionPolicy remotesigned -scope currentuser
# Install and import the library for ssh
Install-Module -Name Posh-SSH
Import-Module posh-ssh
$ curDir = $ MyInvocation.MyCommand.Definition | split-path -parent
function bkprtr {
param ($ ipaddr)
# here we specify login and password to routers with rights to ftp and ssh.
$ secpasswd = ConvertTo-SecureString "Your_password_to_router" -AsPlainText -Force
$ mycreds = New-Object System.Management.Automation.PSCredential ("your_login_to_router", $ secpasswd)
# Connect to ssh and send the backup command
New-SSHSession -ComputerName $ ipaddr -Credential $ mycreds -Force
Invoke-SshCommand -index 0 -Command "system backup save name temp.backup"
Get-SSHSession | Remove-SshSession;
$ Cdate = get-date -Uformat% Y% m% d
$ rptpath = "$ curDirbackup $ cdate"
# Create a directory by the date
if (! (test-path -path $ rptpath)) {new-item -path $ rptpath -itemtype directory}
# Download the file
wget -Uri "ftp: //$ipaddr/temp.backup" -OutFile "$ rptpath $ ipaddr.backup" -Credential $ mycreds
# process file list.txt line by line
foreach ($ ip in gc $ curDirlist.txt) {bkprtr ($ ip)}

List of addresses of routers list.txt should be put in the directory of the script. Backups will be added to the backup folder in a subdirectory with the start date of the script. The backups are referred to at the router address.
Perhaps someone will come in handy.
+ 0 -

Add comment