Linux & Servers guides, explanations and reader checklists on whilenetworking.com

Implementing LVM Snapshot Chains to Enable Point-in-Time Recovery for Production MySQL

In high-availability production environments, the ability to revert a database to a specific moment in time is often more critical than a full…

In high-availability production environments, the ability to revert a database to a specific moment in time is often more critical than a full backup-and-restore cycle. While traditional backups provide safety nets at discrete intervals (e.g., every 24 hours), they leave a data gap where corruption or accidental deletion goes unnoticed until the next snapshot window. Logical Volume Manager (LVM) snapshot chains offer a mechanism to capture point-in-time states without halting the database service, provided the underlying filesystem supports copy-on-write operations. This guide details how to implement and manage LVM snapshot chains specifically for production MySQL instances running on Linux systems utilizing xfs or ext4.

Understanding the Architecture of Snapshot Chains

Before executing any commands, it is essential to understand the mechanics. An LVM snapshot is a read-only view of a logical volume at a specific instant. When data changes in the original volume, the copy-on-write (COW) mechanism copies changed blocks to a reserved space in the snapshot device before writing the old blocks back to the source.

A “snapshot chain” involves creating multiple snapshots sequentially. This allows administrators to navigate through history: if a rollback fails at the most recent point-in-time, the previous snapshot in the chain remains intact. For MySQL, this is particularly useful during dangerous operations like running ALTER TABLE on large InnoDB tables or applying risky patches that might corrupt data structures.

Prerequisites:
* A Linux server with LVM installed (lvm2).
* The root filesystem or a dedicated /var/lib/mysql volume must be on an LVM-managed device (e.g., vg0/lv_mysql_data).
* Sufficient free space in the Volume Group (VG) to hold the snapshot overhead. A common rule of thumb is that the snapshot size should be at least 10% of the expected change rate during the operation window.
* MySQL running with InnoDB storage engine enabled.

Creating a Snapshot Chain for Live Databases

The primary goal is to create a snapshot while the MySQL service remains online. Unlike filesystem snapshots on some older systems, modern LVM tools allow snapshot creation with minimal performance impact if the snapshot size is adequate.

  1. Identify the Logical Volume: Determine the device path containing your MySQL data. In this scenario, we assume /dev/vg0/lv_mysql_data.
  2. Create the Initial Snapshot: Use lvcreate to create a read-only snapshot with a specific size (e.g., 10% of the LV).
# Create a snapshot named snap_mysql_t10 with 5GB free space
sudo lvcreate -L 5G -s -n snap_mysql_t10 vg0/lv_mysql_data
  • -L 5G: Allocates 5 gigabytes of space for the snapshot buffer.
  • -s: Specifies this is a snapshot of an existing LV.
  • -n snap_mysql_t10: Names the new snapshot device.

Expected Output:

  Logical volume "snap_mysql_t10" created.
  1. Attach and Mount (Read-Only): To inspect the data or copy files to a temporary location for analysis, mount the snapshot read-only.
sudo mkdir -p /mnt/mysql_snap
sudo mount /dev/vg0/snap_mysql_t10 /mnt/mysql_snap

Verification Step: Verify that the mount is read-only and the inode count matches the source (excluding new blocks written since the snapshot).

mount | grep snap_mysql_t10
# Output should show: ... /mnt/mysql_snap on /mnt/mysql_snap type xfs (ro,relatime,...)

Managing Snapshot Chains and Rollback Procedures

The true power of a snapshot chain emerges during recovery. If a MySQL update causes corruption or an accidental DROP DATABASE occurs, you can detach the latest snapshot, promote it to a writable volume, and restore data.

Scenario: A critical bug is introduced in a deployed patch at 14:00 UTC. The administrator needs to roll back exactly to 13:59 UTC.

  1. Detach the Snapshot: Unmount the snapshot device to prepare for modification.
    bash
    sudo umount /mnt/mysql_snap
  2. Promote the Snapshot: Convert the read-only snapshot into a writable logical volume. This involves copying the snapshot’s data back to the original LV structure or creating a new LV from the snapshot image.
    bash
    # Option A: Create a new LV from the snapshot (Cleaner for production)
    sudo lvcreate -L 50G -s -r -n lv_mysql_restore vg0/lv_mysql_data
    # Note: The '-s -r' flags may vary by LVM version; often requires manual copy or specific tool like 'lvconvert --merge' in newer versions.

Note on lvconvert --merge: Modern LVM (2.03+) supports merging snapshots directly if the underlying filesystem is XFS. This automatically promotes the snapshot and merges it with the original LV, preserving the original LV’s name.

# Merging a snapshot into the original logical volume (XFS specific)
sudo lvconvert --merge /dev/vg0/snap_mysql_t10
  1. Restart MySQL: Once the filesystem is merged and writable, restart the MySQL service to load the rolled-back data state.
    bash
    sudo systemctl restart mysql

Systemd Integration and Monitoring Best Practices

To automate snapshot creation before risky operations, integrate the process into systemd. You can create a custom unit file or use existing tools like crontab with systemd timers.

Example: Creating a Pre-Operation Snapshot Unit

Create /etc/systemd/system/mysql-pre-snapshot.service:

[Unit]
Description=Create LVM snapshot before risky MySQL operation
After=mysql.service

[Service]
Type=oneshot
ExecStart=/bin/bash -c "/sbin/lvcreate -L 5G -s -n snap_mysql_auto vg0/lv_mysql_data"
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Enable the timer to run before a specific job or via manual trigger:

sudo systemctl enable mysql-pre-snapshot.service

Monitoring and Troubleshooting:
Monitor the snapshot size usage. If the snapshot fills up, it becomes invalid immediately, leading to data loss in the snapshot chain. Use lvs with the -o+lv_attr,pool_lv,data_percent flags to monitor health.

sudo lvs -o+lv_attr,pool_lv,data_percent
  • Common Error: “Invalid snapshot” or “Read-only file system”.
  • Fix: This indicates the snapshot buffer is full. You must either extend the original LV, delete old snapshots to free space, or restore from a previous chain member before the current one fails.

Student Practice Task: Simulating a Recovery Event

For students and junior engineers, practicing in a safe lab environment is crucial. Do not perform these steps on production data without rigorous testing.

Lab Scenario:
1. Set up a virtual machine with Ubuntu Server, LVM, and MySQL installed.
2. Populate the database with test data (e.g., create a table test_events and insert 10 rows).
3. Create an LVM snapshot of the data volume.
4. Delete all rows from test_events.
5. Attempt to restore using the snapshot chain by merging the snapshot back into the original LV.
6. Verify that the 10 rows are restored and the database is writable again.

Industry Application:
In a production setting, this workflow is often part of a “Change Advisory Board” (CAB) process. Before deploying a hotfix or migrating schemas, the operations team creates a snapshot chain. If the deployment fails health checks or causes performance degradation, the rollback procedure activates within minutes rather than hours, minimizing downtime and reputational damage. Always verify policy terms with your organization’s disaster recovery plan (DRP) regarding retention periods for snapshots, as storage costs often dictate how far back you can go in time.

Educational-use notice: This tutorial is for learning and controlled lab use only. Test carefully, use backups, and obtain authorization before making changes. Do not run unfamiliar commands on production systems. We accept no responsibility for misuse, damage, data loss, or production issues.