Guest User

Untitled

a guest
Nov 5th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. Start-Transcript -Path "C:\Scripts\Logs\Backup-$((Get-Date).ToString('yyyy-MM-dd-hh-mm')).log"
  2.  
  3. #region: Log Rotate
  4. $LogPath = "C:\Scripts\Logs\*"
  5. $LogFiles = "*.log"
  6. [int] $LogRetention = "-14" # in Days
  7. Get-ChildItem $LogPath -Include $LogFiles -Recurse | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays($LogRetention)} | Remove-Item
  8. #endregion
  9.  
  10. #region: Import Module
  11. Import-Module VMware.VimAutomation.Core, VMware.VimAutomation.Cis.Core -Force
  12. Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false -DefaultVIServerMode Single -Confirm:$false | Out-Null
  13. #endregion
  14.  
  15. #region: Declarations
  16. ## vCenter Connection
  17. $EncryptedVICredentialsFile = "C:\Scripts\Secure\EncryptedVICredentials.clixml"
  18. $EncryptedVICredentials = Import-CliXml $EncryptedVICredentialsFile
  19. [String]$VIServer = "<vCenter Server>"
  20. ## Backup Mode
  21. [switch]$FullBackup = $true
  22. [switch]$CommonBackup = $false
  23. ## Backup File Password
  24. [VMware.VimAutomation.Cis.Core.Types.V1.Secret]$BackupFilePassword = "<password>"
  25. ## Backup Destination
  26. $EncryptedSCPCredentialsFile = "C:\Scripts\Secure\EncryptedSCPCredentials.clixml"
  27. $EncryptedSCPCredentials = Import-CliXml $EncryptedSCPCredentialsFile
  28. $LocationType = "SCP"
  29. $location = "<SCP Sevrer>/FTPData/fct_vc_vcp001/upload/$VIServer-$((Get-Date).ToString('yyyy-MM-dd-hh-mm'))"
  30. $Comment = "Automatic Backup job"
  31. $LocationUser = $EncryptedSCPCredentials.UserName
  32. [VMware.VimAutomation.Cis.Core.Types.V1.Secret]$locationPassword = $EncryptedSCPCredentials.GetNetworkCredential().Password
  33. #endregion
  34.  
  35. #region: DO NOT TOUCH
  36. try {
  37. $CisServerConnection = Connect-CisServer -Server $VIServer -Credential $EncryptedVICredentials
  38. }catch {throw "vCenter Connection Failed" }
  39. if ($FullBackup) {$parts = @("common","seat")}
  40. if ($CommonBackup) {$parts = @("common")}
  41.  
  42. $BackupAPI = Get-CisService com.vmware.appliance.recovery.backup.job
  43. $CreateSpec = $BackupAPI.Help.create.piece.Create()
  44. $CreateSpec.parts = $parts
  45. $CreateSpec.backup_password = $BackupFilePassword
  46. $CreateSpec.location_type = $LocationType
  47. $CreateSpec.location = $Location
  48. $CreateSpec.location_user = $LocationUser
  49. $CreateSpec.location_password = $LocationPassword
  50. $CreateSpec.comment = $Comment
  51. try {
  52. $BackupJob = $BackupAPI.create($CreateSpec)
  53. }
  54. catch { throw $_.Exception.Message }
  55. do {
  56. $BackupAPI.get("$($BackupJob.ID)") | Select-Object id, progress, state | Format-Table -AutoSize
  57. start-sleep -seconds 5
  58. } until ($BackupAPI.get("$($BackupJob.ID)").progress -eq 100 -or $BackupAPI.get("$($BackupJob.ID)").state -ne "INPROGRESS")
  59.  
  60. $BackupAPI.get("$($BackupJob.ID)") | Select-Object id, progress, state | Format-Table -AutoSize
  61.  
  62. if ($BackupAPI.get("$($BackupJob.ID)").state -ne "SUCCEEDED") {
  63. Throw "VCSA Backup Failed"
  64. }
  65. #endregion
  66.  
  67. Stop-Transcript
Add Comment
Please, Sign In to add comment