This article is first published in the dark world.
I have been learning about LVM logical volumes for some time. The previous articles introduced the basic knowledge about LVM logical volumes, including logical volume and volume group resizing. Today, we will introduce the LVM snapshot feature.
Note: This experiment is integrated with the virtualization KVM to check whether the KVM virtual machine is started properly and whether the LVM is backed up and restored.
For the LVM snapshot function, you can run the lvcreate-HELP command to view the help documentation. As follows:
650) This. width = 650; "Title =" clip_image001 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image001 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519590E1qU.png "width =" 551 "Height =" 528 "/>
First, check the logical volume in the system and use the LVS command. As follows:
650) This. width = 650; "Title =" clip_image002 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image002 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519591YCFM.png "width =" 606 "Height =" 101 "/>
We can see that there is only one logical volume/dev/vg1/vhostdisk in the system. Now we create a new logical volume/dev/vg1/lvmsp, as shown below:
Lvcreate-L 30g-N lvmsp vg1
650) This. width = 650; "Title =" clip_image003 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image003 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519591JJGn.png "width =" 581 "Height =" 139 "/>
Format the new logical volume as follows:
Mkfs. ext4/dev/vg1/lvmsp
650) This. width = 650; "Title =" clip_image004 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image004 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519592j3sd.png "width =" 577 "Height =" 425 "/>
After formatting the logical volume, create a new directory in the system to mount the new logical volume, as shown below:
Mkdir/test
650) This. width = 650; "Title =" clip_image005 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image005 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519592JoXL.png "width =" 415 "Height =" 402 "/>
Mount the logical volume as follows:
Mount/dev/vg1/lvmsp/test
650) This. width = 650; "Title =" clip_image006 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image006 "src =" http://img1.51cto.com/attachment/201408/20/526870_14085195920O6J.png "width =" 527 "Height =" 178 "/>
After the above operations are completed, we now store the hard disk of the KVM virtual machine to the/test directory. As follows:
CP-V/vhostdisk/testlvmos. img/test/
650) This. width = 650; "Title =" clip_image007 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image007 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519593lWfG.png "width =" 467 "Height =" 142 "/>
After the above operations are completed, we will first test whether the current virtual machine can be started normally.
Edit the XML file of the VM and modify the storage path of the hard disk. As follows:
Virsh edit testlvmos
650) This. width = 650; "Title =" clip_image008 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image008 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519593Frlv.png "width =" 548 "Height =" 532 "/>
Start the Virtual Machine testlvmos as follows:
Virsh start testlvmos
650) This. width = 650; "Title =" clip_image009 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image009 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519593gPVV.png "width =" 356 "Height =" 109 "/>
650) This. width = 650; "Title =" clip_image010 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image010 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519594c3xD.png "width =" 740 "Height =" 467 "/>
We can see that the virtual machine testlvmos has started properly. Now we disable the Virtual Machine and create a snapshot for the logical volume/dev/vg1/lvmsp1 at this time.
Run the following command:
Lvcreate-L 100 m-S-N lvmsp1/dev/vg1/lvmsp
Or
Lvcreate-S/dev/vg1/lvmsp-N lvmsp1-L 100 m
650) This. width = 650; "Title =" clip_image011 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image011 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519594zjTh.png "width =" 581 "Height =" 163 "/>
-S indicates the snapshot to be created,-N indicates the snapshot name, and-l indicates the snapshot size.
At the same time, we can easily see that snapshot creation is also a matter of a few seconds. This is very important. Our backup work is also based on this, and it takes a little time to reach the backup function.
Now we mount the snapshot of the logical volume/dev/vg1/lvmsp. As follows:
Mkdir/test1/
Mount/dev/vg1/lvmsp1/test1/
650) This. width = 650; "Title =" clip_image012 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image012 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519595HUDj.png "width =" 406 "Height =" 440 "/>
650) This. width = 650; "Title =" clip_image013 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image013 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519595fUzy.png "width =" 526 "Height =" 205 "/>
In fact, we can also use the generated snapshots for normal operations. However, we will not introduce this article here. We will mainly introduce the backup and recovery after the snapshot is generated.
Now let's check the directory/test/details mounted to the logical volume/dev/vg1/lvmsp1 before taking a snapshot. As follows:
Ll-H/test/
650) This. width = 650; "Title =" clip_image014 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image014 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519596MHWx.png "width =" 413 "Height =" 114 "/>
Check the directory/test1/attached to the snapshot as follows:
650) This. width = 650; "Title =" clip_image015 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image015 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519596I2OO.png "width =" 442 "Height =" 97 "/>
Comparison before and after snapshot ,:
650) This. width = 650; "Title =" clip_image016 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image016 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519596en8S.png "width =" 527 "Height =" 310 "/>
Through comparison, we can find that the content of the logical volume/dev/vg1/lvmsp is exactly the same as that of the snapshot/dev/vg1/lvmsp1. If we back up the content in the snapshot, we can easily restore the status of the current virtual machine. This is the focus of this article, using snapshots to back up virtual machines.
Now, we need to perform the following steps:
1. Backup the file testlvmos. IMG In the snapshot.
2. unmount the mounted Snapshot/dev/vg1/lvmsp1
3. Delete all contents in the/test/directory mounted to the logical volume/dev/vg1/lvmsp.
4. Copy the testlvmos. imgfile backed up by a snapshot to the/test directory mounted to the logical volume/dev/vg1/lvmsp
We can use the CP command to back up the content in the snapshot to another directory. Here we back up the data to the/baklvm/directory, as shown below:
CP-V/test1/testlvmos. img/baklvm/
650) This. width = 650; "Title =" clip_image017 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image017 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519597aS49.png "width =" 455 "Height =" 186 "/>
After the snapshot is backed up, We Will unmount the attached snapshot and delete it. As follows:
Umount/test1/
Lvremove/dev/vg1/lvmsp1
650) This. width = 650; "Title =" clip_image018 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image018 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519597YYlL.png "width =" 614 "Height =" 173 "/>
Now we delete the/test/directory mounted to the logical volume/dev/vg1/lvmsp, as follows:
Rm-r/test /*
650) This. width = 650; "Title =" clip_image019 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image019 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519597ZmAP.png "width =" 422 "Height =" 129 "/>
After the deletion is complete, restart the Virtual Machine testlvmos as follows:
Virsh start testlvmos
650) This. width = 650; "Title =" clip_image020 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image020 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519598UfqQ.png "width =" 542 "Height =" 133 "/>
We can see that the current Virtual Machine testlvmos cannot be started, and the system prompts that the testlvmos. imgfile is missing.
In fact, we also know that the file is missing. Because we have deleted the file as follows:
650) This. width = 650; "Title =" clip_image021 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image021 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519598C0pR.png "width =" 291 "Height =" 93 "/>
Now we will copy the testlvmos. imgfile backed up by the snapshot to the directory/test/mounted by the logical volume/dev/vg1/lvmsp, as shown below:
CP-V/baklvm/testlvmos. img/test/
650) This. width = 650; "Title =" clip_image022 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image022 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519598rCTc.png "width =" 435 "Height =" 113 "/>
Start the VM testlvmos again, as shown below:
650) This. width = 650; "Title =" clip_image023 "style =" border-right-width: 0px; Background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; padding-Right: 0px; border-top-width: 0px; "Border =" 0 "alt =" clip_image023 "src =" http://img1.51cto.com/attachment/201408/20/526870_1408519599UJ7Z.png "width =" 1125 "Height =" 473 "/>
We can clearly see that the virtual machine files backed up by snapshots are exactly the same as the source files. In this way, LVM is restored.
Conclusion: two major aspects of LVM Snapshot Backup are as follows:
1. the LVM snapshot data is exactly the same as the original data.
2. Fast LVM Snapshot
Note:
1. the LVM snapshot volume does not require formatting or other steps. You can directly mount or detach the snapshot volume. After the operation is complete, you should immediately delete the snapshot, to reduce the I/O burden of the system. Snapshots are not automatically updated, so it is meaningless to keep them for a long time.
2. When you restore data from an LVM snapshot, you must first Delete the snapshot. Otherwise, LVM uses the original data.
This article from the "muddy world" blog, please be sure to keep this source http://ilanni.blog.51cto.com/526870/1542560