PowershellでのMikrotikルーター構成の集中バックアップ用スクリプト

このメーカーの多数のルーターでインフラストラクチャが大きくなりすぎた後、1つのリポジトリで構成をバックアップするという疑問が生じました。 ftpにアップロードしてルーターで実行されるスクリプトのソリューションに出会いましたが、これはやや不便です。 すべてのルーターで同じようにスクリプトを構成する必要があります。



一時temp.backupファイルでsshコマンドを使用してルーターでバックアップを開始し、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)}
      
      





ルーターのアドレスのリストlist.txtは、スクリプトディレクトリに配置する必要があります。 バックアップは、スクリプトの開始日とともにサブディレクトリのバックアップフォルダに追加されます。 バックアップ自体の名前は、ルーターのアドレスに付けられています。



おそらく誰かが役に立つでしょう。



更新しました。

中間ファイルを使用しない2番目のバージョン



 #  ,       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() } }
      
      






All Articles