Skip to content

VMware backup script

I have been searching for simple and easy script to backup selected virtual machines running on esxi server, which will:

  • run on linux – do not require windows machine; can’t use PowerCLI
  • do not require direct access to esxi host using ssh (as other backups scripts like do) and will use API
  • support multiple esxi hosts managed by vcenter
  • have clear code – that means no perl hell (including big vsphere perl sdk)
  • have short code
  • can be put into cron

I was unable to find any to comply my requirements. I’m learning python, so I searched for some python vsphere libs and found psphere. Then has born.

How it works? Script will go trought all vms running on esxi host or vcenter and search for vm names listed in backup.list file. If vm is on list, then snapshot is created and copied to backup datastore (as thin provision disk) with virtual machine configuration file (.vmx). After copying snapshot is removed. At the end, will search for old backups (variable KeepDays) and delete them.

In my setup I’m using separate 1.5 TB disk as datastore [backup] to store backups, but you can use for example NFS datastore exported from some ZFS storage with deduplication or from some NAS device.

Script is hardcoded to use first Datacenter in vcenter, if you have more, you have to modify it. Plain esxi without vcenter don’t care about datacenters, so it is all ok.

Download including psphere here


from psphere.client import Client
from psphere.managedobjects import VirtualMachine, VirtualDiskManager, FileManager, Datacenter
from datetime import datetime, date, timedelta
import time, logging

# datastore name where to backup
# how many days keep backups
client = Client("host","root","password")
logging.basicConfig(level=logging.INFO,format='%(asctime)s - %(message)s',datefmt='%Y-%m-%d %H:%M:%S')
logger = logging.getLogger("esxibackup")


vmlist = VirtualMachine.all(client)

for vm in vmlist:
    logger.debug("Found vm: %s"
    if in open('backup.list').read():
      "Found vm %s on backup list"
                destination=BackupToDatastore+" ""%Y-%m-%d")+"/"
                        destinationName=destination+"/%s.vmx" %(,
      "Creating virtual machine %s snapshot"
                                        description="Automatic backup ""%Y-%m-%d %H:%M:%s"),

                while in ["queued", "running"]:
      "Snapshot created")

                for device in vm.config.hardware.device:
                        if device.__class__.__name__ == 'VirtualDisk':
                      "Backuping %s to %s" %(device.backing.fileName,destination))
                                dstSpec = client.create("VirtualDiskSpec")
                                while in ["queued", "running"]:
                                        logger.debug("task update")
                                if == "success":
                                        elapsed_time = -
                              "Elapsed time: %s" %elapsed_time)
                                elif == "error":
                                        logger.error("ERROR: The task finished with an error. If an error was reported it will follow.")
                                                logger.error("ERROR: %s" %
                                        except AttributeError:
                                                logger.error("ERROR: There is no error message available.")
                                        logger.error("UNKNOWN: The task reports an unknown state %s" %

      "Removing virtual machine %s snapshot"
                while in ["queued", "running"]:
                        logger.debug("task update")

for ds in dc.datastore:
                while in ["queued", "running"]:
                if == "success":
                        for file in
                                        from_date =
                                        if < from_date:
                                      "Deleting old backup %s" %file.path)
                                                while in ["queued", "running"]:
                                except ValueError:
                              "Ignoring folder '%s'" %file.path)

Sample output from backup

2014-06-27 03:56:56 - Found vm on backup list
2014-06-27 03:56:56 - Creating virtual machine snapshot
2014-06-27 03:57:07 - Snapshot created
2014-06-27 03:57:07 - Backuping [raid1] to [backup] 2014-06-27/
2014-06-27 04:00:20 - Elapsed time: 0:03:11.276205
2014-06-27 04:00:20 - Backuping [raid1] to [backup] 2014-06-27/
2014-06-27 04:04:44 - Elapsed time: 0:04:22.287747
2014-06-27 04:04:44 - Removing virtual machine snapshot
2014-06-27 04:05:15 - Found vm on backup list
2014-06-27 04:05:15 - Creating virtual machine snapshot
2014-06-27 04:05:25 - Snapshot created
2014-06-27 04:05:25 - Backuping [raid1] to [backup] 2014-06-27/
2014-06-27 04:06:57 - Elapsed time: 0:01:28.828372
2014-06-27 04:06:57 - Removing virtual machine snapshot
2014-06-27 04:07:03 - Found vm on backup list
2014-06-27 04:07:04 - Creating virtual machine snapshot
2014-06-27 04:07:14 - Snapshot created
2014-06-27 04:07:14 - Backuping [raid1] to [backup] 2014-06-27/
2014-06-27 04:08:35 - Elapsed time: 0:01:17.608250
2014-06-27 04:08:35 - Removing virtual machine snapshot
2014-06-27 04:08:42 - Deleting old backup 2014-06-25

