Saturday, November 10, 2012

LVM Snapshot

LVM Snapshot is one of the good methods  data backup and restore. You can take a lvm snapshot for huge data. Here i took lvm snapshot for 60G with 300M but you should actually calculate the size according to the data you take the backup.

-s, --snapshot OriginalLogicalVolume{Name|Path}

              Create  a  snapshot  logical volume (or snapshot) for an existing, so called original logical volume (or origin).  Snapshots provide a ’frozen image’ of  the  contents  of the  origin while the origin can still be updated. They enable consistent backups and online recovery of removed/overwritten data/files.  Thin snapshot is created when the origin  is  a  thin  volume  and the size is not specified. Thin snapshot shares same blocks within the thin pool volume.  The snapshot with the specified  size  does  not need  the  same amount of storage the origin has. In a typical scenario, 15-20% might be enough.  In case the snapshot runs out of storage, use  lvextend(8)  to  grow  it. Shrinking  a  snapshot  is  supported by lvreduce(8) as well. Run lvdisplay(8) on the snapshot in order to check how much data is allocated  to  it.   Note  that  a  small
amount  of  the  space you allocate to the snapshot is used to track the locations of the chunks of data, so you should allocate slightly more space than you actually need and  monitor  the rate at which the snapshot data is growing so you can avoid running  out of space.

So, for a 100 GB LV, We can start with 20 GB for the snapshot volume.


A snapshot volume ONLY contains the files that have been changed since it was created. i.e. If you only changed 2kb of files since the snapshot... the snapshot consumes 2kb of disk space. It is a good idea to plan on allowing the snapshot sufficient space to do what you need before the snapshot is removed... i.e. if you are going to make 100gb of changes after the snaphot is taken... you need to allocate 100gb to the snapshot.. so it can consume 100gb if needed.

Actually, the space that is in actual use by the snapshot can be easily seen after activating the volumes (e.g., with vgchange -a y) -- in percents of the total space allocated for the snapshot:

it is shown in the Allocated to snapshot row of the output of lvdisplay, or by the snap_percent option of lvs.

(I simply was examining the volumes without activating them -- that's why I couldn't see this info straight on.)

If, for example, it's 5%, then you can simply use lvreduce -l 5%LV (or a bit more) to recalim all the unused space from the snapshot volume.

We can take a backup of this using copy command or the dd command.

There is also auto extend option for copy on write in lvm snapshot, we need to specify the percent in /etc/lvm.conf file  auto extend  line.

Example:

[root@ tester~]# lvcreate -L300M -s -n test /dev/VG1/public
  Rounding up size to full physical extent 320.00 MB
  Logical volume "test" created
Here test is the name of the snapshot and its size will be 300M This is a snapshot for logical
volume public.

[root@ tester~]# mkdir /mnt/test
[root@ tester~]# lvdisplay

 --- Logical volume ---
  LV Name                /dev/VG1/test
  VG Name                VG1
  LV UUID                SetE4Z-x9oD-72dH-tKc3-PltJ-vbPk-HXGZmv
  LV Write Access        read/write
  LV snapshot status     active destination for /dev/VG1/public
  LV Status              available
  # open                 0
  LV Size                60.00 GB
  Current LE             1920
  COW-table size         320.00 MB
  COW-table LE           10
  Allocated to snapshot  0.00%
  Snapshot chunk size    4.00 KB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:4

[root@ tester ~]# mount -t ext3 /dev/VG1/test /mnt/test

cd /mnt/test

[root@ test]# ls

It now has the same data like the logical group public.
cd

You can now remove a file or data from the original one and test it.
Then copy the same file from the snapshot. It works..

[root@ tester~]# tar -cf /dev/VG1/test /mnt/test
To take backup of the lvm snapshot.

[root@tester ~]# umount /dev/VG1/test /mnt/test
umount: /dev/VG1/test: not mounted
umount: /mnt/test: not mounted

[root@ tester~]# lvremove /dev/VG1/test
Do you really want to remove active logical volume test? [y/n]: y
  Logical volume "test" successfully removed

No comments: