How to backup vSphere’s virtual machine configurations automatically

We decided to setup some simple solution to backup configurations of all virtual machines in our virtualization cluster as this definitely could come in handy in case of data loss when these virtual machines would need to be restored along with their original configurations.

I was searching for a simple solution on Internet for a while and finally decided myself for solution consisting of simple Powershell script running on vCenter server which connects to vCenter (localhost in our case) and simply fetches .vmx files of all virtual machines and stores them into desired destination. These are the steps to follow leading to working solution:

  1. Download PowerCLI extension from VMware which is needed to control vSphere
    within Powershell. This extension can be downloaded from here
  2. Add Powershell scripting environment (ISE) (Start -> Administrative Tools -> Server Management -> Features -> Add Feature). This actually isn’t necessary but allow us to nicely edit, run and debug Powershell scripts in case of any problems. This step isn’t necessary on Windows 2012 R2 where Powershell ISE should be installed by default.
  3. Create system user vmxbackup on Vcenter server (Start -> Administrative Tools -> Computer Management -> Local Users and Groups -> Users)
  4. Create custom role for vmxbackup system user within vSphere server (Home -> Administration -> Roles) and create vmxbackup role with only these permissions: Datastore->Browse datastore, Datastore->Low level file operations
  5. Add permission for vmxbackup user in vCenter (Host and Clusters -> Vcenter -> Permissions -> Add Permission -> Add vmxbackup with vmxbackup role)
  6. The backup script I placed myself to C:\vSphereBackup\VMX_backup.ps1:

    # Script to backup the virtual machines configuration (vmx) file
    # Version 1.0 Magnus Andersson RTS
    # Version 2.0 Tomáš Brandýský

    $vCenter = “localhost“
    $vCenteruser = “vmxbackup“
    $vCenterpw = “password“

    $backupdir = “C:\vSphereBackup\backups”
    $logfile=”C:\vSphereBackup\backup.log”

    try {
    Add-PSSnapin VMware.VimAutomation.Core
    }

    catch {
    write-host “snap-in already added”
    }

    $ErrorActionPreference = “SilentlyContinue”
    $date=get-date -format g

    # Close all previous connections
    Disconnect-VIServer -Server $vCenter -Confirm:$false

    try {
    “———————————–-———————————————” >> $logfile
    “Backup of the virtual machine configuration files at:” >> $logfile
    $date >> $logfile
    “The backup was initiated by user: ” >> $logfile
    [Security.Principal.WindowsIdentity]::GetCurrent().Name >> $logfile
    “” >> $logfile
    }
    catch {
    write-host “can not write to log file!”
    }

    # Connect to vCenter Server
    Connect-viserver $vCenter -user $vCenteruser -password $vCenterpw -WarningAction 0

    Get-VM | unique | `
    Get-View | `
    ForEach-Object {
    $vmxfile = $_.Config.Files.VmPathName
    $dsname = $vmxfile.split(”]“)[0].TrimStart(“[“).TrimEnd(“]“)
    $ds = Get-Datastore -Name $dsname | unique
    New-PSDrive -Name ds -PSProvider VimDatastore -Root ‘/’ -Location $ds
    Copy-DatastoreItem -Item “ds:\$($vmxfile.split(‘]’)[1].TrimStart(‘ ‘))” -Destination $backupdir
    Remove-PSDrive -Name ds
    }

    $date=get-date -format g

    try {
    “Backup of the virtual machine configuration files finished was at:” >> $logfile
    $date >> $logfile
    “——————————————————————————–” >> $logfile
    }

    catch {
    write-host “can not write to log file!”
    }

    # Close the connection to server
    Disconnect-VIServer -Server $vCenter -Confirm:$false

  7. You can run script manually to check whether it copies all vmx files to C:\vSphereBackup\Backups (according to my observations it’s slower than expected)
  8. I scheduled the task to perform .vmx backups every day (Start -> Administrative Tools -> Computer Management -> System Tools -> Task Scheduler -> Create Basic Task. It is necessary to specify the path in this format in Program/script input field: powershell -file “C:\vSphereBackup\VMX_backup.ps1”
  9. For testing reason the task can also be run simply by clicking on “run” command when appropriate task is selected.

Posted in Backups, Server administration

2 Responses to “How to backup vSphere’s virtual machine configurations automatically”


Leave a Reply