Back Up (And Restore) LVM Partitions With LVM Snapshots

Version 1.1
Author: Falko Timme

This tutorial shows how you can create backups of LVM partitions with an LVM feature called LVM snapshots. An LVM snapshot is an exact copy of an LVM partition that has all the data from the LVM volume from the time the snapshot was created. The big advantage of LVM snapshots is that they can be used to greatly reduce the amount of time that your services/databases are down during backups because a snapshot is usually created in fractions of a second. After the snapshot has been created, you can back up the snapshot while your services and databases are in normal operation.

I will also show how to restore an LVM partition from a backup in an extra chapter at the end of this tutorial.

This document comes without warranty of any kind! I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

I have tested this on a Debian Etch server with the IP address 192.168.0.100 and the hostname server1.example.com. It has two hard disks:

  • /dev/sda (10GB) that contains a small /boot partition (non-LVM), a / partition (LVM, a little less than 10GB), and a swap partition (LVM)
  • /dev/sdb (60GB), unused at the moment; will be used to create a 30GB /backups partition (LVM) and for the snapshots of the / partition (10GB - that's enough because the / partition is a little less than 10GB).

I have created a Debian Etch VMware image that you can download and run in VMware Server or VMware Player (see https://www.howtoforge.com/import_vmware_images to learn how to do that). It has the same specifications as my test system from above. The root password is howtoforge. Using that VMware image, you can do the exact same steps than me in this tutorial to get used to using LVM snapshots.

To restore the / partition from your backup (covered in the last chapter of this tutorial) you need a Linux Live-CD that supports LVM, such as Knoppix or the Debian Etch Netinstall CD which you can use as a rescue CD if you specify rescue at the boot prompt. I will use the Debian Etch Netinstall CD in this example (the list of mirrors is available here: http://www.debian.org/CD/http-ftp/ - I downloaded this one: http://ftp.de.debian.org/debian-cd/4.0_r0/i386/iso-cd/debian-40r0-i386-netinst.iso).

To create a backup of the / partition I will proceed as follows: I will create a snapshot of the / partition, and afterwards I will create a backup of the snapshot (instead of the actual / partition!) on the /backups partition (of course, you can store that backup wherever you want - instead of creating an extra /backups LVM partition, you could also use an external USB drive). The backup can be made using your preferred backup solution, e.g. with tar or dd. Afterwards, I'll destroy the snapshot because it isn't needed anymore and would use system resources.

You don't necessarily need a second HDD for the snapshots - you can use the first one provided you have enough free (unpartitioned) space left on it to create snapshots on it (you should use the same space for the snapshots that you use for the partition that you want to back up). And as mentioned before, you can use a USB drive for backing up the snapshots.

To learn more about LVM, you should read this tutorial: https://www.howtoforge.com/linux_lvm

 

2 Create The /backups LVM Partition

(If you'd like to store your backups somewhere else, e.g on an external USB drive, you don't have to do this.)

Our current situation is as follows:

pvdisplay

server1:~# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               server1
  PV Size               9.76 GB / not usable 0
  Allocatable           yes (but full)
  PE Size (KByte)       4096
  Total PE              2498
  Free PE               0
  Allocated PE          2498
  PV UUID               vQIUga-221O-GIKj-81Ct-2ITT-bKPw-kKElpM

vgdisplay

server1:~# vgdisplay
  --- Volume group ---
  VG Name               server1
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               9.76 GB
  PE Size               4.00 MB
  Total PE              2498
  Alloc PE / Size       2498 / 9.76 GB
  Free  PE / Size       0 / 0
  VG UUID               jkWyez-c0nT-LCaE-Bzvi-Q4oD-eD3Q-BKIOFC

lvdisplay

server1:~# lvdisplay
  --- Logical volume ---
  LV Name                /dev/server1/root
  VG Name                server1
  LV UUID                UK1rjH-LS3l-f7aO-240S-EwGw-0Uws-5ldhlW
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                9.30 GB
  Current LE             2382
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           254:0

  --- Logical volume ---
  LV Name                /dev/server1/swap_1
  VG Name                server1
  LV UUID                2PASi6-fQV4-I8sJ-J0yq-Y9lH-SJ32-F9jHaj
  LV Write Access        read/write
  LV Status              available
  # open                 2
  LV Size                464.00 MB
  Current LE             116
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           254:1

fdisk -l

server1:~# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          31      248976   83  Linux
/dev/sda2              32        1305    10233405    5  Extended
/dev/sda5              32        1305    10233373+  8e  Linux LVM

Disk /dev/sdb: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn't contain a valid partition table

Disk /dev/dm-0: 9990 MB, 9990832128 bytes
255 heads, 63 sectors/track, 1214 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/dm-0 doesn't contain a valid partition table

Disk /dev/dm-1: 486 MB, 486539264 bytes
255 heads, 63 sectors/track, 59 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/dm-1 doesn't contain a valid partition table

So /dev/sda contains the logical volumes /dev/server1/root (/ partition) and /dev/server1/swap_1 (swap partition) plus a small /boot partition (non-LVM).

(BTW, /dev/server1/root is the same as /dev/mapper/server1-root on Debian Etch. The first is a symlink to the second; I will use both notations in this tutorial. The same goes for /dev/server1/swap_1 and /dev/mapper/server1-swap_1.)

I will now create the partition /dev/sdb1 and add it to the server1 volume group, and afterwards I will create the volume /dev/server1/backups (which will be 30GB instead of the full 60GB of /dev/sdb so that we have enough space left for the snapshots) which I will mount on /backups:

fdisk /dev/sdb

server1:~# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.


The number of cylinders for this disk is set to 7832.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help):
 <-- n
Command action
   e   extended
   p   primary partition (1-4)

<-- p
Partition number (1-4): <-- 1
First cylinder (1-7832, default 1): <-- [ENTER]
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-7832, default 7832):
<-- [ENTER]
Using default value 7832

Command (m for help):
 <-- t
Selected partition 1
Hex code (type L to list codes):
 <-- 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help):
 <-- w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

