Create/Show/Delete Snapshots with ESXi CLI
In various situations (mostly in the context of automation), I have been asked by DevOps colleagues how to take a snapshot of a VM via CLI. The whole thing works with a vim-cmd command. You need access via SSH to the respective ESXi server, then we list all available VMs:
vim-cmd vmsvc/getallvms
Then we note the VMID from which we want to create a snapshot. In our case, it is VMID 6 “linux-a-14”.
(Optional) With the following command you can check the power state of the VM:
vim-cmd vmsvc/power.getstate 6
Now that the VM is powered on, we can fire off the vim-cmd command for a snapshot creation:
vim-cmd vmsvc/snapshot.create 6 test1
We can extend the Snapshot Create vim-cmd command by adding a description and defining two options that can be also selected in the GUI by setting 1 (on) or 0 (off) behind the description:
- Including virtual machines memory
- Quiescing guest file system (requires VM tools)
In this Example both options are off:
vim-cmd vmsvc/snapshot.create 6 test snapshot_test 0 0
If the snapshot creation fails, you will get a different (error) message. I create 2 more snapshots (test2, test3) to fill the overview. Because next we fire the command to see the existing snapshots, it is important that you provide the corresponding VMID (in our case 6) again.
vim-cmd vmsvc/snapshot.get 6
Here we can see perfectly (as in the GUI) how the snapshot topology looks like. If you want to delete a specific snapshot you have to enter the following command. In our case we delete Snapshot test2, for this we notice the Snapshot ID 9 in the above overview:
vim-cmd vmsvc/snapshot.remove 6 9
The command directly returns a new overview of the current available Snapshots and as you can see, Snapshot test2 is deleted. If we want to delete ALL Snapshots of a VM, we need the following command:
vim-cmd vmsvc/snapshot.removeall 6
The output is expressive enough, in my opinion. 😀 This is it from this little tutorial for creating/deleting/showing Snapshots via CLI. If you have any questions please leave it in the comments. 🙂