Rotten mud: LVM learning-KVM uses LVM snapshots to back up and restore virtual machines

Source: Internet
Author: User

This article was sponsored by Xiuyi linfeng and first launched 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:

First, check the logical volume in the system and use the LVS command. As follows:

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

Format the new logical volume as follows:

Mkfs. ext4/dev/vg1/lvmsp

After formatting the logical volume, create a new directory in the system to mount the new logical volume, as shown below:

Mkdir/test

Mount the logical volume as follows:

Mount/dev/vg1/lvmsp/test

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/

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

Start the Virtual Machine testlvmos as follows:

Virsh start testlvmos

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

-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/

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/

Check the directory/test1/attached to the snapshot as follows:

Comparison before and after snapshot ,:

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/

After the snapshot is backed up, We Will unmount the attached snapshot and delete it. As follows:

Umount/test1/

Lvremove/dev/vg1/lvmsp1

Now we delete the/test/directory mounted to the logical volume/dev/vg1/lvmsp, as follows:

Rm-r/test /*

After the deletion is complete, restart the Virtual Machine testlvmos as follows:

Virsh start testlvmos

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:

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/

Start the VM testlvmos again, as shown below:

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.