For consistent backup of virtual machines running mysql databases see my next post

13 thoughts on “VMware backup script”

  1. I am getting while running the above script at line 36.
    C:\Python34>python.exe D:\scripts\backup\
    File “D:\scripts\backup\”, line 36“Found vm %s on backup list”
    TabError: inconsistent use of tabs and spaces in indentation


  2. Hi, maybe copy&paste or editor issue? Did you used script copied from webpage itself or from zip file? Please try from zip in article.

  3. I checked your script. However when I think backup a server it gets for example from 200GB to 20GB.
    The point is if I wanted to recover this server, those files at 20GB will weight 200GB when rsyncing them.
    If it’s vm does not matter, but if there are 4, a long time could take for transferring all files.
    Any solution?

  4. Hi,
    when copying VM to backup datastore, it is done as thin-provison disks. If your 200GB VM has only 20GB allocated, backup will have just 20GB. But do not copy thin-provision disks by rsync. Setup NFS datastore and copy it using API or backup your VMs directly to NFS datastore.
    To restore just connect backup datastore to new esxi server and import VMs. You can do vMotion after that.

  5. Does this work with the free version of ESXI? I’m getting the following error:

    [backup] 2014-09-24/PowerMonitor

    Traceback (most recent call last):
    File “./”, line 40, in
    File “/home/mephi/backupESXI/old/psphere/”, line 355, in func
    File “/home/mephi/backupESXI/old/psphere/”, line 173, in invoke
    result = getattr(self.service, method)(_this=_this, **kwargs)
    File “/usr/lib/python2.7/dist-packages/suds/”, line 542, in __call__
    return client.invoke(args, kwargs)
    File “/usr/lib/python2.7/dist-packages/suds/”, line 602, in invoke
    result = self.send(soapenv)
    File “/usr/lib/python2.7/dist-packages/suds/”, line 653, in send
    result = self.failed(binding, e)
    File “/usr/lib/python2.7/dist-packages/suds/”, line 708, in failed
    r, p = binding.get_fault(reply)
    File “/usr/lib/python2.7/dist-packages/suds/bindings/”, line 265, in get_fault
    raise WebFault(p, faultroot)
    suds.WebFault: Server raised fault: ‘Current license or ESXi version prohibits execution of the requested operation.’

  6. Hello
    I don’t kwon python.

    When I run the script return this error

    Traceback (most recent call last):
    File “”, line 4, in
    from psphere.client import Client
    File “W:\Source\Tgarijo\VmWareBackup\psphere\”, line 117
    except URLError, e:
    SyntaxError: invalid syntax

    thank You

  7. Hi,

    Is there an error in the script? It specifies, but does not use the dstSpec, making it copy the spec from the original file, so thick becomes thick again instead of thin.

    I have tried to fiddle with it but, can’t seem to get it working properly.. :-S


  8. Do you happen to have an end to end documented guide to support the implementation? Specifically I am unsure exact where the psphere client should be installed and the script executed from – i.e. the host or target server or just from a desktop client which secure (shell) access to the VMs you want to back up? Additionally, whilst I assume the backup script is a simple text file can you provide an example of the format of the file for me to follow? Last, where exactly do you read the backup list from? again is the the host or target server or desktop client? Some simple, documented, these are the steps you take would take some of the guess work out of it for me.

    Much appreciated.


  9. Hi, there is no more documentation. You can just run it somewhere, where you can run python script(s) – some linux VM for example.
    List of vms to backup is just plain text file with name of vm – one vm per line. Put it in same directory as backup script.

Leave a Reply

Your email address will not be published. Required fields are marked *