ESXi 5.1 revert to snapshot daily or every night

mimipc picture mimipc · Feb 2, 2013 · Viewed 8.6k times · Source

I'm trying to revert a virtual machine to the previous snapshot every day or night. Unfortunately, I haven't found any way to do this the way I want it.

Here are some things I tried and that didn't fit :

- snapshot.action=autoRevert --> The VM has to HALT, REBOOT doesn't work the same. I don't want to power on my VM manually.

- snapshot.action=autoRevert on a running snapshot. I tried this, thinking it might work and resolve the first issue. But when i HALT my VM, the snapshot is reverted but the VM is placed in a suspended state...

- PowerCLI script : I don't want to have a Windows machine running just for this little thing.

- NonPersistent disk : same thing as the first issue : VM needs to HALT, not REBOOT.

How can I simply do this ? I thought I could just do those things and place a cron on my linux VM to reboot every night.

Answer

Bill Agee picture Bill Agee · Feb 4, 2013

In the past I've set up scripts that revert VMs to specific snapshots via the SSH server on my ESXi host. Once sshd is enabled, you can remotely run vim-cmd over SSH. This was on ESXi 4.x, but I assume the same can be done in newer versions.

The catch was that I had to enable the so-called "Tech Support Mode" to get sshd running, as documented in the VMware KB: kb.vmware.com/kb/1017910

The procedure I used was to first look up the ID of the VM in question by running:

vim-cmd vmsvc/getallvms

Then, you can view your VM's snapshot tree by passing its ID to this command (this example uses the VM with ID 80):

vim-cmd vmsvc/get.snapshotinfo 80

Finally, you can use an SSH client to remotely revert the VM to an arbitrary snapshot by passing the VM and snapshot IDs to 'snapshot.revert':

ssh root@YOUR_VMWARE_HOST vim-cmd vmsvc/snapshot.revert VM_ID 0 SNAPSHOT_ID

One other thing to note is that you can set up public key authentication between the ESXi server and the machine running your scripts so that the latter won't have to use a password.

The only annoyance with that approach was that I didn't immediately see a way to preserve the authorized_keys file on the ESXi server between reboots - if the ESXi server has to be rebooted, you'll have to rebuild its authorized_keys file before public key auth will work again.