User Tools

Site Tools


Advertisements: AliExpress Hide Your IP Microsoft Products
linux:debian:lvm_snapshot

LVM Snaphots & Backup

In this post I will describe how I used snapshot feature of LVM (Logical Volume Manager) version 2 of Linux under Debian 8 (jessie) GNU/Linux OS.

I used LVM Snapshots to backup my mailpiler server. Since there are lots of compressed emails and MySQL database in my mailpiler server, using rsync can be tricky and time consuming. It is easier to copy a single binary file to a remote server, which is in the same network and bandwith is not a problem.

To be able use LVM Snapshots, your data must be on LVM :)

In my case, my logical volume name is mailpiler and my root lies on mailpiler logical volume, so I can get the snapshot of whole system. lvdisplay command shows your logical volumes as below;

[email protected]:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/mailpiler/root
  LV Name                root
  VG Name                mailpiler
  LV UUID                gxAwA0-EfO9-xPw0-IJrV-YsA9-6gsV-PuCmHe
  LV Write Access        read/write
  LV Creation host, time mailpiler, 2015-03-16 13:34:34 +0200
  LV Status              available
  # open                 1
  LV Size                500.00 GiB
  Current LE             128000
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0

  --- Logical volume ---
  LV Path                /dev/mailpiler/swap_1
  LV Name                swap_1
  VG Name                mailpiler
  LV UUID                YV7GoB-vHDD-gBVf-EWAO-3RY7-ncPZ-I81Jq5
  LV Write Access        read/write
  LV Creation host, time mailpiler, 2015-03-16 13:34:34 +0200
  LV Status              available
  # open                 2
  LV Size                4.00 GiB
  Current LE             1024
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:1

  --- Logical volume ---
  LV Path                /dev/mailpiler/backups
  LV Name                backups
  VG Name                mailpiler
  LV UUID                fHu907-f0Bq-pn0c-ORCt-K8oE-9QA2-lBf3NV
  LV Write Access        read/write
  LV Creation host, time mailpiler, 2017-03-06 09:43:37 +0300
  LV Status              available
  # open                 1
  LV Size                1.00 TiB
  Current LE             262144
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:2

I have another logical volume called backups but you don't have to have another logical volume. I use it to store my snapshots.

Although LVM snapshots are atomic, it is better to stop your file system intensive applications before taking snapshot. In my case, MySQL and mailpiler services should be stopped before snapshot takes place. Taking a LVM snapshot takes at most 1-3 seconds on my server. After taking the snapshot, you can start your services safely.

You should note that, after a snapshot is taken, you system runs slower because LVM copies data to make data in snapshot unchaged when original data has changed, that is called Copy-on-write (COW).

Another important thing is you should have enough space to copy your snapshot if you want to copy whole snapshot as image. As you see from above, mailpiler lvm size is 500 gigabyte so if you consider to copy whole snapshot to another place, you need to provide that much space. My backups logical volume's size is 1 terabyte and it fits at least one snapshot image.

To take a snapshot, following command is executed, do not forget to change paramaters (name,size and target logical volume path) per your needs and stop your data intensive applications before taking snapshot.

lvcreate -s -n rootsnapshot -L 500G /dev/mailpiler/root

# -s|--snapshot
# -n|--name LogicalVolumeName
# -L|--size LogicalVolumeSize

Then you get a message as below if everything is OK

 Logical volume "rootsnapshot" created

Then when I run lvdisplay command I get the output below and verify that you have a new logical volume with 500G size

[email protected]:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/mailpiler/root
  LV Name                root
  VG Name                mailpiler
  LV UUID                gxAwA0-EfO9-xPw0-IJrV-YsA9-6gsV-PuCmHe
  LV Write Access        read/write
  LV Creation host, time mailpiler, 2015-03-16 13:34:34 +0200
  LV snapshot status     source of
                         rootsnapshot [active]
  LV Status              available
  # open                 1
  LV Size                500.00 GiB
  Current LE             128000
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0

  --- Logical volume ---
  LV Path                /dev/mailpiler/swap_1
  LV Name                swap_1
  VG Name                mailpiler
  LV UUID                YV7GoB-vHDD-gBVf-EWAO-3RY7-ncPZ-I81Jq5
  LV Write Access        read/write
  LV Creation host, time mailpiler, 2015-03-16 13:34:34 +0200
  LV Status              available
  # open                 2
  LV Size                4.00 GiB
  Current LE             1024
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:1

  --- Logical volume ---
  LV Path                /dev/mailpiler/backups
  LV Name                backups
  VG Name                mailpiler
  LV UUID                fHu907-f0Bq-pn0c-ORCt-K8oE-9QA2-lBf3NV
  LV Write Access        read/write
  LV Creation host, time mailpiler, 2017-03-06 09:43:37 +0300
  LV Status              available
  # open                 1
  LV Size                1.00 TiB
  Current LE             262144
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:2

  --- Logical volume ---
  LV Path                /dev/mailpiler/rootsnapshot
  LV Name                rootsnapshot
  VG Name                mailpiler
  LV UUID                eN98Px-6clx-o9Rj-8IHz-MU15-Jjop-p6OWmR
  LV Write Access        read/write
  LV Creation host, time mailpiler, 2017-03-13 15:08:13 +0300
  LV snapshot status     active destination for root
  LV Status              available
  # open                 0
  LV Size                500.00 GiB
  Current LE             128000
  COW-table size         500.00 GiB
  COW-table LE           128000
  Allocated to snapshot  0.00%
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:3

Now, it is time to backup snapshot with dd or tar OR from snapshot by mounting your snapshot volume and backup individual files instead of block based backup. (you can prefer rsync for this purpose) You can mount snapshot volume with following command.

mkdir -p /mnt/snap
mount -o ro /dev/mailpiler/rootsnapshot /mnt/snap

You don't have to mount snapshot as read only with -o ro option like I do, but I want to be on safe side and I don't want to change data in snapshot. If you are using LVM version 1, snapshots are read only anyway.

Copying image of snapshot is easy, I prefer dd command as below;

dd if=/dev/mailpiler/rootsnapshot of=/backups/rootsnapshot.dd bs=100M

Do not forget to put appropriate bs parameter otherwise it can take very long time. After your backup is completed, you should remove the snapshot volume for performance reasons.

Removing snapshot volume is as easy as taking snapshot with following command;

[email protected]:~# lvremove /dev/mailpiler/rootsnapshot
Do you really want to remove active logical volume rootsnapshot? [y/n]: y
  Logical volume "rootsnapshot" successfully removed
[email protected]:~#

To verify snapshot is removed, lvdisplay or lvs commands can be executed.

For further information or examples, you may visit following links;

Please do not hesitate to ask any questions about lvm snapshots.

Note 1: After creating images of snapshot, I transfer the image to a FreeNAS server and the dataset has gzip compression so I do not need to compress the images with any other tool.

Note 2: I pull the images from mailpiler server via NFS with a bash script scheduled with a cron task on FreeNAS server.

Note 3: You can use a better backup method, for example, xdelta3 package can create a diff file for binaries.

Discussion

Enter your comment. Wiki syntax is allowed:
 
linux/debian/lvm_snapshot.txt · Last modified: 2017/03/13 13:06 by ozan