Comments on Back Up (And Restore) LVM Partitions With LVM Snapshots

Back Up (And Restore) LVM Partitions With LVM Snapshots 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.

9 Comment(s)

Add comment

Please register in our forum first to 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.