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 backup.py 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, backup.py 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.

12 thoughts on “VMware backup script”

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

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?
Thanks!

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.

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.

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.