In my home lab, I run vCenter 6.7 with several ESXi hosts and since I built the lab I never did any backups.
Now that my VMs are not test VMs and they became fairly important, I’ve decided to take care of the backups.
Initially, I was thinking of using the Veeam Free Backup, but this program can’t do any schedules in the free version. Then I decided to play with OVF Tool, but I was having multiple issues with this tool and logging with a domain account. So, finally, I used PowerCLI and AWS CLI.
The idea is to create a OVA export of each VM and then upload it to an S3 bucket. The source is well commented, so you shouldn’t have any issues in the script’s logic.
###################################################################################################### # vCenter2AWS v0.1 # K.Andreev - 06/30/2018 # Description: Creates an OVA backup of all or selected vCenter VMs # and uploads them to a bucket in AWS (optional) # Prerequisites: PowerCLI, AWS CLI (configured) # NOTE: The VM being backed up will be shut down and powered up when the OVA file is complete # If using PowerCLI 10.x execute this line once # Set-PowerCLIConfiguration -InvalidCertificateAction Ignore ###################################################################################################### # Function for logging Function LogWrite { Param ([string]$logstring) Add-content $LogFile -value ((Get-Date -format "[dd/MMM/yyyy:HH:mm:ss] ").ToString() + $logstring) } ###################################################################################################### ########## IMPORTANT PARAMETERS. DO NOT IGNORE. MODIFY TO SUIT YOUR NEEDS. ########################### ###################################################################################################### # vCenter hostname $VCenter = "vcenter.domain.local" # vCenter username $Username = "[email protected]" # vCenter password $Password = "SecretPassword" # Destination for the OVA backup files, make sure you have trailing \ at the end of this string $Destination = "D:\VMs\" # Exclude the following VMs from backups $Exclude = @("VMware vCenter Server Appliance") # Name of the S3 bucket (make sure it exists, lower case only, starts with s3://) $S3Bucket = "s3://vspherebackups" # Do you want to upload to S3 $Upload = $true # Specify if the OVA file contains a time stamp # e.g. vmname.YYYYMMDDHHmm.ova ($true) or vmname.ova ($fasle) $TimeStamp = $true # Do you want the OVA files to be deleted after upload to S3 $DeleteOVA = $true # Log file path and name $LogFile = "D:\VMs\vcenter2aws.log" ###################################################################################################### ################### EXPORT VMs AS OVA FILES ########################################################## ###################################################################################################### # Connect to vCenter $Server = Connect-VIServer $VCenter -User $Username -Password $Password LogWrite ("Connected to " + $Server) # Get all VMs $VMs = Get-VM # Loop through each of the VMs Foreach ($VM in $VMs) { # Get the name of each VMs $Name = $VM.Name # Skip the backup for the VMs that are in the array variable defined above if ($Exclude.Contains($Name)) { LogWrite ("Backup skipped for "+ $Name) Continue } # Stop the VM in order to take a backup Stop-VM -VM $Name -Confirm:$false LogWrite ("Stopping VM " + $Name) # Calculates the time stamp that will become part of the name if ($TimeStamp) { $TimeStampName = Get-Date -UFormat "%Y%m%d%H%M" $TimeStampName = "." + $TimeStampName } else # otherwise the time stamp will be blank { $TimeStampName = "" } LogWrite ("Backup starts for " + $Name) # Exports the VM as OVA file, overwrites if there is a same file at the destination Get-VM -Name $Name | Export-VApp -Destination ($Destination + $Name + $TimeStampName + ".ova") -Format Ova -Force:$true LogWrite ("Backup ends for " + $Name) # Once the export completes, power up the VM Start-VM -VM $Name -RunAsync LogWrite ("Starting VM " + $Name) } # Disconnect from vCenter Disconnect-VIServer -Server $Server -Confirm:$false LogWrite ("Disconnected from " + $Server) ###################################################################################################### ################################ UPLOAD TO AWS ####################################################### ###################################################################################################### # Exit if upload is not needed if (!$Upload) { LogWrite "Upload to S3 skipped, exiting..." exit } LogWrite ("Upload to S3 bucket " + $S3Bucket + " starts") # Use AWS CLI command to sync the destination folder and S3. aws s3 sync $Destination $S3Bucket LogWrite ("Upload to S3 bucket " + $S3Bucket + " ends") ###################################################################################################### ################################ DELETE THE OVA FILES ################################################ ###################################################################################################### # Exit if delete is not needed if (!$DeleteOVA) { LogWrite "Deleting OVA files skipped, exiting..." exit } Get-ChildItem $Destination -Include *.ova -Recurse | Foreach ($_) {Remove-Item $_.Fullname} LogWrite ("Deleting OVA files from " + $Destination) LogWrite "Script completed...Have a nice day!"