{"id":6493,"date":"2017-02-27T08:54:36","date_gmt":"2017-02-27T00:54:36","guid":{"rendered":"http:\/\/rmohan.com\/?p=6493"},"modified":"2017-02-27T08:54:36","modified_gmt":"2017-02-27T00:54:36","slug":"logical-volume-snapshots-in-centos-6","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6493","title":{"rendered":"Logical Volume Snapshots in CentOS 6"},"content":{"rendered":"<p>If it hasn\u2019t happened to you yet, you\u2019ll eventually experience a package update or system change that renders your server useless. You\u2019ll then be suffer through multiple stages of fright, ranging from \u201cDo we have a recent backup?\u201d to \u201cWhere\u2019s the documentation to rebuild this thing!?\u201d<\/p>\n<p>With the advent of logical volumes in Linux, we now have the ability to create snapshots of any our volumes before we make any changes to the system or the data. Snapshots allow us to rollback changes to a point in time before anything was done.<\/p>\n<p>In this tutorial, I\u2019ll show you how protect your system from bad patches or package installations. The method shown isn\u2019t limited to system patching, it can be used for any use case where changes are made. For example, you could create snapshot of a volume hosting your users\u2019 profiles before running scripts that modify them. If scripts causes a major problem, you can roll back.<\/p>\n<p>Another great example for snapshots is backups. Although snapshots should never be considered a form of backup, they do allow you to create a consistent state of a volume which can be targeted by your backup software.<\/p>\n<p>It\u2019s important to remember that logical volume snapshots create crash consistent states of your data; lvm cannot create application consistent snapshots, therefore, If you are running database services, remember to stop them first. Failing to do so may result in corrupted tables.<\/p>\n<h3>Prerequisets for Logical Volume Snapshots<\/h3>\n<p>The following are required before you can use snapshots on a volume.<\/p>\n<ul>\n<li>It must be a logical volume. Disk partitions cannot be snapshotted.<\/li>\n<li>The volume group hosting the logical volume must have unused space available.<\/li>\n<li>The amount of unused space must be enough to hold changes to the volumes data. If your change rate is 100MB a day and the snapshot is expected to be around for half a day, you\u2019ll need at least 50MB.<\/li>\n<\/ul>\n<h3>System Configuration<\/h3>\n<p>To make this tutorial easier to follow along, our lab server had the following disk configuration.<\/p>\n<table>\n<tbody>\n<tr>\n<th>Volume Group<\/th>\n<th>Device<\/th>\n<th>Size<\/th>\n<th>Unused Space<\/th>\n<\/tr>\n<tr>\n<td>vg01<\/td>\n<td>SDA2<\/td>\n<td>120 GB<\/td>\n<td>20 GB<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The following logical volumes were created.<\/p>\n<table>\n<tbody>\n<tr>\n<th>Log. Volume<\/th>\n<th>Volume Group<\/th>\n<th>Size<\/th>\n<th>Mount Point<\/th>\n<\/tr>\n<tr>\n<td>swap<\/td>\n<td>vg01<\/td>\n<td>2 GB<\/td>\n<\/tr>\n<tr>\n<td>lv_root<\/td>\n<td>vg01<\/td>\n<td>12 GB<\/td>\n<td>\/<\/td>\n<\/tr>\n<tr>\n<td>lv_var<\/td>\n<td>vg01<\/td>\n<td>60 GB<\/td>\n<td>\/var<\/td>\n<\/tr>\n<tr>\n<td>lv_home<\/td>\n<td>vg01<\/td>\n<td>26 GB<\/td>\n<td>\/home<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Creating a Snapshot<\/h2>\n<ol>\n<li>Verify the amount of unused storage in the volume group. Replace vg01 with the name of your volume group.<br \/>\nvgdisplay vg01<\/li>\n<li>The output should look similar to the one below. I\u2019ve highlighted the area which details the unused space.<\/li>\n<li>Create a snapshot for the root volume, lv_root.<br \/>\n<code>lvcreate -s -n lv_root_snap -L 10G vg01\/lv_root<\/code><\/p>\n<table>\n<tbody>\n<tr>\n<th>-s<\/th>\n<td>Signals to lvm that a snapshot is being created.<\/td>\n<\/tr>\n<tr>\n<th>-n<\/th>\n<td>Snapshot name<\/td>\n<\/tr>\n<tr>\n<th>-L<\/th>\n<td>Snapshot size, in kilobytes, megabytes, gigabytes, etc.<\/td>\n<\/tr>\n<tr>\n<th>vg01\/lv_root<\/th>\n<td>Volume to be snapshotted.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/li>\n<li>If successful, you will see the following output.<\/li>\n<\/ol>\n<h2>Modify Data on Logical Volume<\/h2>\n<p>With the snapshot created, we can now proceed with any planned modification to the volume. In this scenario, we\u2019re going to install system and application patches.<\/p>\n<ol>\n<li>Install the latest system patches.<br \/>\n<code>yum update<\/code><\/li>\n<\/ol>\n<h2>Rollback Snapshot<\/h2>\n<p>Our applications stopped working after the update. Instead of spending time trying to isolate the problem and attempt to correct it, causing longer down-time for our applications, we\u2019re simple going to rollback the snapshot to before the update.<\/p>\n<ol>\n<li>If possible, unmount the snapshotted volume. We created a snapshot of the root volume, so we won\u2019t be able to.<\/li>\n<li>Rollback the snapshot to bring volume to a state before the updates were installed.<br \/>\n<code>lvconvert --merge \/dev\/vg01\/lv_root_snap<\/code><\/li>\n<li>If the volume was still mounted, like ours was, you will get the following notification. Reboot the server to complete the rollback. Otherwise, if you can just mount the volume to see the rollback.\n<pre>Can't merge over open origin volume\r\nMerging of snapshot lv_root_snap will start next activation.<\/pre>\n<\/li>\n<\/ol>\n<h2>Mounting a Snapshot<\/h2>\n<p>Volume snapshots can also be mounted alongside their parent volumes. This allows us to do many things, such as browse the volume\u2019s state before changes were made; modify the volume, with an ability to safely rollback our changes; or create a point-in-time copy of a volume for our backup software, so that our data remains consistent while users are logged in.<\/p>\n<ol>\n<li>Create a directory to mount our snapshot into.<br \/>\n<code>mkdir \/snapshots<\/code><\/li>\n<li>Mount the snapshot.<br \/>\n<code>mount \/dev\/vg01\/lv_root_snap \/snapshots<\/code><\/li>\n<li>When done, unmount the snapshot.<br \/>\n<code>umount \/snapshots<\/code><\/li>\n<\/ol>\n<h2>Deleting Snapshots<\/h2>\n<p>Finally, snapshot changes can be applied to their parent volume after changes are made, if the results of whatever modification you made are satisfactory. To do this, we simply delete the snapshot.<\/p>\n<ol>\n<li>Unmount the snapshot, if you have it mounted.<\/li>\n<li>Delete the snapshot.<br \/>\n<code>lvremove \/dev\/vg01\/lv_root_snap<\/code><\/li>\n<li>When prompted, type \u2018y\u2019 and press Enter to confirm the deletion.<\/li>\n<li>You should see the following output if deletion was successfully completed.\n<pre>Do you really want to remove active logical volume lv_root_snap? [y\/n]: y\r\nLogical volume \"lv_root_snap\" successfully removed<\/pre>\n<\/li>\n<li>Verify the snapshot was deleted.<br \/>\n<code>lvdisplay vg01\/lv_root_snap<\/code><\/li>\n<li>You should get the following message if the snapshot was deleted.\n<pre>One or more specified logical volume(s) not found.<\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>If it hasn\u2019t happened to you yet, you\u2019ll eventually experience a package update or system change that renders your server useless. You\u2019ll then be suffer through multiple stages of fright, ranging from \u201cDo we have a recent backup?\u201d to \u201cWhere\u2019s the documentation to rebuild this thing!?\u201d<\/p>\n<p>With the advent of logical volumes in Linux, we [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6493"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6493"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6493\/revisions"}],"predecessor-version":[{"id":6494,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6493\/revisions\/6494"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}