since I could not find how to post this as a blog/post/tutorial in AskUbuntu.. I decided on posting it here, were it is best exposed anyhow ...
Setting up LVM Snapshot as a backup/restore point in ubuntu.
Well, as I see it, with all the great usefulness of Linux, its biggest drawback is with its mixed up way of installing apps. All the dlls go to one directory. All the configuration files to another. And the binaries are also put in a bunch. Yes, Windows works the same way more or less, but with the nature of system/server software installed on Linux, being that most of the software installed is only about 95% mature at any given time, messing up your system twice a month is very probable..
Of course, we can backup the system before and after each installation, but that takes a million years. System backups (as oppose to just data/files backup) requires you to turn off you computer and use some form of bootable live CD and a backup media. And, if the drive is more than 15% full, it might take a long time to do the backup, even with modern hardware.
The alternative - LVM Snapshots - You can read about LVM snapshots here: http://www.tutonics.com/2012/12/lvm-guide-part-2-snapshots.html
In short - snapshots are a way of freezing the used sectors on the HardDisk, telling the system to write any change/delete/add on a reserved part of the HardDisk, called a snapshot volume, thus postpone the decision of keeping/discarding the changes on the drive to some future time, as far as the snapshot volume space provides. And so, once you decide to rollback the changes (or keep them), you can do it with one line.
It may seem complex at first, but I can assure you that after the first time effort, all that is needed to use the snapshot mechanism is just 2 line scripts (downloads provided!)
Warranty - No warranty what so ever is given for the instructions below. Use at your own risk!
If you don't have a pre-configured volume group, you will probably need to install a fresh clean Ubuntu. Then, you will have to put some effort into configuring the system correctly for the first time, but from this point on, snapshot will become effortless. And by effortless I mean one liner commands, using pre-configured scripts.
- Boot to the ubuntu live cd (no installation yet.. just the live cd)
- Open Accessories -> Terminal
We will be clearing the hard drive! Please backup anything important before going through this stage.
First, let's remove the existing partitions:
# fdisk /dev/sda Enter 'd' to delete - enter for as many partitions you have > d > <enter> ... Enter 'w' to write down changes > w
Create the boot partition and the rest of the drive as the other partition
# fdisk /dev/sda > n To create a new partition. > p For Boot Primary partition. > 1 Press <enter> to accept the default start sector. Enter: > +512M To give the boot partition a size of 512MB > n To create (another) new partition. > p Should be Primary too. > <enter> To accept the default value of 2 (the second partition) > <enter> To accept the default start sector > <enter> To accept the whole disk > t To change the type of the second partition to LVM > 2 To choose the second partition > 8e To set the type to 'LVM Linux' > w To write changes and exit.
To check that the changes worked well run:
# fdisk -l
You should get something like this:
Device Boot Start End Blocks Id System /dev/sda1 2048 524287 261120 83 Linux /dev/sda2 524288 488397167 243936440 8e Linux LVM
Remove any previous volume groups / logical volumes:
Will display the logical volumes you may have on the system
# lvremove <existing_logical_volume_name>
If any volume install that may interfere with this installation need be removed.
Will show any existing volume group on the system
# vgremove <existing_volume_group>
Same as for the volume groups..
Create the Volume Group 'vgsnap'
# vgcreate vgsnap /dev/sda2
Should result with:
Volume group "vgsnap" successfully created
Create the Logical Volume
# lvcreate -l 100%FREE -n lvroot vgsnap
The root logical volume, starting with 100% of device.
# lvresize -L -30G /dev/mapper/vgsnap-lvroot
This will take off 30G for swap & snapshot volumes. Of course - you can choose smaller sizes for the swap/snapshot.
> 'y' for the warning shown.
And create the swap volume:
# lvcreate -L 15G -n lvswap vgsnap
Note: we leave now 15G of unused drive space, reserved for the future snapshot.
Close the terimnal and start the Installer.
Choose "Use LVM..." (You may also choose to enable whole disk encryption - but I didn't test it..)
Select "Something Else" and press continue. At this stage you will find allot of /dev/mapper/... volumes defined. Some of these are auto generated (lvsnap-*) - you can ignore them for now.
Select: /dev/mapper/vgsnap-lvroot volume. Choose "ext4" as format, and mount as '/'
Select: /dev/mapper/vgsnap-lvswap volume, and choose "swap area"
If you are to create an encrypted drive (I have no clue if that would work with the snapshots?), you will probably want to select the /dev/sda2 and mark the "volume for encryption". Didn't test it though..
Note: The unused 15G are left free for the snapshot volume.
Select: /dev/sda1 partition. Format it as 'ext2' and mount it as '/boot'
Below - choose: /dev/sda1 partition as boot
Using the snapshot
The following steps will show how to use the snapshot at any given time:
Mounting the snapshot:
Create the snapshot volume
# lvcreate -L 15G -s -n lvsnap /dev/mapper/vgsnap-lvroot
Create the mount directory
# mkdir /mnt/vgsnap-lvsnap
Mount the snapshot as read-only mount
# mount -o ro /dev/vgsnap/lvsnap /mnt/vgsnap-lvsnap
Snapshot is On!
At this point you may start messing up your system, installing beta drivers, and even (No! That's just inappropriate).
From now on you may at any time (as far as the snapshot size provides..) return to previous state, or, if satisfied with the new look - keep it.
Automatic snapshot volume free disk space monitoring: Using a tweaked version of the script provided here: http://blog.rimuhosting.com/2012/02/20/cron-script-for-checking-disk-space/ I created monitor_snapshot script (see link at the end of document)
How to use/install it? - Pretty much as with the link above:
# cd /etc/cron.hourly # copy /home/<user>/Downloads/monitor_snapshot # chmod +x monitor_snapshot
Before running it for the first time you better edit the values in it, such as your email address (where the notifications are to be sent, etc..) If properly configured, the cron daemon will run the script every X hours/days - as you decide - and notify you once the snapshot is about to be full.
To test the script run:
# sh /etc/cron.hourly/monitor_snapshot
It will print the percentage (number) of free space in the snapshot.
Important! Once you're done with your tests/installations - rollback or keep changes - and Umount the snapshot!
Rolling back changes: (See rollback_snapshot script, with the following instructions in it, link at the end of document)
# lvconvert --merge /dev/vgsnap/lvsnap
This will also umount the snapshot, but will discard any of your changes. Also - You will see a warning that suggest that the revert will only occur next activation, so:
Note: If you skip merging in, your changes shall be committed once you Umount the snapshot.
Committing changes - umounting the snapshot: (See commit_snapshot script, with the following instructions in it - link at the end of document)
# umount /mnt/vgsnap-lvsnap Will unmount (commit0 the snapshot # lvremove /dev/mapper/vgsnap-lvsnap # rm -rf /mnt/vgsnap-lvsnap/ # rm /etc/cron.hourly/monitor_snapshot Remove the cron daemon script (that is unused)
Re-mounting the snapshot: (See remount script, with the following instructions in it - link at the end of document) Pretty much as before:
# lvcreate -L 15G -s -n lvsnap /dev/mapper/vgsnap-lvroot # mkdir /mnt/vgsnap-lvsnap # mount -o ro /dev/vgsnap/lvsnap /mnt/vgsnap-lvsnap
If you saved the monitor_snapshot at your home dir you can restore it back:
# cp /home/<user>/monitor_snapshot /etc/cron.hourly/ # chmod +x /etc/cron.hourly/monitor_snapshot
That's all folks! Following instructions are for quitters only ;)
Discarding the snapshot volume (If no longer required)
# rmdir /mnt/vgsnap-lvsnap # lvremove /dev/vgsnap/lvsnap
(You might also want to resize the root partition or swap to occupy the missing 15G reserved for swap)
Discarding the volume group - Possible only by removing the volume group's volumes, which probably means deleting the OS installation. But of course, there's no real need to remove the volume group...
To remove the volume group:
# vgremove vgsnap
Important Note! - formatting / running fdisk and removing all partitions will NOT remove the volumes nor the group, as these are written on a different part of the drive... So, just use the remove instructions given above and remove the volumes and group if required.