Free snapshot backups of virtual machines in VMware
This is an easy and good way of doing snapshot backups of your virtual machines in VMware and copy them to another datastore or NAS. It’s a simple script that you can run on one of the virtual machines on the host.
Requirements: plink.exe, to connect to the host and run CLI commands to perform the snapshots. It’s part of the VMware vSphere CLI package. Save and install it to your Windows VM running the backups.
Once installed, we need to create a batch file. Here is one example with a filename of ”vmbackup.bat”. You run this script with these parameters:
vmbackup.bat <vmware-host> <vmware-root-password> <vm-to-backup> <datastore-vm-is-on> <backup-to-this-destination>
This script will perform the following steps:
- Get the ID of the virtual machine for the backup.
- Delete and create the destination directory for the backup.
- Copy the .vmx configuration file of the VM.
- Take a snapshot of the VM.
- Create an archive and compress the .vmdk (disk of the VM) on the datastore.
- Remove all snapshots of them VM.
- Copy the .tar.gz backup file to the backup destination.
- Delete the .tar.gz backup file from the datastore.
@echo off
REM usage: vmbackup "server rootpassword vmname datastore backuppath"
SET server=%1
SET password=%2
SET vmname=%3
SET datastore=%4
SET backuppath=%5
REM Get VM ID
FOR /F "tokens=1,2 delims= " %%n in ('plink root@%server% -pw %password% "vim-cmd vmsvc/getallvms | grep %vmname%"') do (
echo Creating backup of %vmname% on %server%. ID is %%n
REM Remove and recreate destination directories
rd /q /s %backuppath%\%vmname%
REM Ping the NAS to wake it up and spin up the disks
ping 192.168.0.99
md %backuppath%\%vmname%
REM Copy vmx before snapshot (on restore rename to .vmx again)
plink root@%server% -pw %password% "cd vmfs/volumes/%datastore%/%vmname% ; cp %vmname%.vmx %vmname%.vmx.renamed"
echo.
REM Take snapshot
plink root@%server% -pw %password% "vim-cmd vmsvc/snapshot.create %%n "BackupScript" "Snapshot created by Backup Script"
echo.
REM Add vmdk to tar gzip after snapshot
plink root@%server% -pw %password% "cd vmfs/volumes/%datastore%/%vmname% ; tar -czvf %vmname%.tar.gz %vmname%.vmdk %vmname%-flat.vmdk %vmname%.vmx.renamed"
echo.
REM Remove all snapshots
plink root@%server% -pw %password% "vim-cmd vmsvc/snapshot.removeall %%n"
REM Copy tar.gz file
vifs.pl --server %server% --username root --password %password% --get "[%datastore%]%vmname%/%vmname%.tar.gz" %backuppath%\%vmname%
echo.
REM Remove tar.gz file and Remove .renamed (renamed vmx before snapshot)
plink root@%server% -pw %password% "cd vmfs/volumes/%datastore%/%vmname% ; rm %vmname%.tar.gz ; rm %vmname%.vmx.renamed"
echo.
)
You can then run this script as a scheduled task. To restore a backup you simple shut the virtual machine down, copy the file back to the datastore and uncompress it from the archive. Edit the .vmx config file if needed (datastore change for example) and start the VM. If it’s not in the inventory, you can also import the restored virtual machine as a new VM.