Backup and Restore LVM partition using Snapshot


A snapshot is the state of a system at a particular point in time and it is also called as photography. LVM also have the feature of snapshot. So that we can easily take the backup of current logical partition and moved to somewhere.

It is mainly used in live production environment, if you need to clone the exact copy of production environment then you will prefer this method.

Here we are going to take a backup of one logical volume and restore it in another machine on same network.

Step 1: Careate a new partition

[root@server ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (265-652, default 265):
Using default value 265
Last cylinder, +cylinders or +size{K,M,G} (265-652, default 652): +1G

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

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

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

Step 2: Run “partprobe” command to inform the OS/Kernel of partition table changes

[root@server ~]# partprobe

Step 3: LVM setup for taking snapshot for existing logical partition

Create the physical volume (PV)

[root@server ~]# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created

Extend Volume Group (VG)

[root@server ~]# vgextend vg01 /dev/sdb3
Volume group "vg01" successfully extended

[root@server ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup00 1 2 0 wz--n- 19.53g 2.88g
vg01 3 1 0 wz--n- 3.02g 2.54g

Creating snapshot of existing partition

[root@server ~]# lvcreate -L 1G -s -n lv_snap /dev/vg01/lv01
Logical volume "lv_snap" created

Note: Snapshot size should be larger than actual current size of logical volume.

[root@server ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup00 1 2 0 wz--n- 19.53g 2.88g
vg01 3 2 1 wz--n- 3.02g 1.54g

[root@server ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
LogVol00 VolGroup00 -wi-ao--- 14.65g
LogVol01 VolGroup00 -wi-ao--- 2.00g
lv01 vg01 owi-a-s-- 500.00m
lv_snap vg01 swi-aos-- 1.00g lv01 0.00

[root@server ~]# mkdir /snapdata

Check whether all datas from existing logical volume (/dev/vg01/lv01) copied to lvm snapshot (/dev/vg01/lv_snap)

[root@server ~]# mount /dev/vg01/lv_snap /snapdata
[root@server ~]# cd /snapdata/
[root@server snapdata]# ls
fstab lost+found passwd

So above commands shows that the datas are copied to the new logical volume. Now we need to compress and send this backup to other machine.

[root@server ~]# dd if=/dev/vg01/lv_snap | gzip > /data/lvdata.gz
1024000+0 records in
1024000+0 records out
524288000 bytes (524 MB) copied, 8.07202 s, 65.0 MB/s

Transfer the backup file to client machine

[root@server ~]# scp -r /data/lvdata.gz client:/tmp

Client Side:

Step 1: Create PV, VG, LV

[root@client ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created

[root@client ~]# vgcreate vgclient /dev/sdb1
Volume group "vgclient" successfully created

[root@client ~]# lvcreate -L 500M -n restoredata /dev/vgclient
Logical volume "restoredata" created

Step 2: Restore the datas to current lvm

[root@client ~]# gzip -dc /tmp/lvdata.gz | dd of=/dev/vgclient/restoredata
1024000+0 records in
1024000+0 records out
524288000 bytes (524 MB) copied, 63.7254 s, 8.2 MB/s

Execute the below command to make the lvm changes reflect

[root@client ~]# pvscan && vgscan && lvscan

Step 3: Mount the partition

[root@client ~]# mkdir /backup
[root@client ~]# mount /dev/vgclient/restoredata /backup
[root@client ~]# cd /backup
[root@client backup]# ls
fstab lost+found passwd

That’s it!! Enjoy..

Leave a comment

Leave a comment