Инструменты пользователя

Инструменты сайта


different:powershell_ssh_scp

Различия

Показаны различия между двумя версиями страницы.

Ссылка на это сравнение

Следующая версия
Предыдущая версия
different:powershell_ssh_scp [2024/08/19 12:02] – создано rootdifferent:powershell_ssh_scp [2024/12/21 19:00] (текущий) – внешнее изменение 127.0.0.1
Строка 1: Строка 1:
 ====== SCP/SSH Powershell password ====== ====== SCP/SSH Powershell password ======
-<code=powershell | download>+====== Установить модуль Posh-SSH ====== 
 +<code=powershell>
 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
 </code> </code>
-<code=powershell | download>+ 
 +====== Скрипт бэкапа ====== 
 +<code=powershell>
 Import-Module Posh-SSH Import-Module Posh-SSH
 +  
 +#list of adcs
 $servers = @() $servers = @()
-$servers += @{ +$servers += @{name = 'adc01'ip = '192.168.124.11'
-    name = 'adc01' +$servers +@{name = 'adc02'; ip = '192.168.124.12'} 
-    ip = '192.168.124.11' +  
-+  
- +  
- +##touch this: 
-#$cred Get-Credential -Credential nsroot +#username:
-#$cred.Password | ConvertFrom-SecureString | Set-Content ./nsroot.txt +
- +
 $nsUser = "nsroot" $nsUser = "nsroot"
-$encryptedPass Get-Content ./nsroot.txt | ConvertTo-SecureString +  
-$cred = New-Object System.Management.Automation.PsCredential($nsUser, $encryptedPass) +#path to file contains encrypted pass: 
 +$credPath "./adcpasswd.txt
 +  
 +  
 + 
 $backupStorePath = "./" $backupStorePath = "./"
 +$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
 + 
 + 
 +##ask for creds if password file does not exist
 +if ($(Test-Path -Path $credPath) -ne $true) {
 +    $cred = Get-Credential -Credential $nsUser
 +    $cred.Password | ConvertFrom-SecureString | Set-Content $credPath
 +}
 + 
 +#do not touch that:
 $backup_level = "basic", "full" $backup_level = "basic", "full"
-$doFullBackup = $true #or $false +$encryptedPass Get-Content $credPath | ConvertTo-SecureString 
- +$cred = New-Object System.Management.Automation.PsCredential($nsUser, $encryptedPass) 
- +  
 +function remove_backup { 
 +    param ( 
 +        $server, 
 +        $backupName 
 +    ) 
 +    $sshCommand = "rm system backup " + $backupName +".tgz" 
 +    Write-Host($sshCommand) 
 +    $sshSession = New-SSHSession -ComputerName $server.ip -Credential $cred -AcceptKey -Force 
 +    $result = Invoke-SSHCommand -Command $sshCommand -SSHSession $sshSession 
 +    if ($result.ExitStatus -eq 0) { 
 +            Write-Host("remove: ",$server.name,$backupname) 
 +    } 
 +    else { 
 +        Write-Host($result.Host, $result.Output) 
 +    } 
 +    Remove-SSHSession -SessionId $sshSession.SessionId 
 +} 
 + 
 function get_backup { function get_backup {
     param (     param (
 +        $server,
         $backupName,         $backupName,
-        $destinationPath = "./"+        $destinationPath = "./"
 +        $erase
     )     )
     $backup_file_path = "/var/ns_sys_backup/$backupName.tgz"     $backup_file_path = "/var/ns_sys_backup/$backupName.tgz"
Строка 41: Строка 75:
         -Path $backup_file_path `         -Path $backup_file_path `
         -PathType File `         -PathType File `
-        -Destination $destinationPath+        -Destination $destinationPath 
 +        -Force 
 +    if ($erase -eq $true){remove_backup -server $server -backupName $backupName}
     return $result     return $result
 } }
 + 
 function create_backup { function create_backup {
     param (     param (
         $server,         $server,
-        $doFullBackup = $True+        $doFullBackup = $true, 
 +        $erase = $false
     )     )
- +  
-    $backup_name_pattern = "adc_"$server.name + "_" + $(get-date -format "yyyy_MM_dd"+    $backup_name_pattern = $server.name+ "_" + $backup_level[$doFullBackup]  + "_" + $(get-date -format "yyyy_MM_dd"
-    Write-Host($server.name,$backup_name_pattern,$backup_level[$doFullBackup]) +    Write-Host($server.name,$backup_name_pattern,$backup_level[$doFullBackup], $erase
-    $sshCommand = "create system backup " + $backup_name_pattern +" -level " + $backup_level[$doFullBackup] +    $sshCommands = @() 
-    #Write-Host($sshCommand)+    $sshCommands += "set HA node -haSync DISABLED -haProp DISABLED" #0 
 +    $sshCommands += "save ns config" #1 
 +    $sshCommands += "create system backup " + $backup_name_pattern +" -level " + $backup_level[$doFullBackup] #2 
 +    $sshCommands += "set HA node -haSync ENABLED -haProp ENABLED" #3 
 +    $sshCommands += "save ns config" #4
     $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+    Invoke-SSHCommand -Command $sshCommands[0] -SSHSession $sshSession 
 +    Invoke-SSHCommand -Command $sshCommands[1] -SSHSession $sshSession 
 +    $result = Invoke-SSHCommand -Command $sshCommands[2] -SSHSession $sshSession
     if ($result.ExitStatus -eq 0) {     if ($result.ExitStatus -eq 0) {
-        $result = get_backup -backupName $backup_name_pattern -destinationPath $backupStorePath+        $result = get_backup -server $server -backupName $backup_name_pattern -destinationPath $backupStorePath -erase $erase
     }     }
     else {     else {
         Write-Host($result.Host, $result.Output)         Write-Host($result.Host, $result.Output)
     }     }
 +    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+    create_backup -server $server -doFullBackup $doFullBackup -erase $eraseBackup
 } }
 </code> </code>
different/powershell_ssh_scp.1724058151.txt.gz · Последнее изменение: 2024/12/21 19:00 (внешнее изменение)