1. Take the snapshot of VM.
2.Backup the Original VMDK file.
3.Remove the snapshot after vmdk file backed up.
You can use below powershell script to backup the vmdk file in your local system drive and you can also fixed the size of datastore( if datastore size is equal to 40GB then take the snapshot and backup the VMDK file otherwise exit) if datastore size is equal to your mentioned size then it will take the snapshot, backup the vmdk file and remove the snapshot. you have to mention size in MB in script.
To run the script first fixed the size and then run with vmware powershell.
ex.- ./test.ps1 "Test" where "Test" is your VM Name.
param([string] $vc)
$size= "27189"
$bpath="d:\MyBackupFolderForVMs\"
$dc=Get-Datastore -VM $vc
foreach($ds in $dc)
{
if ($_FreeSpaceMB -le $size)
{
Write-Host -ForegroundColor Green "Enough space Available on the datastore" $dc
Write-Host "Please wait Creating Snapshot"
new-Snapshot -VM $vc -Name Backup
Write-Host "Snapshot has been created"
Write-Host "Mapping Datastore As a Local Drive"
$datastore = Get-Datastore $dc
New-PSDrive -Location $datastore -Name ds -PSProvider VimDatastore -Root "\"
Set-Location ds:\
Write-Host "Copying VMDK files to " $bpath
Copy-DatastoreItem -Item ds:\$vc\$vc.vmdk -Destination $bpath
Write-Host "Vmdk files copied successfully to" $bpath
Write-Host "Removing Snapshot"
Get-Snapshot -VM $vc | Remove-Snapshot -Confirm:$false
Set-Location G:\
Write-Host -ForegroundColor Yellow $vc "vmdk file has been saved to" $bpath "and Ready for backup"
}
else
{
Write-Host -ForegroundColor Red "No Enough Space Available on the" $dc "datastore can't create snapshot"
}
}