How to Create a Virtual Machine Snapshot?

Virtual Machine (VM) snapshots are a powerful tool for protecting data and quickly restoring the system in case of failures, errors, or the need to perform risky operations. They allow you to save the state of your virtual machine at a specific point in time so you can return to it at any moment. In this article, we will thoroughly review the process of creating virtual machine snapshots, various methods, tools, and strategies to help you effectively use this important feature. We will focus on practical examples and best practices to ensure reliable protection of your virtual environments.

Contents:

Basic Concepts and Advantages of VM Snapshots

How to Create a Virtual Machine Snapshot? - Explanation of what VM snapshots are and why they are useful.
A virtual machine (VM) snapshot is essentially a copy of the state of the VM’s disk and RAM at a specific point in time. It includes all the data contained on the virtual disks, as well as the current state of the RAM, hardware configuration, and network settings. This allows you to «freeze» the state of the VM and return to it if necessary. Think of it as a «restore point» for your virtual machine. The primary goal of a snapshot is to provide a quick and easy way to roll back changes that may cause problems. For example, before installing new software, updating the system, or making configuration changes, you can create a snapshot. If something goes wrong, you can quickly return to the previous, working state of the VM without spending time reinstalling the operating system or restoring data from backups. It is important to understand that snapshots are not a replacement for full backups. They are intended for temporary solutions and quick recovery, not for long-term data storage. Relying on snapshots for an extended period can lead to performance issues and data loss, especially if there are too many snapshots or they take up a lot of space.

Advantages of Using VM Snapshots

The main advantages of using VM snapshots include:
  • Quick recovery after errors: The ability to quickly roll back to the previous state of the VM significantly reduces downtime in case of failures or errors.
  • Testing and development: Snapshots allow developers to safely experiment with new code or configurations, knowing that they can always return to the previous state.
  • Installing updates and patches: Creating a snapshot before installing updates provides the ability to quickly roll back if the update causes compatibility issues or other undesirable consequences.
  • Creating temporary copies for development or testing tasks: You can create a snapshot of a running system and use it to create an isolated environment for development and testing without affecting the main system.

When Not to Use VM Snapshots

Snapshots are not suitable for the following scenarios:
  • Long-term data storage: Snapshots are not a reliable means for long-term data storage. They can lead to decreased performance and data loss if the underlying VM disk is damaged.
  • Replacing backups: Snapshots do not replace full backups. Backups should be created regularly and stored in another location to protect against serious failures, such as storage damage or natural disasters.
  • Virtual machines with high write intensity: Snapshots can negatively impact the performance of virtual machines that intensively write data to disk, such as databases.
Example 1: Suppose you plan to update the PHP version on your web server running on a virtual machine. Before starting the update, create a snapshot of the virtual machine. If, after the update, the website stops working due to compatibility issues, you can quickly return to the state before the update by restoring the VM from the snapshot.
# Example: Updating PHP on Ubuntu (create a snapshot before this!)
sudo apt update
sudo apt upgrade php
Example 2: A developer is working on a new module for a web application. Before starting work, they create a snapshot of the virtual machine with the working version of the application. If, during development, the module causes crashes or errors, the developer can easily restore the VM from the snapshot and continue working with a clean version of the application.
# Example: Installing Composer (create a snapshot before this!)
sudo apt update
sudo apt install composer
Example 3: A system administrator plans to make changes to the configuration of the virtual machine’s network settings. Before making changes, they create a snapshot of the VM. If, after the changes, the VM loses network connectivity, the administrator can quickly return to the previous configuration by restoring the VM from the snapshot.
# Example: Changing the /etc/network/interfaces file (create a snapshot before this!)
sudo nano /etc/network/interfaces
# Make the necessary changes
sudo systemctl restart networking
Expert tip: Always document what changes you plan to make before creating a snapshot. This will help you remember what the snapshot was created for and simplify the restoration process if needed. The snapshot name should be informative and reflect the essence of the planned changes. For example, «Before_PHP_7.4_Update».
FunctionVM SnapshotsBackup
PurposeQuick recovery after errors, testingData protection from serious failures, long-term storage
Creation frequencyAs needed, before changesRegularly, on schedule
Storage locationOn the same storage as the VMIn another location (separate storage, cloud)
ReliabilityLess reliable, dependence on the base diskMore reliable, independent storage

Creating a Snapshot in VMware vSphere

How to Create a Virtual Machine Snapshot? - Step-by-step guide with screenshots for creating a snapshot in VMware vSphere.
VMware vSphere provides a simple and intuitive interface for creating and managing virtual machine snapshots. VMware snapshots allow you to capture the state of a virtual machine, including the contents of disks and memory, allowing you to quickly return to the previous state if necessary. It is important to understand that creating snapshots can impact the performance of a virtual machine, especially for systems with high write intensity. Therefore, it is recommended to create snapshots only when it is really necessary and delete them after completing the necessary operations. VMware uses so-called «delta disks» to store changes made after creating a snapshot. These disks grow as data changes, and a large number of snapshots or their long-term storage can lead to a significant increase in occupied space and a decrease in performance.

Creating a Snapshot via vSphere Client

The most common way to create a snapshot in VMware vSphere is through the vSphere Client graphical interface.
  • Step 1: Connect to the vSphere Client and select the virtual machine for which you want to create a snapshot.
  • Step 2: Right-click on the selected virtual machine and select «Snapshot» -> «Take Snapshot».
  • Step 3: Enter a name and description for the snapshot. The description should be informative so that you can easily identify what the snapshot was created for.
  • Step 4: Check the «Memory» box (if you need to include the memory state in the snapshot). Including memory in the snapshot takes longer, but allows you to return to the exact same state of the VM as it was at the time of the snapshot (for example, with running applications).
  • Step 5: Check the «Quiesce» box (if you need the file system to be in a consistent state). Quiesce freezes the file system while the snapshot is being created to ensure data integrity. This option is especially important for virtual machines with databases or other critical applications.
  • Step 6: Click «OK» to create the snapshot.
Example 1: Creating a snapshot before installing the VMware Tools update.
  • Connect to the vSphere Client.
  • Select the VM on which you need to update VMware Tools.
  • Right-click -> Snapshot -> Take Snapshot.
  • Name: «Before_VMware_Tools_Update».
  • Description: «Snapshot before updating VMware Tools to version 12.x».
  • Memory: Unchecked.
  • Quiesce: Checked.
  • Click «OK».
Example 2: Creating a snapshot with memory included before debugging an application.
  • Connect to the vSphere Client.
  • Select the VM with the application being developed.
  • Right-click -> Snapshot -> Take Snapshot.
  • Name: «Before_Application_Debugging».
  • Description: «Snapshot with memory enabled for application debugging».
  • Memory: Checked.
  • Quiesce: Unchecked.
  • Click «OK».

Creating a Snapshot Using PowerCLI

To automate the creation of snapshots, you can use PowerCLI, a PowerShell cmdlet designed to manage VMware vSphere.
# Connecting to vCenter Server
Connect-VIServer -Server vcenter.example.com -User administrator@vsphere.local -Password "Password123!"

# Getting the virtual machine
$VM = Get-VM -Name "MyVM"

# Creating a snapshot
New-Snapshot -VM $VM -Name "Before_Changes" -Description "Snapshot before making changes to the configuration" -Memory -Quiesce
Explanation:
  • Connect-VIServer: Connects to vCenter Server. Replace vcenter.example.com, administrator@vsphere.local and "Password123!" with your actual data.
  • Get-VM: Gets the virtual machine object named «MyVM». Replace «MyVM» with the name of your virtual machine.
  • New-Snapshot: Creates a snapshot of the virtual machine.
    • -VM: Specifies the virtual machine for which to create the snapshot.
    • -Name: Specifies the name of the snapshot.
    • -Description: Specifies the description of the snapshot.
    • -Memory: Includes the memory state in the snapshot.
    • -Quiesce: Ensures file system consistency.
Example 3: Automatically creating a snapshot before running a script using PowerCLI.
# Connecting to vCenter Server
Connect-VIServer -Server vcenter.example.com -User administrator@vsphere.local -Password "Password123!"

# Getting the virtual machine
$VM = Get-VM -Name "MyVM"

# Creating a snapshot
New-Snapshot -VM $VM -Name "Before_Script" -Description "Snapshot before running script" -Memory:$false -Quiesce:$true

# Running the script (replace with the path to your script)
.\MyScript.ps1

# Disconnecting from vCenter Server
Disconnect-VIServer -Confirm:$false
Expert Tip: Use PowerCLI to automate the creation and management of snapshots, especially if you need to create snapshots regularly for a large number of virtual machines. Write a script that will automatically create snapshots before performing certain tasks and delete them after completion to avoid accumulating outdated snapshots.

Creating a Snapshot in Microsoft Hyper-V

Microsoft Hyper-V, an integrated virtualization platform in Windows Server, also provides snapshot capabilities called «Checkpoints.» Hyper-V checkpoints allow you to save the state of a virtual machine at a specific point in time, similar to snapshots in VMware. However, it is important to note that Microsoft distinguishes between two types of checkpoints: standard and production checkpoints. Standard checkpoints capture the memory state of the virtual machine, which can be useful for debugging or testing, but is not recommended for production systems as it can lead to data integrity issues. Production checkpoints use backup technologies at the guest operating system level (e.g., VSS in Windows) to ensure data integrity and are recommended for use in production environments.

Creating a Checkpoint via Hyper-V Manager

The easiest way to create a checkpoint is to use the Hyper-V Manager graphical interface.
  • Step 1: Open Hyper-V Manager.
  • Step 2: Select the virtual machine for which you want to create a checkpoint.
  • Step 3: Right-click on the selected virtual machine and select «Checkpoint».
  • Step 4: Hyper-V will create a checkpoint, which will be displayed in the «Checkpoints» panel for the selected virtual machine.
Example 1: Creating a checkpoint before installing a Windows Server operating system update.
  • Open Hyper-V Manager.
  • Select the VM with Windows Server.
  • Right-click -> Checkpoint.
  • After creating the checkpoint, you can safely install Windows updates.

Creating a Production Checkpoint

To create a production checkpoint, you need to configure its use in the virtual machine settings.
  • Step 1: Open Hyper-V Manager.
  • Step 2: Select the virtual machine for which you want to use production checkpoints.
  • Step 3: Right-click on the selected virtual machine and select «Settings».
  • Step 4: In the settings, select the «Checkpoints» section.
  • Step 5: Select the «Production Checkpoint» option. You can select «Standard Checkpoint» to create standard checkpoints, but it is recommended to use «Production Checkpoint» for production systems.
  • Step 6: Click «OK» to save the changes.
Now, when creating a checkpoint for this virtual machine, Hyper-V will use a production checkpoint.

Creating a Checkpoint Using PowerShell

To automate the creation of checkpoints, you can use PowerShell.
# Getting the virtual machine
$VM = Get-VM -Name "MyVM"

# Creating a checkpoint
Checkpoint-VM -Name $VM.Name -SnapshotName "Before_Changes"
Explanation:
  • Get-VM: Gets the virtual machine object named «MyVM». Replace «MyVM» with the name of your virtual machine.
  • Checkpoint-VM: Creates a checkpoint of the virtual machine.
    • -Name: Specifies the name of the virtual machine.
    • -SnapshotName: Specifies the name of the checkpoint.
To create a production checkpoint, make sure the «Production Checkpoint» option is selected in the virtual machine settings, as described above. PowerShell will use this setting when creating the checkpoint. Example 2: Automatically creating a checkpoint before running a script using PowerShell.
# Getting the virtual machine
$VM = Get-VM -Name "MyVM"

# Creating a checkpoint
Checkpoint-VM -Name $VM.Name -SnapshotName "Before_Script"

# Running the script (replace with the path to your script)
.\MyScript.ps1
Example 3: Checking the checkpoint type.
Get-VM -Name "MyVM" | Get-VMSnapshot | Select-Object Name, SnapshotType
Expert Tip: Always use Production Checkpoints for production virtual machines to ensure data integrity. Standard checkpoints should only be used for testing and debugging when you need to capture the memory state of the virtual machine. Regularly delete outdated checkpoints to avoid performance issues and lack of disk space.

Managing and Restoring from Snapshots

After creating a virtual machine snapshot, it is important to be able to effectively manage it and, if necessary, restore the virtual machine from the snapshot. Both VMware vSphere and Microsoft Hyper-V provide tools for managing snapshots and checkpoints, including the ability to delete, restore, and view information about snapshots. Improper management of snapshots can lead to performance issues, lack of disk space, and even data loss. Therefore, it is important to understand how to properly use these tools and follow best practices for snapshot management. The key is to regularly delete outdated snapshots that are no longer needed and monitor disk space usage. It is also important to remember that restoring from a snapshot will result in the loss of all changes made to the virtual machine after the snapshot was created.

Managing Snapshots in VMware vSphere

In VMware vSphere, snapshot management is done through the Snapshot Manager, available in the vSphere Client.
  • Snapshot Manager: Open the vSphere Client, select the virtual machine, and go to the «Snapshots» tab. Here you will see a list of all snapshots created for this virtual machine.
  • Restoring from a Snapshot: Select the snapshot from which you want to restore the virtual machine and click «Revert». Confirm the restoration to return to the state of the virtual machine at the time the snapshot was created.
  • Deleting a Snapshot: Select the snapshot you want to delete and click «Delete». If you want to delete all snapshots, click «Delete All». Warning: Deleting a snapshot only deletes the snapshot file, not the data contained within it. The data will be merged with the base disk of the virtual machine.
  • Editing the Snapshot Name and Description: Select the snapshot and click «Edit» (pencil icon). Here you can change the snapshot name and description.
