Chapter 17. Volume Snapshots

Contents

17.1. Understanding Volume Snapshots
17.2. Creating Linux Snapshots with LVM
17.3. Monitoring a Snapshot
17.4. Deleting Linux Snapshots
17.5. Merging a Snapshot with the Original Volume to Revert Changes or Roll Back to a Previous State

A file system snapshot is a copy-on-write technology that monitors changes to an existing volume’s data blocks so that when a write is made to one of the blocks, the block’s value at the snapshot time is copied to a snapshot volume. In this way, a point-in-time copy of the data is preserved until the snapshot volume is deleted.

17.1. Understanding Volume Snapshots

A file system snapshot contains metadata about and data blocks from an original volume that have changed since the snapshot was taken. When you access data via the snapshot, you see a point-in-time copy the original volume. There is no need to restore data from backup media or to overwrite the changed data.

[Important]

During the snapshot’s lifetime, the snapshot must be mounted before its original volume can be mounted.

In a Xen host environment, the virtual machine must be using an LVM logical volume as its storage back-end, as opposed to using a virtual disk file.

Linux snapshots allow you to create a backup from a point-in-time view of the file system. The snapshot is created instantly and persists until you delete it. You can backup the file system from the snapshot while the volume itself continues to be available for users. The snapshot initially contains some metadata about the snapshot, but no actual data from the original volume. Snapshot uses copy-on-write technology to detect when data changes in an original data block. It copies the value it held when the snapshot was taken to a block in the snapshot volume, then allows the new data to be stored in the original block. As blocks change from their original value, the snapshot size grows.

When you are sizing the snapshot, consider how much data is expected to change on the original volume and how long you plan to keep the snapshot. The amount of space that you allocate for a snapshot volume can vary, depending on the size of the original volume, how long you plan to keep the snapshot, and the number of data blocks that are expected to change during the snapshot’s lifetime. The snapshot volume cannot be resized after it is created. As a guide, create a snapshot volume that is about 10% of the size of the original logical volume. If you anticipate that every block in the original volume will change at least one time before you delete the snapshot, then the snapshot volume should be at least as large as the original volume plus some additional space for metadata about the snapshot volume. Less space is required if the data changes infrequently or if the expected lifetime is sufficiently brief.

Beginning in SLES 11 SP3, an LVM logical volume snapshots can be thinly provisioned. Thin provisioning is assumed if you to create a snapshot without a specified size. The snapshot is created as a thin volume that uses space as needed from a thin pool. A thin snapshot volume has the same characteristics as any other thin volume. You can independently activate the volume, extend the volume, rename the volume, remove the volume, and even snapshot the volume.

[Important]

To use thinly provisioned snapshots in a cluster, the original volume and its snapshots must be managed in a single cluster resource. This allows the volume and its snapshots to always be mounted exclusively on the same node.

When you are done with the snapshot, it is important to remove it from the system. A snapshot eventually fills up completely as data blocks change on the original volume. When the snapshot is full, it is disabled, which prevents you from remounting the original volume.

Remove snapshots in a last created, first deleted order.

17.2. Creating Linux Snapshots with LVM

The Logical Volume Manager (LVM) can be used for creating snapshots of your file system.

  • Open a terminal console, log in as the root user, then enter

    lvcreate -s [-L <size>] -n snap_volume source_volume_path
    

    If no size is specified, the snapshot is created as a thin snapshot.

    For example:

    lvcreate -s -L 1G -n linux01-snap /dev/lvm/linux01
    

    The snapshot is created as the /dev/lvm/linux01-snap volume.

