Rexxer

Some tips for me and other

Powershell + Mikrotik backups

Source: https://habrahabr.ru/post/351108/

#зададим политику сразу, чтобы не вспминать команду при переносе 
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)

#Подключение по 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


} 

#обрабатываем файл list.txt построчно
foreach ($ip in gc $curDir\list.txt ){bkprtr($ip)}

Comments are currently closed.