LVM for MySQL backups

From Proven Scaling Wiki

Jump to: navigation, search

LVM is the Logical Volume Manager for Linux. See Wikipedia's entry on LVM for additional background information.

Background

LVM uses several important terms:

  • Physical Volume (PV) — This is a block device, normally a physical disk or hardware RAID volume such as /dev/sda. It could also be a partition on a disk, such as /dev/sda1. PVs do not have their own names, the name of the actual device is used to reference a PV.
  • Volume Group (VG) — This is a group of PVs, which can be expanded such that PVs can be combined in interesting ways. If you have hardware or software RAID, there is normally just a single PV per VG. Each VG is given a unique name by the sysadmin.
  • Logical Volume (LV) — This is an actual volume within a VG. A VG can contain many LVs, presented as a block devices, which can be formatted with a filesystem, mounted, and used. Each LV is given a unique name by the sysadmin. LVs appear in /dev using the device mapper, for instance: /dev/VolGroup00/foo.
  • Snapshot (sometimes SN) — LVs can be "snapshotted", which means that a new LV is created, with copy-on-write semantics from an existing LV (called the "origin"). The new snapshot LV only consumes the amount of space necessary to copy changed blocks (and some bookkeeping information). When you create the snapshot, you specify a size for it. If the snapshot becomes full, it will be deactivated and thus become unusable. Because a snapshot needs space, and is a new LV, you must have some free space left on the VG in order to create a snapshot of an LV. How much space you need depends entirely on how much write traffic is occurring on the LV during the time which the snapshot lives.

An example configuration

As an example, using a build/test/dev machine owned by Proven Scaling:

  • Physical Disk Configuration is a RAID 1+0 of 4 160GB SATA disks, presented as /dev/sda.
  • sda is partitioned into the following partitions:
    • sda1: 100M (/boot)
    • sda2: ~300G (LVM PV)
  • PV is created on sda2
  • VG called main (~300G) is created on PV sda2
  • LV called root (30G) is created on VG main
  • LV called data (235G) is created on VG main
  • LV called swap0 (2G) is created on VG main

This leaves ~30G free on the VG for snapshots.

You can examine the LVM configuration like so:

 [root@kamet ~]# pvs
    PV         VG   Fmt  Attr PSize   PFree
    /dev/sda2  main lvm2 a-   296.88G 30.50G
 [root@kamet ~]# vgs
    VG   #PV #LV #SN Attr   VSize   VFree
    main   1   3   0 wz--n- 296.88G 30.50G
 [root@kamet ~]# lvs
    LV    VG   Attr   LSize   Origin Snap%  Move Log Copy%
    data  main -wi-ao 234.38G
    root  main -wi-ao  30.00G
    swap0 main -wi-ao   2.00G

Taking MySQL backups

In order to take a MySQL backup, you follow this basic procedure:

  • FLUSH TABLES WITH READ LOCK — this will take a moment, closing all tables and blocking further writes and commits; hold the connection open until the UNLOCK TABLES below
  • lvcreate -LxxxM -s snapname /dev/vgname/lvname — creates a snapshot of xxx MB of lvname in vgname
  • UNLOCK TABLES — unlocks the tables
  • tar, rsync, etc. — copy the backup off of the snapshot
  • lvremove /dev/vgname/snapname — remove the snapshot
Personal tools