Skip to content

Instantly share code, notes, and snippets.

@vMarkusK
Created November 5, 2018 15:55
Show Gist options
  • Save vMarkusK/21a18ae527ee66d8259dbbf79141f1d3 to your computer and use it in GitHub Desktop.
Save vMarkusK/21a18ae527ee66d8259dbbf79141f1d3 to your computer and use it in GitHub Desktop.
Create a VCSA Backup via BackupAPI. Used as a Veeam Pre or Post Script
Start-Transcript -Path "C:\Scripts\Logs\Backup-$((Get-Date).ToString('yyyy-MM-dd-hh-mm')).log"
#region: Log Rotate
$LogPath = "C:\Scripts\Logs\*"
$LogFiles = "*.log"
[int] $LogRetention = "-14" # in Days
Get-ChildItem $LogPath -Include $LogFiles -Recurse | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays($LogRetention)} | Remove-Item
#endregion
#region: Import Module
Import-Module VMware.VimAutomation.Core, VMware.VimAutomation.Cis.Core -Force
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false -DefaultVIServerMode Single -Confirm:$false | Out-Null
#endregion
#region: Declarations
## vCenter Connection
$EncryptedVICredentialsFile = "C:\Scripts\Secure\EncryptedVICredentials.clixml"
$EncryptedVICredentials = Import-CliXml $EncryptedVICredentialsFile
[String]$VIServer = "<vCenter Server>"
## Backup Mode
[switch]$FullBackup = $true
[switch]$CommonBackup = $false
## Backup File Password
[VMware.VimAutomation.Cis.Core.Types.V1.Secret]$BackupFilePassword = "<password>"
## Backup Destination
$EncryptedSCPCredentialsFile = "C:\Scripts\Secure\EncryptedSCPCredentials.clixml"
$EncryptedSCPCredentials = Import-CliXml $EncryptedSCPCredentialsFile
$LocationType = "SCP"
$location = "<SCP Sevrer>/FTPData/fct_vc_vcp001/upload/$VIServer-$((Get-Date).ToString('yyyy-MM-dd-hh-mm'))"
$Comment = "Automatic Backup job"
$LocationUser = $EncryptedSCPCredentials.UserName
[VMware.VimAutomation.Cis.Core.Types.V1.Secret]$locationPassword = $EncryptedSCPCredentials.GetNetworkCredential().Password
#endregion
#region: DO NOT TOUCH
try {
$CisServerConnection = Connect-CisServer -Server $VIServer -Credential $EncryptedVICredentials
}catch {throw "vCenter Connection Failed" }
if ($FullBackup) {$parts = @("common","seat")}
if ($CommonBackup) {$parts = @("common")}
$BackupAPI = Get-CisService com.vmware.appliance.recovery.backup.job
$CreateSpec = $BackupAPI.Help.create.piece.Create()
$CreateSpec.parts = $parts
$CreateSpec.backup_password = $BackupFilePassword
$CreateSpec.location_type = $LocationType
$CreateSpec.location = $Location
$CreateSpec.location_user = $LocationUser
$CreateSpec.location_password = $LocationPassword
$CreateSpec.comment = $Comment
try {
$BackupJob = $BackupAPI.create($CreateSpec)
}
catch { throw $_.Exception.Message }
do {
$BackupAPI.get("$($BackupJob.ID)") | Select-Object id, progress, state | Format-Table -AutoSize
start-sleep -seconds 5
} until ($BackupAPI.get("$($BackupJob.ID)").progress -eq 100 -or $BackupAPI.get("$($BackupJob.ID)").state -ne "INPROGRESS")
$BackupAPI.get("$($BackupJob.ID)") | Select-Object id, progress, state | Format-Table -AutoSize
if ($BackupAPI.get("$($BackupJob.ID)").state -ne "SUCCEEDED") {
Throw "VCSA Backup Failed"
}
#endregion
Stop-Transcript
@vMarkusK
Copy link
Author

vMarkusK commented Nov 5, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment