top of page
Search

Backup and Restore a Virtual Machine using Snapshots

Updated: Aug 27, 2024

Introduction

Backing up and restoring virtual machines (VMs) is a critical aspect of IT infrastructure management, ensuring data protection and enabling rapid recovery in the event of hardware failures, data corruption, or other disasters.


For those interested in setting up a KVM virtualization environment, I’ve previously covered the installation process in detail. You can find the guide here.


KVM-Snapshot

  • A snapshot preserves (maintain in the original) the state and data of a virtual machine at a specific point in time.

  • The data includes all of the files that make up the virtual machine. This includes disks, memory, and other devices, such as virtual network interface cards.

  • Snapshot are useful for creating backups, testing software change and rolling back to a previous state if needed


Why are Snapshots Important?

Snapshots are a vital tool for IT administrators and developers alike. They allow for:


  1. Quick Recovery: In case of a failure, whether due to a software bug, configuration error, or system crash, snapshots allow the system to be restored to its last known good state, minimizing downtime.

  2. Testing and Development: Snapshots provide a safe environment to test new software or system configurations. If something goes wrong, the system can easily revert to the pre-testing state.

  3. Data Protection: Regular snapshots serve as backups, ensuring that critical data can be recovered in the event of corruption or accidental deletion.


Snapshot in Action

Imagine you're developing a new feature for a web application on a virtual machine. Before making significant changes to the codebase, you create a snapshot of the VM. After implementing the changes, you discover that the new code has introduced a bug that causes the application to crash. Instead of spending hours troubleshooting or, worse, losing progress, you can quickly revert to the snapshot taken before the changes. The VM returns to its previous stable state, and you can reattempt the code changes with a fresh start.


Managing Virtual Machine Snapshots

Snapshots are a crucial tool in managing virtual machines (VMs), allowing you to capture the state of a VM at a specific point in time. This guide walks you through the process of creating, restoring, and deleting snapshots using the virsh command-line tool, which is commonly used with KVM (Kernel-based Virtual Machine) environments.


1. Creating a Snapshot

Creating a snapshot involves saving the current state of your virtual machine, including its disk, memory, and device states. This is particularly useful before making changes to the VM, allowing you to revert to this state if something goes wrong.


Steps:

  • Open Terminal: Access the terminal on your Linux system.

  • Run the Snapshot Command: Use the following command to create a snapshot:

virsh snapshot-create-as fedvm2 omkar 

Explanation:

  • virsh: Command-line tool to interact with KVM.

  • snapshot-create-as: Sub-command used to create a snapshot.

  • fedvm: The name of the virtual machine.

  • omkar: The name of the snapshot.


  • A snapshot named "omkar" was created for the virtual machine "Fedvm2" using the virsh snapshot-create-as command.


2.  Restoring a Snapshot

If you encounter an issue with your VM after making changes, you can restore it to the state captured in a previous snapshot. This will revert the VM to its exact condition at the time the snapshot was taken.


Steps:

  • Identify the Snapshot: List the snapshots to identify the one you want to restore:

virsh snapshot-list fedvm2
  • Run the Restore Command: Use the following command to restore the snapshot:

virsh snapshot-revert fedvm2 omkar

Explanation:

  • snapshot-revert: Sub-command used to revert the VM to a specific snapshot.

  • omkar: The name of the snapshot to which you want to revert.


3. Deleting a Snapshot

Once a snapshot is no longer needed, it’s good practice to delete it to free up resources.


Steps:

  • Run the Delete Command: Use the following command to delete the snapshot:

virsh snapshot-delete fedvm2 omkar

Explanation:

  • snapshot-delete: Sub-command used to delete a specific snapshot.

  • omkar: The name of the snapshot to be deleted.


4. Execution

To demonstrate the power of snapshots, let’s walk through a real-world example.


Step 1: Preparing the VM

Before creating the snapshot, I created a file named omkar on the virtual machine and added the following content:

Hi, I am a Fedora.

Step 2: Creating the Snapshot


After ensuring that the VM was in a stable state with the omkar file in place, I took a snapshot using the following command:

virsh snapshot-create-as fedvm omkar --description "Snapshot before editing the omkar file"

Step 3: Modifying the File

To test the snapshot’s effectiveness, I edited the omkar file by adding another line of text:

hello snapshot

The file now contained:

Hi, I am a Fedora.
hello snapshot

Output:

  • After running command ‘snapshot-revert’ data in the file get reverted at initial state.

virsh snapshot-revert fedvm omkar

Output:

Conclusion

After reverting the snapshot, I checked the omkar file, and as expected, the content had been restored to its original state:

Hi, I am a Fedora.

This example clearly shows how snapshots can effectively safeguard your data and system state, allowing you to recover from changes quickly and efficiently. By using snapshots, you ensure that any unwanted modifications can be undone, keeping your virtual environment stable and reliable.



Resilient !!






 
 
 

Comments


bottom of page