Back Up (And Restore) LVM Partitions With LVM Snapshots - Page 2

3 Create An LVM Snapshot Of /

Now it's time to create the snapshot of the /dev/server1/root volume. We will call the snapshot rootsnapshot:

lvcreate -L10G -s -n rootsnapshot /dev/server1/root

The output of

lvdisplay

should look like this:

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 snapshot status     source of
                         /dev/server1/rootsnapshot [active]
  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

  --- Logical volume ---
  LV Name                /dev/server1/rootsnapshot
  VG Name                server1
  LV UUID                9zR5X5-OhM5-xUI0-OolP-vLjG-pexO-nk36oz
  LV Write Access        read/write
  LV snapshot status     active destination for /dev/server1/root
  LV Status              available
  # open                 1
  LV Size                9.30 GB
  Current LE             2382
  COW-table size         10.00 GB
  COW-table LE           2560
  Allocated to snapshot  0.01%
  Snapshot chunk size    8.00 KB
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           254:5

We want to mount /dev/server1/rootsnapshot on /mnt/server1/rootsnapshot, so we have to create that directory first:

mkdir -p /mnt/server1/rootsnapshot

Then we mount our snapshot:

mount /dev/server1/rootsnapshot /mnt/server1/rootsnapshot

Then we run

ls -l /mnt/server1/rootsnapshot/

This should show all directories and files that we know from our / partition:

server1:~# ls -l /mnt/server1/rootsnapshot/
total 132
drwxr-xr-x  2 root root  4096 2007-04-10 21:02 backups
drwxr-xr-x  2 root root  4096 2007-04-10 20:35 bin
drwxr-xr-x  2 root root  4096 2007-04-10 20:25 boot
lrwxrwxrwx  1 root root    11 2007-04-10 20:25 cdrom -> media/cdrom
drwxr-xr-x 13 root root 40960 2007-04-10 20:36 dev
drwxr-xr-x 57 root root  4096 2007-04-10 21:09 etc
drwxr-xr-x  3 root root  4096 2007-04-10 20:36 home
drwxr-xr-x  2 root root  4096 2007-04-10 20:26 initrd
lrwxrwxrwx  1 root root    28 2007-04-10 20:29 initrd.img -> boot/initrd.img-2.6.18-4-486
drwxr-xr-x 13 root root  4096 2007-04-10 20:34 lib
drwx------  2 root root 16384 2007-04-10 20:25 lost+found
drwxr-xr-x  4 root root  4096 2007-04-10 20:25 media
drwxr-xr-x  2 root root  4096 2006-10-28 16:06 mnt
drwxr-xr-x  2 root root  4096 2007-04-10 20:26 opt
drwxr-xr-x  2 root root  4096 2006-10-28 16:06 proc
drwxr-xr-x  3 root root  4096 2007-04-10 20:42 root
drwxr-xr-x  2 root root  4096 2007-04-10 20:36 sbin
drwxr-xr-x  2 root root  4096 2007-03-07 23:56 selinux
drwxr-xr-x  2 root root  4096 2007-04-10 20:26 srv
drwxr-xr-x  2 root root  4096 2007-01-30 23:27 sys
drwxrwxrwt  2 root root  4096 2007-04-10 21:09 tmp
drwxr-xr-x 10 root root  4096 2007-04-10 20:26 usr
drwxr-xr-x 13 root root  4096 2007-04-10 20:26 var
lrwxrwxrwx  1 root root    25 2007-04-10 20:29 vmlinuz -> boot/vmlinuz-2.6.18-4-486

So our snapshot has successfullly been created!

Now we can create a backup of the snapshot on the /backups partition using our preferred backup solution. For example, if you like to do a file-based backup, you can do it like this:

tar -pczf /backups/root.tar.gz /mnt/server1/rootsnapshot

And if you like to do a bitwise backup (i.e. an image), you can do it like this:

dd if=/dev/server1/rootsnapshot of=/backups/root.dd

server1:~# dd if=/dev/server1/rootsnapshot of=/backups/root.dd
19513344+0 records in
19513344+0 records out
9990832128 bytes (10 GB) copied, 320.059 seconds, 31.2 MB/s

You could also use both ways to be prepared for whatever might happen to your /dev/server1/root volume. In this case, you should have two backups afterwards:

ls -l /backups/

server1:~# ls -l /backups/
total 9947076
drwx------ 2 root root      16384 2007-04-10 21:04 lost+found
-rw-r--r-- 1 root root 9990832128 2007-04-10 21:28 root.dd
-rw-r--r-- 1 root root  184994590 2007-04-10 21:18 root.tar.gz

Afterwards, we unmount and remove the snapshot to prevent it from consuming system resources:

umount /mnt/server1/rootsnapshot
lvremove /dev/server1/rootsnapshot

That's it, you've just made your first backup from an LVM snapshot.

Share this page:

17 Comment(s)

Add comment

Comments

From: anees khan at: 2012-01-19 09:18:16

# 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

From: alxgomz at: 2009-10-13 23:57:00

Unlike what's show here, there is no need to allocate the total volume size to its snapshot. eg: a volume of 10GB can be "snapshoted" to a snapshot a of 1GB! In fact only modified data are stored on disk.