pvcreate /dev/sdb1
vgextend server1 /dev/sdb1
lvcreate --name backups --size 30G server1
mkfs.ext3 /dev/mapper/server1-backups
mkdir /backups

Now let's mount our /dev/server1/backups volume on /backups:

mount /dev/mapper/server1-backups /backups

To have that volume mounted automatically whenever you boot the system, you must edit /etc/fstab and add a line like this to it:

vi /etc/fstab
[...]
/dev/mapper/server1-backups /backups               ext3    defaults,errors=remount-ro 0       1

Now our new situation looks like this:

pvdisplay

server1:~# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               server1
  PV Size               9.76 GB / not usable 0
  Allocatable           yes (but full)
  PE Size (KByte)       4096
  Total PE              2498
  Free PE               0
  Allocated PE          2498
  PV UUID               vQIUga-221O-GIKj-81Ct-2ITT-bKPw-kKElpM

  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               server1
  PV Size               59.99 GB / not usable 0
  Allocatable           yes
  PE Size (KByte)       4096
  Total PE              15358
  Free PE               7678
  Allocated PE          7680
  PV UUID               cvl1H5-cxRe-iyNg-m2mM-tjxM-AvER-rjqycO

vgdisplay

server1:~# vgdisplay
  --- Volume group ---
  VG Name               server1
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               69.75 GB
  PE Size               4.00 MB
  Total PE              17856
  Alloc PE / Size       10178 / 39.76 GB
  Free  PE / Size       7678 / 29.99 GB
  VG UUID               jkWyez-c0nT-LCaE-Bzvi-Q4oD-eD3Q-BKIOFC

lvdisplay

server1:~# lvdisplay
  --- Logical volume ---
  LV Name                /dev/server1/root
  VG Name                server1
  LV UUID                UK1rjH-LS3l-f7aO-240S-EwGw-0Uws-5ldhlW
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                9.30 GB
  Current LE             2382
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           254:0

  --- Logical volume ---
  LV Name                /dev/server1/swap_1
  VG Name                server1
  LV UUID                2PASi6-fQV4-I8sJ-J0yq-Y9lH-SJ32-F9jHaj
  LV Write Access        read/write
  LV Status              available
  # open                 2
  LV Size                464.00 MB
  Current LE             116
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           254:1

  --- Logical volume ---
  LV Name                /dev/server1/backups
  VG Name                server1
  LV UUID                sXq2Xe-y2CE-Ycko-rCoE-M5kl-E1vH-KQRoP6
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                30.00 GB
  Current LE             7680
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           254:2

Share this page:

Suggested articles

9 Comment(s)

Add comment

Comments

By: anees khan

# lvcreate –L 100M –s –n snapshot-name Logic-Volume-Name

# mount /dev/Volume-Group/snapshot-namme /mount-point

