different:powershell_ssh_scp
Различия
Показаны различия между двумя версиями страницы.
| Предыдущая версия справа и слеваПредыдущая версияСледующая версия | Предыдущая версия | ||
| different:powershell_ssh_scp [2024/08/19 14:21] – root | different:powershell_ssh_scp [2024/12/21 19:00] (текущий) – внешнее изменение 127.0.0.1 | ||
|---|---|---|---|
| Строка 1: | Строка 1: | ||
| ====== SCP/SSH Powershell password ====== | ====== SCP/SSH Powershell password ====== | ||
| - | < | + | ====== Установить модуль Posh-SSH ====== |
| + | < | ||
| Import-Module PowershellGet | Import-Module PowershellGet | ||
| Set-ExecutionPolicy -ExecutionPolicy RemoteSigned #Yes | Set-ExecutionPolicy -ExecutionPolicy RemoteSigned #Yes | ||
| Install-Module Posh-SSH #Yes | Install-Module Posh-SSH #Yes | ||
| </ | </ | ||
| - | < | + | |
| + | ====== Скрипт бэкапа ====== | ||
| + | < | ||
| Import-Module Posh-SSH | Import-Module Posh-SSH | ||
| + | |||
| #list of adcs | #list of adcs | ||
| $servers = @() | $servers = @() | ||
| $servers += @{name = ' | $servers += @{name = ' | ||
| $servers += @{name = ' | $servers += @{name = ' | ||
| - | + | ||
| - | ##to make cred file | + | |
| - | #$cred = Get-Credential -Credential $nsUser | + | |
| - | # | + | |
| - | + | ||
| ##touch this: | ##touch this: | ||
| #username: | #username: | ||
| $nsUser = " | $nsUser = " | ||
| + | |||
| #path to file contains encrypted pass: | #path to file contains encrypted pass: | ||
| $credPath = " | $credPath = " | ||
| - | + | ||
| - | + | ||
| + | |||
| $backupStorePath = " | $backupStorePath = " | ||
| $doFullBackup = $true #or $false $true - make full, $false - make basic | $doFullBackup = $true #or $false $true - make full, $false - make basic | ||
| $eraseBackup = $false #or $true $true - erase backup from adc after transfer, $false - leave backup on adc | $eraseBackup = $false #or $true $true - erase backup from adc after transfer, $false - leave backup on adc | ||
| - | + | ||
| - | + | ||
| - | ##to make cred file uncomment two strings below, after take it back: | + | ##ask for creds if password |
| - | #$cred = Get-Credential -Credential $nsUser | + | if ($(Test-Path -Path $credPath) -ne $true) { |
| - | #$cred.Password | ConvertFrom-SecureString | Set-Content $credPath | + | |
| - | + | $cred.Password | ConvertFrom-SecureString | Set-Content $credPath | |
| + | } | ||
| + | |||
| #do not touch that: | #do not touch that: | ||
| $backup_level = " | $backup_level = " | ||
| $encryptedPass = Get-Content $credPath | ConvertTo-SecureString | $encryptedPass = Get-Content $credPath | ConvertTo-SecureString | ||
| $cred = New-Object System.Management.Automation.PsCredential($nsUser, | $cred = New-Object System.Management.Automation.PsCredential($nsUser, | ||
| + | |||
| function remove_backup { | function remove_backup { | ||
| param ( | param ( | ||
| Строка 49: | Строка 50: | ||
| $sshCommand = "rm system backup " + $backupName +" | $sshCommand = "rm system backup " + $backupName +" | ||
| Write-Host($sshCommand) | Write-Host($sshCommand) | ||
| - | $sshSession = New-SSHSession -ComputerName $server.ip -Credential $cred -AcceptKey | + | $sshSession = New-SSHSession -ComputerName $server.ip -Credential $cred -AcceptKey |
| $result = Invoke-SSHCommand -Command $sshCommand -SSHSession $sshSession | $result = Invoke-SSHCommand -Command $sshCommand -SSHSession $sshSession | ||
| if ($result.ExitStatus -eq 0) { | if ($result.ExitStatus -eq 0) { | ||
| Строка 59: | Строка 60: | ||
| Remove-SSHSession -SessionId $sshSession.SessionId | Remove-SSHSession -SessionId $sshSession.SessionId | ||
| } | } | ||
| + | |||
| function get_backup { | function get_backup { | ||
| param ( | param ( | ||
| Строка 79: | Строка 80: | ||
| return $result | return $result | ||
| } | } | ||
| + | |||
| function create_backup { | function create_backup { | ||
| param ( | param ( | ||
| Строка 86: | Строка 87: | ||
| $erase = $false | $erase = $false | ||
| ) | ) | ||
| - | + | ||
| - | $backup_name_pattern = $server.name + " | + | $backup_name_pattern = $server.name+ " |
| Write-Host($server.name, | Write-Host($server.name, | ||
| - | $sshCommand | + | $sshCommands = @() |
| - | | + | $sshCommands += "set HA node -haSync DISABLED -haProp DISABLED" |
| + | $sshCommands += "save ns config" | ||
| + | $sshCommands += " | ||
| + | | ||
| + | | ||
| $sshSession = New-SSHSession -ComputerName $server.ip -Credential $cred -AcceptKey | $sshSession = New-SSHSession -ComputerName $server.ip -Credential $cred -AcceptKey | ||
| - | $result = Invoke-SSHCommand -Command $sshCommand | + | |
| + | Invoke-SSHCommand -Command $sshCommands[1] -SSHSession $sshSession | ||
| + | | ||
| if ($result.ExitStatus -eq 0) { | if ($result.ExitStatus -eq 0) { | ||
| $result = get_backup -server $server -backupName $backup_name_pattern -destinationPath $backupStorePath -erase $erase | $result = get_backup -server $server -backupName $backup_name_pattern -destinationPath $backupStorePath -erase $erase | ||
| Строка 99: | Строка 106: | ||
| Write-Host($result.Host, | Write-Host($result.Host, | ||
| } | } | ||
| + | Invoke-SSHCommand -Command $sshCommands[3] -SSHSession $sshSession - | ||
| + | Invoke-SSHCommand -Command $sshCommands[4] -SSHSession $sshSession | ||
| Remove-SSHSession -SessionId $sshSession.SessionId | Remove-SSHSession -SessionId $sshSession.SessionId | ||
| } | } | ||
| - | + | ||
| - | + | ||
| + | |||
| foreach ($server in $servers) { | foreach ($server in $servers) { | ||
| create_backup -server $server -doFullBackup $doFullBackup -erase $eraseBackup | create_backup -server $server -doFullBackup $doFullBackup -erase $eraseBackup | ||
| } | } | ||
| </ | </ | ||
different/powershell_ssh_scp.1724066518.txt.gz · Последнее изменение: 2024/12/21 19:00 (внешнее изменение)
