LVM Snapshots for MySQL Backups

Say that you are working with some data that is constantly changing, like some MySQL databases, and you are charged to take a valid backup without any data inconsistency or downtime. You may consider using mysqldump; however, this utility will lock tables during the duration of the backup and can cause down time (especially if using MyISAM table engine which performs Table level locking). Then, you think you may just copy or archive the MySQL table files (/var/lib/mysql/DB), however, this can cause data inconsistent as during the copy some of the data may of changed. Not to mention, if it is InnoDB table engine all table data is stored in the ibdata files.

At this point you may be scratching your head not knowing what to do. You could stand up another server and setup MySQL replication. Unfortunately, you do not have any available server’s or workstation to setup for this purpose. Fortunately, for you, this server will setup by default with LVM (Logical Volume Management) by your distribution. Using LVM, we are able to create a snapshot of an existing Logical Volume using free space on the Volume Group. Lets go ahead and see some of the syntax and examples below.

Below we have our Logical Volume mounted on /var/lib/mysql.


# df -h /var/lib/mysql/

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/mysql-lvol0

591M   38M  524M   7% /var/lib/mysql

So lets go ahead and check out the information about this Logical Volume.


# lvdisplay /dev/mysql/lvol0

--- Logical volume ---

LV Name                /dev/mysql/lvol0

VG Name                mysql

LV UUID                BW3zO3-KVvQ-ABvt-4ylN-bw8t-ivvj-Vsp48P

LV Write Access        read/write

LV Status              available

# open                 1

LV Size                600.00 MB

Current LE             150

Segments               1

Allocation             inherit

Read ahead sectors     auto

- currently set to     256

Block device           253:2

Now lets check the Volume Group this Logical Volume is apart of and see if there is any free space.


# vgdisplay mysql

--- Volume group ---

VG Name               mysql

System ID

Format                lvm2

Metadata Areas        1

Metadata Sequence No  2

VG Access             read/write

VG Status             resizable

MAX LV                0

Cur LV                1

Open LV               1

Max PV                0

Cur PV                1

Act PV                1

VG Size               1020.00 MB

PE Size               4.00 MB

Total PE              255

Alloc PE / Size       150 / 600.00 MB

Free  PE / Size       105 / 420.00 MB

VG UUID               PtaSHo-cdQ4-ofy1-ug6O-7q3z-OLS6-4oAyRt

As we do have an available 420 MB or 105 PE (Physical extents) lets go ahead and create a snapshot, mount it and view its contents.


# lvcreate --size 400MB --snapshot --name snap /dev/mysql/lvol0

Logical volume "snap" created

# mount /dev/mysql/snap /mnt/

# ls -l /mnt

total 20568

-rw-rw---- 1 mysql mysql 10485760 Feb 24 09:19 ibdata1

-rw-rw---- 1 mysql mysql  5242880 Feb 24 09:25 ib_logfile0

-rw-rw---- 1 mysql mysql  5242880 Feb 24 09:18 ib_logfile1

drwx------ 2 mysql mysql    16384 Feb 24 09:14 lost+found

drwx------ 2 mysql mysql     4096 Feb 24 09:18 mysql

srwxrwxrwx 1 mysql mysql        0 Feb 24 09:25 mysql.sock

drwx------ 2 mysql mysql     4096 Feb 24 09:18 test

Now we have a snapshot of our MySQL data which will not change even though the live data mounted at /var/lib/mysql is contently changing. Using this snapshot we can safely copy the contents and if need be use for a restore in the future.

RHEL Logical Volume Manager

Setting up a LVM

Get the Flash Player to see this player.

1. Create Partition on the Disk using fdisk

-> fdisk /dev/sdc

n (add new partition)
p (Primary Partition)
1 (Start at 1st cylinder)
512 (end at last cylinder)
t (change partition type)
num (the number of the partition, refer to your parittion listing)
83 (LVM partition type)
w (Write Changes)

(Always run partprobe after altering your partition table to detect changes)
-> partprobe

2. Register the Physical Volume

-> pvcreate /dev/sdc1

3. Create Volume Group

-> vgcreate pool_of_space /dev/sdc1

4. Create Logical Volume

-> lvcreate -n usable_space -l 255 pool_of_space

5. Format with Filesystem

-> mkfs.ext3 /dev/pool_of_space/usable_space

Append space to current LVM

Get the Flash Player to see this player.

1. Create Partition
-> create a new partition as mentioned in the above section.

2. Create partition in to Physical volume.
-> pvcreate /dev/sdc2

3. Extend the Volume Group of your choice.
-> vgextend pool_of_space /dev/sda2

4. Extend your Logical Volume by adding additional space/ physical extents to it.
lvextend -l +400 /dev/pool_of_space/usable_space

5. Resize your filesystem
resize2fs /dev/pool_of_space/usable_space

Shrink your LVM

Get the Flash Player to see this player.

1. In order to shrink a Filesystem you most unmount that filesystem
-> umount /dev/pool_of_space/usable_space

2. Check the filesystem for consistency before shrinking
-> e2fsck -f /dev/pool_of_space/usable_space

3. Resize your filesystem down to the size you wish
-> resize2fs /dev/pool_of_space/usable_space 1G

4. Remove those now unused Physical extents from your Logical Volume
-> lvreduce -L 1G /dev/pool_of_space/usable_space