How to reset a VM to a previous snapshot?

I am running Ubuntu virtual machine on Ubuntu host machine using VirtualBox. I installed some softwares on my virtual machine. I wonder if there is a method to reset my current virtual machine to a given previous state (snapshot) of my choice? How can I do that? How can I reset to a previous snapshot undoing any changes I will make further to my VM?

Any method is welcome, especially the ones I can program either in a bash script or any programming language.


Solution 1:

Take a snapshot

From the Virtual Box GUI (Virtualbox Manager) when Machine > Tools > Snapshots is selected we can easily take a snapshot from a running virtual machine by selecting Take Snapshot or pressing HOST+T:

enter image description here

This will open a dialog to give in a sensible name and optionally a description of this snapshot:

enter image description here

The snapshot will then be saved in the machine's directory (depending on the size of the machine this may take a while).

On the command line the same can be achieved by

VBoxManage snapshot <Name_of_VM> take <Name_of_Snapshot>

Restore a snapshot

From the Virtualbox Manager we can restore a shut down virtual machine to any snapshot by selecting a machine's Snapshots (1):

enter image description here

Select the appropriate snapshot from the list, then press enter image description here to restore this snapshot.

The current state of the machine will then be lost. To also restore the current state we will first have to create a snapshot too.

Restoring a snapshot from the command line is done with

VBoxManage snapshot <Name_of_VM> restore <Name_of_Snapshot>