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

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Sun, 2007-04-15 12:27. ::

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.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Tue, 2014-03-25 23:18.
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.
Submitted by mastapat11 (not registered) on Wed, 2011-10-26 07:04.

"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
 

Submitted by Neron.L (not registered) on Mon, 2012-09-24 10:56.

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.

Submitted by alxgomz (not registered) on Wed, 2009-10-14 00:57.
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.
Submitted by situationalawareness (not registered) on Mon, 2009-11-16 21:16.

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.

Submitted by Anonymous (not registered) on Fri, 2010-01-22 00:41.

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.