Friday, 10 February 2012

SNAPSHOT LINUX LV

LVM Snapshots
Snapshot Creation
All Pages

LVM Snapshots Overview


Continuing with our LVM how to series, in this KB, we will build on the knowledge from our LVM Configuration KB, and explore logical volume (LV) snapshots. We will show you how to create a LV snapshot which can then be mounted for a backup or some other purpose.
Linux administrators typically use LV snapshots for backups. Often times, running a backup can take anywhere from a couple of minutes to several hours (depending on the amount of data to backup.) Depending on how busy your system, from the time the backup begins to the time it completes, some of your files could have changed with omissions to open files. Snapshots provide a means of obtaining a consistent volume (free of changes) in a matter of seconds. The snapshot (which is a exact copy of the LV taken at the time the snapshot command was run) can then be backed-up without the worry of changes to the file system or need to shutdown running databases or close open files.
LVM snapshots can only be taken from logical volumes. Non LV partitions cannot have a snapshot taken of them.
Snapshots require only a fraction of the space required by the source LV. Behind the scenes, when a snapshot is taken, the source LV is frozen while the changes to the LV are written to the LV snapshot. Thus, the size of the snapshot only needs to be large enough for the anticipated changes to the file system over the lifetime of the snapshot. So in other words, the size of the source LV has no bearing on the size requirements for the snapshot LV, but rather how many changes will be made to the source LV throughout the lifetime of the existence of the snapshot LV.
Once the snapshot is removed, the changes logged in the LV snapshot are written back to the source LV. Should a snapshot LV run out of space, the snapshot will be released.
One cautionary note with LV snapshots, they are I/O intensive. I have seen I/O performance hits by more than 50% of the snapshot LV while running with a snapshot. Therefore, you should ensure that you do not run with a snapshot LV any longer than absolutely necessary.



Snapshot Creation


Creating a logical volume (LV) snapshot is much the same process as creating a LV (see our LVM Configuration KB for details on how to create a LV.) However, when creating a LV snapshot you must use a -s command line switch in conjunction with the LV you wish to snapshot. The syntax is as follows:
  • lvcreate -L <SIZE_OF_SNAPSHOT> -s -n <NAME_OF_SNAPSHOT> <LV_TO_SNAPSHOT>
In the following example, we will create a 500MB snapshot LV of an existing LV, lets being:
  1. Use lvdisplay to find the name of the logical volume you wish to snapshot

    [root@Linux01 ~]# lvdisplay
    --- Logical volume ---
    LV Name/dev/TCPDumpVolGRP/TCPDumpLV
    VG NameTCPDumpVolGRP
    LV UUIDhYQs4t-YtY7-51hl-c4ps-4N6d-2W7h-IidcxF
    LV Write Accessread/write
    LV Statusavailable
    # open1
    LV Size1.50 GB
    Current LE48
    Segments1
    Allocationinherit
    Read ahead sectorsauto
    - currently set to256
    Block device253:5

    ... OUTPUT TRUNCATED
  2. Create a new 500MB snapshot from the source LV /dev/TCPDumpVolGRP/TCPDumpLV

    [root@Linux01 /]# lvcreate -L 500M -s -n BackupLV /dev/TCPDumpVolGRP/TCPDumpLV
    Rounding up size to full physical extent 512.00 MB
    Logical volume "BackupLV" created
    [root@Linux01 ~]#
    Note: Our snapshot LV (500MB) is about 1/3 the size of the source LV (1.5 GB). As we have already said, the snapshot LV does not need to be the same size of the source. Because the snapshot LV will only contain the changes made to the source LV while its snapshot, and we know there is not a high rate of change on the source LV, we are fine using 1/3 the size (and would have probably been safe making it far less.)
  3. Verify the snapshot has been created

    [root@Linux01 ~]# lvdisplay
    --- Logical volume ---
    LV Name/dev/TCPDumpVolGRP/TCPDumpLV
    VG NameTCPDumpVolGRP
    LV UUIDhYQs4t-YtY7-51hl-c4ps-4N6d-2W7h-IidcxF
    LV Write Accessread/write
    LV Statusavailable
    # open1
    LV Size1.50 GB
    Current LE48
    Segments1
    Allocationinherit
    Read ahead sectorsauto
    - currently set to256
    Block device253:5

    --- Logical volume ---
    LV Name/dev/TCPDumpVolGRP/BackupLV
    VG NameTCPDumpVolGRP
    LV UUIDmSWMF0-5JtO-GkAd-plBb-YIf8-1HOg-JRfV34
    LV Write Accessread/write
    LV snapshot statusactive destination for /dev/TCPDumpVolGRP/TCPDumpLV
    LV Statusavailable
    # open0
    LV Size1.50 GB
    Current LE48
    COW-table size512.00 MB
    COW-table LE16
    Allocated to snapshot0.00%
    Snapshot chunk size4.00 KB
    Segments1
    Allocationinherit
    Read ahead sectorsauto
    - currently set to256
    Block device253:7
    ... OUTPUT TRUNCATED
    Note: You will notice on the BackupLV that although the LV size says that it's 1.5GB, the copy-on-write (COW) table informs us that it's actually only 512MB (the source LV is 1.5GB.) Also, the percentage allocated to the snapshot is currently at 0%. As updates are made to the source LV, you'll notice this percentage will increase.
  4. We can now mount the snapshot so that it may be backed-up

    [root@Linux01 /]# mkdir -p /mnt/backup
    [root@Linux01 /]# mount /dev/TCPDumpVolGRP/BackupLV /mnt/backup/
    [root@Linux01 /]# df -kh

    FilesystemSizeUsedAvailUse%Mounted on
    /dev/mapper/VolGroup00-LogVol043.9G2.2G1.6G59%/
    /dev/sda199M12M82M13%/boot
    tmpfs1006M01006M0%/dev/shm
    /dev/mapper/VolGroup00-LogVol00992M41M901M5%/home
    /dev/mapper/VolGroup00-LogVol02992M69M872M8%/tmp
    /dev/mapper/VolGroup00-LogVol032.0G150M1.7G8%/var
    /dev/mapper/TCPDumpVolGRP-4GLV4.0G137M3.7G4%/4GLV
    /dev/mapper/TCPDumpVolGRP-TCPDumpLV1.5G920M497M65%/TCPDumpLV
    /dev/mapper/TCPDumpVolGRP-BackupLV1.5G920M497M65%/mnt/backup
  5. Once the snapshot has been backed-up, unmount the LV and remove it

    [root@Linux01 /]# umount /mnt/backup/
    [root@Linux01 /]# lvremove /dev/mapper/TCPDumpVolGRP-BackupLV
    Do you really want to remove active logical volume "BackupLV"? [y/n]: y
    Logical volume "BackupLV" successfully removed
    [root@Linux01 /]#
Nice work, you now know how-to snapshot a LV. Good luck!

No comments:

Post a Comment