From: situationalawareness at: 2009-11-16 20:16:11

True, the easiest way to know how much would be needed for snapshot size is to look at how much is taken on the logical volume that the snapshot will be for.

How I work it is I create a volume group double the size of the logical volume I will be using.  Then, when it's time to perform maintenance, the scripting is guaranteed at all times to have the space necessary even if logical volume being snapshotted is at 100% utilization.

From: Anonymous at: 2010-01-21 23:41:34

The snapshot will fill depending on the amount of stuff that changes in the original volume.

So giving a snapshot the same size than the volume you're snapshotting means you're safe even if every single block changes during the time needed to do your backup.

Which is a pretty wasteful level of safety for the vast majority of cases.

From: mastapat11 at: 2011-10-26 06:04:40

"lvcreate -L3G -s -n rootsnapshot /dev/pve/root"

 using this command i get the error: "Insufficient free extents (384) in volume group pve: 768 required"

what am i doing wrong? 

lvdisplay says:

LV Name                /dev/pve/root
  VG Name                pve
  LV UUID                tsnxNe-d93T-rypG-GjfC-TXNG-NWdM-6xyqNA
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                3.00 GiB
  Current LE             768
 

From: Neron.L at: 2012-09-24 09:56:58

To create a new LV, there must have enjoy free space on VG. That message is indicated there not enough free space to you to create another LV.

 So you can try to mount a additional disk to your system, and run 'vgextend' for give more free space to your VG.

From: Anonymous at: 2014-03-25 22:18:20

If you choose to do the image option, is there any reason to mount the snapshot? The dd command  shown uses the /dev location instead of the /mnt location.

From: Ritesh Raj Sarraf at: 2009-04-03 18:43:50

Thanks for pointing out dd+compression_utils.

I was wondering if there are tools which could take a block diff and allow generating a delta ?

From: Anonymous at: 2009-05-20 23:45:45

look at rdiff-backup

From: at: 2009-02-26 01:57:57

I really enjoyed the tutorial, everything worked well and  it was nice and straight forward, but would you mind including how to restore a tar style backup?  dd backups are great, but they don't allow for the use of compression and use space on the destination disk for the free space on the source disk.

From: Anonymous at: 2009-03-01 00:06:32

dd image files are very compressable! Just run gzip(1) or bzip2(1) on the 'rawImageFile.dd'.  Bzip2 will give you better compression, but will take longer (use pbipz2 if you have a multi-core box, it'll be faster).  This will give you rawImageFile.dd.bz2. 

 

You just have to have storage space available to uncompress it when you do a restore.  If you're of the geeky persuasion, pipe the output of bunzip2(1) directly to the disk.  This also applies to compressing the image file; you can pipe the output of dd(8) to bzip2 and compress the image, on the fly, directly to the raw file.

From: suresh at: 2009-12-24 07:15:05

thanks a lot for this wonderful tutorial ,  keep it up

From: Drumar at: 2010-06-02 00:16:00

Great job describing how this is done, this gave me all the pointers I needed to get the job done on an external disk. Since it's not that obvious, I'll give some pointers here:

Your snapshot must be in your volume group; But my volume group is 100% in use :-) But shortly I'll want to do an upgrade that I can revert if things go wrong...

My volumegroup has the name rootvg with the / partition on rootlv (sorry, grew up with lvm on AIX)

My external disk is on /dev/sdc1 and I bought it for this purpose mainly, so it can handle the 50G rootlv with ease (it's 1.5TB)

I noticed that for a snapshot you need slightly more space on disk than the size of the volume; so to be safe I added 10% (probably quite overdone)

mounted my /dev/sdc1 on /mnt/bigdisk

dd if=/dev/zero of=/mnt/bigdisk/rootlv_snapshot bs=1024 count=55M # this creates a 55G image file

added this image to the loopback interface:

losetup -f /mnt/bigdisk/rootlv_snapshot

added the 8e filesystem identifier with fdisk /dev/loop0

created the physical volume: pvcreate /dev/loop0

added it to rootvg: vgextend rootvg /dev/loop0

went on as described; you can dd /mnt/bigdisk/rootlv_snapshot, mount it and tar your files;

 

Cleaning up goes like

- umount the snapshot volume

- remove the logical volume: lvremove /dev/rootvg/rootsnapshot

- remove the physical volume from rootvg: vgreduce rootvg /dev/loop0

- remove the pyhsical volume: pvremove /dev/loop0

- detach the image from the loopback interface: losetup -d /dev/loop0

- remove the image file: rm /mnt/bigdisk/rootlv_snapshot

- bzip2 the dd image

Hope this helps people getting a backup done when their vg is fully used.

From: at: 2011-01-10 13:29:22

What is the purpose to create a loop back device on the external usb storage ?

From: Drumar at: 2011-06-20 21:07:10

It allows the file to be treated as a device, so it can be attached to the volumegroup, which allows it to be the snapshot device.

From: Johnny at: 2010-09-22 05:13:18

Awesome tutorial, people like you keep linux going.  Thanks.

From: at: 2011-01-10 13:35:28

If root hdd failure and no longer bootable, after replaced with a new hdd, could I use the snapshot backup image to restore back the whole system ?