#tar –zcvzf   /backup/snapshot.tar.gz  /mount-point

The /boot/ partition resides on its own non-LVM partition. In the following example, it is the first partition on the first drive (/dev/sda1). Bootable partitions cannot reside on LVM logical volumes.

#dd if=/dev/sda1 of=/backup/boot.dd

Note the uuid of your / partition

#pvscan –u > /backup/uuid

#lvdisplay > /backup/lvdisplay



On New System

To restore the / partition from your backup (covered in the last chapter of this tutorial) you need a Linux Live-CD that supports LVM, such as Knoppix or the Debian Etch Netinstall CD which you can use as a rescue CD if you specify rescue at the boot prompt.



Repartition  your / hard-disk. Mount the shared disk.

#dd if=/backup/boot.dd of=/dev/sda1

#more /backup/uuid

#pvcreate –uuid  uuid /dev/sda2

#more /backup/lvdisplay

#pvcreate Volume-Group-name /dev/sda2

#lvcreate –l 100%FREE –n Logic-Volume-name Volume-Group

#mount  /dev/Volume-Group/Logic-Volume /mount-point

#cd /mount-point

#tar –xvzf  /backup/root.tar.gz

#umount  /mount-point

#reboot

By: ^x3ro

May I mention, that you do not need "the same space for the snapshots that you use for the partition that you want to back up". You just need as much space as data being changed while the snapshot is active.

Example: Imagine you create a snapshot of your "/" partition and then neither you, nor your system is changing any files on your "/" partition, then you could theoretically create a 0 bit snapshot partition (which is AFAIK practically impossible). 

If you have a database of 2 GB running on that partition, that is used by a webservice, chances are, that the database files are bing changed while the snapshot is active. Therefore you should create a – lets say – 2 GB snapshot partition. This way, in theory, the whole data in the database could be changed without rendering the snapshot useless.

In fact, the snapshot partition does not contain any physical files until a file gets changed in the source partition (in this case the "/" partition). When a file is changed, the original is moved to the snapshot partition before it is overwritten.

So if you have a "file.txt" on your "/" partition, with content "foo" and you change the content to "bar" while the snapshot is active, the "file.txt" will be copied / moved to the snapshot partition before it gets overwritten. Now the only physical file on the snapshot partition is the "file.txt". Every other files are just references to the files on the "/" partition.

With this information in mind, it seems more intelligent to have a bit of free space on the LVM partition within that you want to make a snapshot as LVM would otherwise need to copy the original files to another disk (which could be time intensive) before they are overwritten.

I'm new to LVM so forgive me if some of the information is incorrect. But if I understood it right, this is exactly how snapshots work and this is also what makes LVM snapshots so awesome.

 

^x3ro

By: ohaya

Hi,

 

I was wondering if what you're describing can be used to backup a RHEL instance such that that backup can then be used later to do a bare metal restore from the backup?  

In our case, all of our systems are running RHEL5, on physical machines (i.e., bare metal) and we are going to be "upgrading" these systems to RHEL6-based by installing RHEL6 on each machine, then installing our app software.  

But, we would like to have a way to put the machines back to the same state they were in when they were on RHEL5, e.g., in case the installation of RHEL6 and our apps, failed.

Thanks!

 

Jim

By: Victor

If i choose a file based backup (tar.gz file), how i can restore the root partition from this file? because the dd command do the whole space of my 1tb root partition.

By: Carr

Thanksful for this series of articles ! Sorry I'm little confused and wanna make sure the purpose of different kind of backup mechanism. In First paragraph  "Normally you can restore a backup from the same running system if the volume that you want to restore doesn't contain system-critical files." Is that mean using "lvconvert --merge" with snapshot that keep system's old status ? If not , what kind of situation we should mount then tar or dd to restore like this article had descibed? Appreciate in any response!

By: Gordon

The download location of the VMWare image has changed. It is now https://www.howtoforge.com/downloads/files/Debian_Etch_LVM_Snapshots.zip

By: Daniel D

How long does it take to restore a LVM Snapshot?   For example if I'm usiong 5 tb?  Thanks

By: Ruben

Why do you remove your Snapshot? You can use the command to restore your snapshot to the /root lv:

lvconvert --merge /dev/vggroup/yoursnapshot

By: Daniel

Backing up a snapshot is useless. You should backup the LV block device that has been left iddle by the snapshot, as new data is being written to to snapshot instead. As a matter of fact, backing up a block device that the OS is writting to will render non usable data.