📜 ⬆️ ⬇️

Script for centralized backup of configurations of Mikrotik routers on Powershell

After fouling of the infrastructure with a large number of routers of this manufacturer, the question of configuration backups in one storage has come up. Came across solutions of scripts executed on routers with unloading on ftp, but this is somewhat inconvenient, since requires setting up 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 file temp.backup and downloading it via FTP.

#  ,       Set-ExecutionPolicy remotesigned -scope currentuser #     ssh Install-Module -Name Posh-SSH Import-Module posh-ssh $curDir = $MyInvocation.MyCommand.Definition | split-path -parent function bkprtr { param($ipaddr) #          ftp  ssh. $secpasswd = ConvertTo-SecureString "___" -AsPlainText -Force $mycreds = New-Object System.Management.Automation.PSCredential ("___", $secpasswd) $error.clear() #  ssh      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 = "$curDir\backup\$cdate" #    if (!(test-path -path $rptpath)) {new-item -path $rptpath -itemtype directory} #  wget -Uri "ftp://$ipaddr/temp.backup" -OutFile "$rptpath\$ipaddr.backup" -Credential $mycreds #     If ($error.count -gt 0) { get-date | out-file "$curDir\error.log" -append $ipaddr | out-file "$curDir\error.log" -append $error | out-file "$curDir\error.log" -append $error.clear() } } #  list.txt  foreach ($ip in gc $curDir\list.txt ){bkprtr($ip)} 

The list of addresses of the list.txt routers should be placed in the script directory. Backups will be added to the backup folder in a subdirectory with the launch date of the script. The backups themselves are named by the address of the router.

Perhaps someone will come in handy.
')
upd.
second version without intermediate files

 #  ,       Set-ExecutionPolicy remotesigned -scope currentuser #     ssh Install-Module -Name Posh-SSH Import-Module posh-ssh $curDir = $MyInvocation.MyCommand.Definition | split-path -parent function bkprtr { param($ipaddr) #          ftp  ssh. $secpasswd = ConvertTo-SecureString "___" -AsPlainText -Force $mycreds = New-Object System.Management.Automation.PSCredential ("___+ct", $secpasswd) $error.clear() $Cdate=get-date -Uformat %Y%m%d $rptpath = "$curDir\backup\$cdate" #    if (!(test-path -path $rptpath)) {new-item -path $rptpath -itemtype directory} #  #  ssh      $sshsession=New-SSHSession -ComputerName $ipaddr -Credential $mycreds -Force $ssh = $sshSession | New-SSHShellStream start-sleep -s 5 $ssh.read() | out-null $ssh.WriteLine( "export" ) start-sleep -s 15 $ssh.read() | sc "$rptpath\$ipaddr.backup" $tfileproc=gc "$rptpath\$ipaddr.backup" $tfileproc[2..($tfileproc.count-2)]|sc "$rptpath\$ipaddr.backup" Get-SSHSession | Remove-SshSession; #     If ($error.count -gt 0) { get-date | out-file "$curDir\error.log" -append $ipaddr | out-file "$curDir\error.log" -append $error | out-file "$curDir\error.log" -append $error.clear() } } 

Source: https://habr.com/ru/post/351108/


All Articles