LVM: Managing snapshots

LVM: Managing snapshots

# Tested on RHEL 6 & 7

# LVM allows to take read/write snapshots of a logical volume. An snapshot consists of a
# point-in-time copy of a logical volume that can be written to independently from the
# original volume.

# They can be used for backup purposes or test changes, for example.

# When a snapshot is created it points to the same block on disk as the original but when
# a change is made to either the original or the snapshot, it will be copied and snapshot
# will point to the copy instead.

# To store changes a snapshot is given an initial space. Once it has used all the
# available space it will be turned 'inactive'. To avoid this, we can configure snapshot
# to automatically extend the space available when a defined threshold is reached.

# We can define following variables in /etc/lvm/lvm.conf configuration file:
#     snapshot_autoextend_threshold
#     snapshot_autoextend_percent
#
# When usage goes above 'snapshot_autoextend_threshold' percent, it will be grown by
# 'snapshot_autoextend_percent' of the original size. Once snapshot has reached the same
# size as the original logical volume it will stop being extended.
#
# lvm2-monitor service has to be restarted for changes to take effect


# Creating the snapshot of an existing logical volume

LV=lv_test         # Our existing logical volume
SNAP=lv_snapshot  
# Snapshot
VG=datavg          # Volume group our logical volume belongs to
SIZE=256M          # Desired size for the snapshot - do your calculations before ;)

lvcreate -s -n $SNAP -L $SIZE /dev/$VG/$LV


# To check how much space has been used in a snapshot, we can use lvs

lvs
  LV          VG     Attr      LSize     Pool   Origin    Data%  Move Log Cpy%Sync Convert
  [...]
  lv_snapshot rootvg swi-a-s-- 256.00m          lv_test   0.01



# Snapshot cat be mounted to make modifications

mount /dev/$VG/$SNAP /mnt


# Once modifications done, we can merge changes to origin logical volume (both original
# lv and snapshot should be unmounted, if not next time lv will be de-activated and
# re-activated it will be synchronized). If changes may be discarded we can 'lvremove'
# snapshot

# NOTE: Use snapshot merging carefully; merging will discard any change done to original
# logical volume after taking the snapshot
# Apart from that, one has to take into account that merging logical volumes will
# definitely destroy snapshot.

umount /dev/$VG/$LV
umount /mnt

lvconvert --merge /dev/$VG/$SNAP
  Merging of volume lv_snapshot started.
  lv_test: Merged: 100.0%
  lv_test: Merged: 100.0%
  Merge of snapshot into logical volume lv_test has finished.
  Logical volume "lv_snapshot" successfully removed


# We may run merge in the background ('-b'). If the original volume is in use, the merge
# will be started next time the original volume is activated. Obviously merging a snapshot
# of the root file system requires a reboot of the server




# Example of snapshot
# ------------------------------------------------------------------------------------------

# lvs rootvg/lv_test
  LV      VG     Attr      LSize   Pool Origin Data%  Move Log Cpy%Sync Convert
  lv_test rootvg -wi-ao--- 256.00m

# ll /test/
total 12
-rw-r--r-- 1 root root     0 Aug  5 16:19 1.txt
-rw-r--r-- 1 root root     0 Aug  5 16:19 2.txt
drwx------ 2 root root 12288 Aug  5 16:06 lost+found

# lvcreate -s -n lv_snapshot -L 32M /dev/rootvg/lv_test
  Logical volume "lv_snapshot" created

# mount /dev/rootvg/lv_snapshot /mnt

# ll /mnt/
total 12
-rw-r--r-- 1 root root     0 Aug  5 16:19 1.txt
-rw-r--r-- 1 root root     0 Aug  5 16:19 2.txt
drwx------ 2 root root 12288 Aug  5 16:06 lost+found

# touch /mnt/3.txt
# touch /mnt/4.txt

# touch /test/5.txt

# ll /test/
total 12
-rw-r--r-- 1 root root     0 Aug  5 16:19 1.txt
-rw-r--r-- 1 root root     0 Aug  5 16:19 2.txt
-rw-r--r-- 1 root root     0 Aug  5 16:21 5.txt
drwx------ 2 root root 12288 Aug  5 16:06 lost+found

# ll /mnt/
total 12
-rw-r--r-- 1 root root     0 Aug  5 16:19 1.txt
-rw-r--r-- 1 root root     0 Aug  5 16:19 2.txt
-rw-r--r-- 1 root root     0 Aug  5 16:21 3.txt
-rw-r--r-- 1 root root     0 Aug  5 16:21 4.txt
drwx------ 2 root root 12288 Aug  5 16:06 lost+found

# lvs rootvg/lv_test rootvg/lv_snapshot
  LV          VG     Attr      LSize   Pool Origin  Data%  Move Log Cpy%Sync Convert
  lv_snapshot rootvg swi-aos--  32.00m      lv_test   0.12
  lv_test     rootvg owi-aos-- 256.00m

# umount /test
# umount /mnt

# lvconvert --merge /dev/rootvg/lv_snapshot
  Merging of volume lv_snapshot started.
  lv_test: Merged: 100.0%
  Merge of snapshot into logical volume lv_test has finished.
  Logical volume "lv_snapshot" successfully removed

# lvs rootvg/lv_test rootvg/lv_snapshot
  One or more specified logical volume(s) not found.
  LV      VG     Attr      LSize   Pool Origin Data%  Move Log Cpy%Sync Convert
  lv_test rootvg -wi-a---- 256.00m

# mount /dev/rootvg/lv_test /test

# ll /test
total 12
-rw-r--r-- 1 root root     0 Aug  5 16:19 1.txt
-rw-r--r-- 1 root root     0 Aug  5 16:19 2.txt
-rw-r--r-- 1 root root     0 Aug  5 16:21 3.txt
-rw-r--r-- 1 root root     0 Aug  5 16:21 4.txt
drwx------ 2 root root 12288 Aug  5 16:06 lost+found


# Files 3.txt and 4.txt that were created on snapshot have been merged to original LV.
# On the other hand file 5.txt, that was created on original LV after having taken the
# snapshot, has been discarded during merge operation.
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
HowTo: Send Email from an SMTP Server using the Command Line
Viewed 1838 times since Mon, Feb 18, 2019
RHEL: Getting/Setting hardware clock’s time
Viewed 2927 times since Sat, Jun 2, 2018
Watchdog script to keep an application running
Viewed 18406 times since Tue, Jul 31, 2018
Find All Large Files On A Linux System
Viewed 2072 times since Mon, Oct 29, 2018
What is OS Watcher Utility and How to use it for Database Troubleshooting ?
Viewed 29937 times since Thu, Jun 21, 2018
RHEL: How to rebuild and/or patch a RPM package
Viewed 8534 times since Sat, Jun 2, 2018
RHEL: GPT/MBR partition tables (using disks larger than 2 TiB)
Viewed 12182 times since Sun, May 27, 2018
LVM: Reduce SWAP size by shrinking existing Logical Volume
Viewed 6086 times since Sat, Jun 2, 2018
Easily Find Bugs In Shell Scripts With ShellCheck
Viewed 3233 times since Thu, Apr 18, 2019
OEL 7 – How to disable IPv6 on Oracle Linux 7
Viewed 20161 times since Fri, Aug 3, 2018