17.3. Monitoring a Snapshot

  • Open a terminal console, log in as the root user, then enter

    lvdisplay snap_volume
    

    For example:

    lvdisplay /dev/vg01/linux01-snap
    
    --- Logical volume ---
      LV Name                /dev/lvm/linux01
      VG Name                vg01
      LV UUID                QHVJYh-PR3s-A4SG-s4Aa-MyWN-Ra7a-HL47KL
      LV Write Access        read/write
      LV snapshot status     active destination for /dev/lvm/linux01
      LV Status              available
      # open                 0
      LV Size                80.00 GB
      Current LE             1024
      COW-table size         8.00 GB
      COW-table LE           512
      Allocated to snapshot  30%
      Snapshot chunk size    8.00 KB
      Segments               1
      Allocation             inherit
      Read ahead sectors     0
      Block device           254:5
    

17.4. Deleting Linux Snapshots

  • Open a terminal console, log in as the root user, then enter

    lvremove snap_volume_path
    

    For example:

    lvremove /dev/lvmvg/linux01-snap
    

17.5. Merging a Snapshot with the Original Volume to Revert Changes or Roll Back to a Previous State

Snapshots can be useful if you need to roll back or restore data on a volume to a previous state. For example, you might need to revert data changes that resulted from an administrator error or a failed or undesirable package installation or upgrade.

You can use the lvconvert --merge command to revert the changes made to an LVM logical volume. The merging begins as follows:

  • If both the original volume and snapshot volume are not open, the merge begins immediately.

  • If the original volume or snapshot volume are open, the merge starts the first time either the original volume or snapshot volume are activated and both are closed. You can manually deactivate and activate the original volume to get the merge to start.

  • If the original volume cannot be closed, such as the root file system, the merge is deferred until the next time the server reboots and the original volume is activated. You can manually restart the server and then activate and mount the original volume to get the merge to start.

After a merge begins, the merge continues automatically after server restarts until it is complete. A new snapshot cannot be created for the original volume while a merge is in progress.

While the merge is in progress, reads or writes to the original volume are transparently redirected to the snapshot that is being merged. This allows users to immediately view and access the data as it was when the snapshot was created. They do not need to wait for the merge to complete.

When the merge is complete, the original volume contains the same data as it did when the snapshot was taken, plus any data changes made after the merge began. The resulting logical volume has the original volume’s name, minor number, and UUID. The original volume is automatically remounted, and the snapshot volume is removed.

  1. Open a terminal console, log in as the root user, then enter

    lvconvert --merge  [-b] [-i <seconds>] [<snap_volume_path>[...<snapN>]|@<volume_tag>]
    

    You can specify one or multiple snapshots on the command line. You can alternatively tag multiple original volumes with the same volume tag then specify @<volume_tag> on the command line. The snapshots for the tagged volumes are merged to their respective original volumes. For information about tagging logical volumes, see Section 4.7, “Tagging LVM2 Storage Objects”.

    The options include:

    -b, --background

    Run the daemon in the background. This allows multiple specified snapshots to be merged concurrently in parallel.

    -i, --interval <seconds>

    Report progress as a percentage at regular intervals. Specify the interval in seconds.

    For more information about this command, see the lvconvert(8) man page.

    For example:

    lvconvert --merge /dev/lvmvg/linux01-snap
    

    This command merges /dev/lvmvg/linux01-snap into its original volume.

    lvconvert --merge @mytag
    

    If lvol1, lvol2, and lvol3 are all tagged with mytag, each snapshot volume is merged serially with its respective original volume; that is: lvol1, then lvol2, then lvol3. If the --background option is specified, the snapshots for the respective tagged logical volume are merged concurrently in parallel.

  2. (Optional) If both the original volume and snapshot volume are open and they can be closed, you can manually deactivate and activate the original volume to get the merge to start immediately.

    umount <original_volume>
    lvchange -an <original_volume>
    lvchange -ay <original_volume>
    mount <original_volume> <mount_point>
    

    For example:

    umount /dev/lvmvg/lvol01
    lvchange -an /dev/lvmvg/lvol01
    lvchange -ay /dev/lvmvg/lvol01
    mount /dev/lvmvg/lvol01 /mnt/lvol01
    
  3. (Optional) If both the original volume and snapshot volume are open and the original volume cannot be closed, such as the root file system, you can restart the server and mount the original volume to get the merge to start immediately after the restart.