Example 1: Restoring a virtual machine from a snapshot after a failed update.
  • Open the vSphere Client.
  • Select the VM that had the failed update.
  • Go to the «Snapshots» tab.
  • Select the «Before_Update» snapshot.
  • Click «Revert».
  • Confirm the restoration.

Managing Snapshots Using PowerCLI in VMware

PowerCLI also provides snapshot management capabilities.
# Connecting to vCenter Server
Connect-VIServer -Server vcenter.example.com -User administrator@vsphere.local -Password "Password123!"

# Getting the virtual machine
$VM = Get-VM -Name "MyVM"

# Getting a list of snapshots
Get-Snapshot -VM $VM

# Restoring from a snapshot
Get-Snapshot -VM $VM -Name "Before_Changes" | Revert-Snapshot -Confirm:$false

# Deleting a snapshot
Get-Snapshot -VM $VM -Name "Before_Changes" | Remove-Snapshot -Confirm:$false

# Deleting all snapshots
Get-Snapshot -VM $VM | Remove-Snapshot -Confirm:$false
Explanation:
  • Get-Snapshot: Gets a list of snapshots or a specific snapshot of the virtual machine.
  • Revert-Snapshot: Restores the virtual machine from the snapshot. -Confirm:$false disables the confirmation prompt.
  • Remove-Snapshot: Deletes the snapshot. -Confirm:$false disables the confirmation prompt.
Example 2: Creating a report on virtual machine snapshots using PowerCLI.
# Connecting to vCenter Server
Connect-VIServer -Server vcenter.example.com -User administrator@vsphere.local -Password "Password123!"

# Getting a list of virtual machines and their snapshots
$Report = Get-VM | Get-Snapshot | Select-Object VMName, Name, Created, SizeMB

# Exporting the report to a CSV file
$Report | Export-Csv -Path "C:\SnapshotsReport.csv" -NoTypeInformation

Managing Checkpoints in Hyper-V

In Hyper-V, checkpoint management is done through Hyper-V Manager.
  • Hyper-V Manager: Open Hyper-V Manager, select the virtual machine, and in the «Checkpoints» panel you will see a list of all checkpoints created for this virtual machine.
  • Restoring from a Checkpoint: Select the checkpoint from which you want to restore the virtual machine, right-click, and select «Apply». Confirm the restoration to return to the state of the virtual machine at the time the checkpoint was created.
  • Deleting a Checkpoint: Select the checkpoint you want to delete, right-click, and select «Delete». If you want to delete all checkpoints, select the virtual machine, right-click, and select «Delete Checkpoint Subtree». Warning: Deleting a checkpoint only deletes the checkpoint file, not the data contained within it. The data will be merged with the base disk of the virtual machine.
  • Renaming a Checkpoint: Select the checkpoint, right-click, and select «Rename».
Example 3: Deleting all checkpoints for a virtual machine.
  • Open Hyper-V Manager.
  • Select the VM for which you want to delete all checkpoints.
  • Right-click -> Delete Checkpoint Subtree.
  • Confirm the deletion.

Managing Checkpoints Using PowerShell in Hyper-V

PowerShell also provides checkpoint management capabilities in Hyper-V.
# Getting the virtual machine
$VM = Get-VM -Name "MyVM"

# Getting a list of checkpoints
Get-VMSnapshot -VMName $VM.Name

# Restoring from a checkpoint
Get-VMSnapshot -VMName $VM.Name -Name "Before_Changes" | Restore-VMSnapshot -Confirm:$false

# Deleting a checkpoint
Get-VMSnapshot -VMName $VM.Name -Name "Before_Changes" | Remove-VMSnapshot -Confirm:$false

# Deleting all checkpoints
Get-VMSnapshot -VMName $VM.Name | Remove-VMSnapshot -Confirm:$false
Explanation:
  • Get-VMSnapshot: Gets a list of checkpoints or a specific checkpoint of the virtual machine.
  • Restore-VMSnapshot: Restores the virtual machine from the checkpoint. -Confirm:$false disables the confirmation prompt.
  • Remove-VMSnapshot: Deletes the checkpoint. -Confirm:$false disables the confirmation prompt.
Expert Tip: Regularly audit snapshots and checkpoints to ensure that there are no outdated snapshots taking up space and affecting performance. Automate the process of deleting outdated snapshots using PowerShell or PowerCLI scripts. Set snapshot retention policies that define the maximum retention period for snapshots and checkpoints, as well as who is responsible for creating and deleting them.