Backup and Restore Logical Volume using LVM Snapshot

Backup Restore LVM using Snapshot
LVM (Logical Volume Manager) gives us the ability to create Snapshots. LVM Snapshot is a logical volume that temporarily preserves the original data of changing logical volume, so it’s data can be backed up preserving consistency. Snapshot must be created prior to any critical modifications on the Logical Volume in order to fulfill it’s role.

Note: Snapshot Volume needs to be large enough to store the data that will change while it exists. If more data changes than the Snapshot can hold, the Snapshot will become unusable. That means we have to “predict” how much data will be changed on the original Volume while the Snapshot exists to let the Snapshot hold all those data changes.

Below we will create Snapshot of the Logical Volume (back up Logical Volume), make changes on the Logical Volume and restore it’s previous state using LVM Snapshot.

Steps:

1. Verify existing Logical Volumes and their mount points

[root@tuxfixer ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/fedora/swap
  LV Name                swap
  VG Name                fedora
  LV UUID                eCqPuY-10lE-jE73-BveE-jgSc-6V9W-b29s4q
  LV Write Access        read/write
  LV Creation host, time tuxfixer, 2016-02-10 23:34:47 +0100
  LV Status              available
  # open                 2
  LV Size                3.88 GiB
  Current LE             992
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/fedora/root
  LV Name                root
  VG Name                fedora
  LV UUID                wdM8rg-GeKd-GgJq-3KdJ-2iHr-qnXV-OwbITW
  LV Write Access        read/write
  LV Creation host, time tuxfixer, 2016-02-10 23:34:48 +0100
  LV Status              available
  # open                 1
  LV Size                45.63 GiB
  Current LE             11681
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/fedora/storage
  LV Name                storage
  VG Name                fedora
  LV UUID                N1iB1P-W3Dh-0NIv-aUJd-P6hF-KMUv-NTeweI
  LV Write Access        read/write
  LV Creation host, time tuxfixer, 2016-04-10 19:14:54 +0200
  LV Status              available
  # open                 1
  LV Size                20.00 GiB
  Current LE             5120
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3
[root@tuxfixer ~]# df -hT
Filesystem                 Type      Size  Used Avail Use% Mounted on
devtmpfs                   devtmpfs  2.0G     0  2.0G   0% /dev
tmpfs                      tmpfs     2.0G  124K  2.0G   1% /dev/shm
tmpfs                      tmpfs     2.0G  1.2M  2.0G   1% /run
tmpfs                      tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/mapper/fedora-root    ext4       45G  6.6G   36G  16% /
tmpfs                      tmpfs     2.0G   64K  2.0G   1% /tmp
/dev/vda1                  ext4      477M  135M  313M  31% /boot
tmpfs                      tmpfs     396M   24K  396M   1% /run/user/42
tmpfs                      tmpfs     396M     0  396M   0% /run/user/1000
/dev/mapper/fedora-storage ext4       20G  5.2G   14G  28% /mnt 

2. Backup Logical Volume (Create LVM Snapshot of the Logical Volume)

Let’s backup storage Logical Volume by creating it’s Snapshot. We assume that the data changes on the storage Logical Volume will not exceed 10 GB while the Snapshot exists. That’s why we will create Snapshot of storage Logical Volume (/dev/fedora/storage) called storage_snapshot that is 10 GB in size:

[root@tuxfixer ~]# lvcreate -s -n storage_snapshot -L 10G /dev/fedora/storage
  Logical volume "storage_snapshot" created.

Verify created LVM Snapshot:

[root@tuxfixer ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/fedora/swap
  LV Name                swap
  VG Name                fedora
  LV UUID                eCqPuY-10lE-jE73-BveE-jgSc-6V9W-b29s4q
  LV Write Access        read/write
  LV Creation host, time tuxfixer, 2016-02-10 23:34:47 +0100
  LV Status              available
  # open                 2
  LV Size                3.88 GiB
  Current LE             992
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/fedora/root
  LV Name                root
  VG Name                fedora
  LV UUID                wdM8rg-GeKd-GgJq-3KdJ-2iHr-qnXV-OwbITW
  LV Write Access        read/write
  LV Creation host, time tuxfixer, 2016-02-10 23:34:48 +0100
  LV Status              available
  # open                 1
  LV Size                45.63 GiB
  Current LE             11681
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/fedora/storage
  LV Name                storage
  VG Name                fedora
  LV UUID                N1iB1P-W3Dh-0NIv-aUJd-P6hF-KMUv-NTeweI
  LV Write Access        read/write
  LV Creation host, time tuxfixer, 2016-04-10 19:14:54 +0200
  LV snapshot status     source of
                         storage_snapshot [active]
  LV Status              available
  # open                 1
  LV Size                20.00 GiB
  Current LE             5120
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3
   
  --- Logical volume ---
  LV Path                /dev/fedora/storage_snapshot
  LV Name                storage_snapshot
  VG Name                fedora
  LV UUID                d9eSG0-qp9I-fNU3-H8G5-iSBV-nkXh-vUEfhm
  LV Write Access        read/write
  LV Creation host, time tuxfixer, 2016-04-10 20:10:04 +0200
  LV snapshot status     active destination for storage
  LV Status              available
  # open                 0
  LV Size                20.00 GiB
  Current LE             5120
  COW-table size         10.00 GiB
  COW-table LE           2560
  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           253:6                             

Note: data allocated to storage_snapshot Volume for this moment is 0.00%, because no data changes have been made on storage Volume since Snapshot creation

3. Test LVM Snapshot

storage Volume is mounted in /mnt directory and the content of the directory is:

[root@tuxfixer ~]# ls -l /mnt
total 5090328
-rw-r--r--. 1 root root 1469054976 Apr 10 22:11 Fedora-Live-Workstation-x86_64-23-10.iso
drwx------. 2 root root      16384 Apr 10 19:22 lost+found
-rw-------. 1 root root 3743416320 Apr 10 22:13 rhel-server-7.0-x86_64-dvd.iso

Now test the Snapshot (/dev/fedora/storage_snapshot) to see, if it holds data changes – let’s remove .iso files from storage Volume (/dev/fedora/storage):

[root@tuxfixer ~]# rm -f /mnt/*.iso

Verify /mnt directory content:

[root@tuxfixer ~]# ls -l /mnt
total 16
drwx------. 2 root root 16384 Apr 10 19:22 lost+found

We have removed .iso files, in next point we will try to restore those files.

4. Restore Logical Volume using LVM Snapshot

Before we restore storage Volume, we have to unmount it from the mount point:

[root@tuxfixer ~]# umount /mnt

Now let’s try to restore storage Volume content by means of merging storage_snapshot with that Volume:

[root@tuxfixer ~]# lvconvert --merge /dev/fedora/storage_snapshot
  Merging of volume storage_snapshot started.
  storage: Merged: 100.0%

Mount storage Volume back in /mnt directory and verify it’s content:

[root@tuxfixer ~]# mount /dev/fedora/storage /mnt
[root@tuxfixer ~]# ls -l /mnt
total 3979284
-rw-r--r--. 1 root root  331350016 Apr 10 22:35 debian-8.4.0-i386-netinst.iso
drwx------. 2 root root      16384 Apr 10 19:22 lost+found
-rw-------. 1 root root 3743416320 Apr 10 22:34 rhel-server-7.0-x86_64-dvd.iso

Previously removed files are back again in the /mnt directory – Logical Volume has been restored from Snapshot.

Verify existing Logical Volumes after merging:

[root@tuxfixer ~]# lvs
  LV      VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root    fedora -wi-ao---- 45.63g                                                    
  storage fedora -wi-ao---- 20.00g                                                    
  swap    fedora -wi-ao----  3.88g         

Note: LVM Snapshot (/dev/fedora/storage_snapshot) doesn’t exist any more on Logical Volume list, because it was merged with the original Volume (/dev/fedora/storage).

Share on: Share on FacebookShare on Google+Tweet about this on TwitterShare on StumbleUponShare on LinkedInPin on PinterestBuffer this pageShare on TumblrDigg thisFlattr the authorShare on RedditShare on VKShare on Yummly
Liked it? Take a second to support tuxfixer.com on Patreon!
Hadoop Developer Training

Leave a Reply

Name *
Email *
Website