Consistent backup using LVM snapshot
As we know LVM is an efficient way to manage logical volumes within Linux kernel. One of the great advantages of LVM is that it can be used for taking consistent backups with the help of snapshots. This blog helps you to understand the steps to be taken for a consistent backup using LVM snapshot.The backup consistency plays an important role i the production environment since there are chances that same file is being used by multiple users/processes. Suppose the file is open while backup action is in place, you may end up getting an inconsistent backup. Please keep in mind that this procedure will work only on LVM based partitions.
Step # 1: In the example shown here, I have a VG named vol_grp1 and a logical volume /dev/vol_grp1/lv_ora1 is mounted on /u01 from the VG. The VG is having 14G of free space.
[root@vimalt-linux ~]# vgdisplay --- Volume group --- VG Name vol_grp1 System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 12 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 1 Act PV 1 VG Size 15.00 GiB PE Size 4.00 MiB Total PE 3839 Alloc PE / Size 250 / 1000.00 MiB Free PE / Size 3589 / 14.02 GiB VG UUID HgwGsP-eXE1-fn1I-iFld-QW2C-8p35-ASjz2B
[root@vimalt-linux ~]# lvdisplay --- Logical volume --- LV Path /dev/vol_grp1/lv_ora1 LV Name lv_ora1 VG Name vol_grp1 LV UUID O6NnvA-zCFK-Z0HU-7u2f-ceqI-BAyr-pc236J LV Write Access read/write LV Creation host, time vimalt-linux, 2015-04-27 11:17:12 +0530 LV Status available # open 1 LV Size 1000.00 MiB Current LE 250 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
[root@vimalt-linux ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 37G 7.4G 28G 21% / tmpfs 499M 0 499M 0% /dev/shm /dev/sda1 485M 55M 405M 12% /boot /dev/mapper/vol_grp1-lv_ora1 985M 18M 918M 2% /u01
Step # 2: I wanted to take the backup of /u01 file system which is of 985M. First create a LVM snapshot as below of LV /dev/vol_grp1/lv_ora1 . Here u01_bkup is the name given for snapshot. Keep in mind that if the snapshot logical volume becomes full it will be dropped (become unusable) so it is vitally important to allocate enough space. Here I have allocated 985M since that is maximum possible snapshot size.
[root@vimalt-linux ~]# lvcreate -L985M -s -n u01_bkup /dev/vol_grp1/lv_ora1 Rounding up size to full physical extent 988.00 MiB Logical volume "u01_bkup" created
Step # 3: You can verify the snapshot created using lvs and lvdisplay command.
[root@vimalt-linux ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv_ora1 vol_grp1 owi-aos-- 1000.00m u01_bkup vol_grp1 swi-a-s-- 988.00m lv_ora1 0.00
[root@vimalt-linux ~]# lvdisplay /dev/vol_grp1/u01_bkup --- Logical volume --- LV Path /dev/vol_grp1/u01_bkup LV Name u01_bkup VG Name vol_grp1 LV UUID 92v5nP-YM83-iHE8-i6rC-UHG6-SFhe-dp3T09 LV Write Access read/write LV Creation host, time vimalt-linux, 2015-04-30 17:55:31 +0530 LV snapshot status active destination for lv_ora1 LV Status available # open 1 LV Size 1000.00 MiB Current LE 250 COW-table size 988.00 MiB COW-table LE 247 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:1
Step # 4: Create a mount point and get the snapshot mounted.
[root@vimalt-linux ~]# mkdir /root/u01_backup [root@vimalt-linux ~]# mount /dev/vol_grp1/u01_bkup /root/u01_backup/ [root@vimalt-linux ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 37G 7.4G 28G 21% / tmpfs 499M 0 499M 0% /dev/shm /dev/sda1 485M 55M 405M 12% /boot /dev/mapper/vol_grp1-lv_ora1 985M 18M 918M 2% /u01 /dev/mapper/vol_grp1-u01_bkup 985M 18M 918M 2% /root/u01_backup
Step # 5: Now you can use your convenient way to backup the mounted snapshot. Here I have used tar command.
[root@vimalt-linux u01_backup]# cd /root/u01_backup/ [root@vimalt-linux u01_backup]# tar -pjcvf /root/u01_backup_`date +"%d-%m-%Y"`.tar.bz2 . ./ ./passwd ./hosts ./lost+found/ [root@vimalt-linux u01_backup]# ls -l /root/u01_backup_* -rw-r--r-- 1 root root 1118 Apr 30 18:11 /root/u01_backup_30-04-2015.tar.bz2
Step # 6: You can verify the contents of the created .tar.bz2 file as below. You can now move the backup file to tapes or backup server as you require.
[root@vimalt-linux u01_backup]# tar -tvf /root/u01_backup_30-04-2015.tar.bz2 drwxr-xr-x root/root 0 2015-04-27 12:09 ./ -rw-r--r-- root/root 2021 2015-04-27 11:39 ./passwd -rw-r--r-- root/root 158 2015-04-27 11:20 ./hosts drwx------ root/root 0 2015-04-27 11:17 ./lost+found/
Step # 7: Now unmount and remove the snapshot
[root@vimalt-linux ~]# umount /root/u01_backup [root@vimalt-linux ~]# lvremove /dev/vol_grp1/u01_bkup Do you really want to remove active logical volume u01_bkup? [y/n]: y Logical volume "u01_bkup" successfully removed
That’s it!
Read More:
If you like this post and wish to receive more articles from us, please like our FB page: Button
Your suggestions and feedbacks will encourage us and help to improve further, please feel free to write your comments.
For more details on our services, please drop us an E-mail at info@grepitout.com
Add Comment