Setting up LVM Snapshot as a backup/restore point in ubuntu.
Why?
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.
So, how can this be done?
Best Answer
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!
How?
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.
We will be clearing the hard drive! Please backup anything important before going through this stage.
First, let's remove the existing partitions:
Create the boot partition and the rest of the drive as the other partition
To check that the changes worked well run:
You should get something like this:
Remove any previous volume groups / logical volumes:
Will display the logical volumes you may have on the system
If any volume install that may interfere with this installation need be removed.
Will show any existing volume group on the system
Same as for the volume groups..
Create the Volume Group 'vgsnap'
Should result with:
Create the Logical Volume
The root logical volume, starting with 100% of device.
This will take off 30G for swap & snapshot volumes. Of course - you can choose smaller sizes for the swap/snapshot.
And create the swap volume:
Note: we leave now 15G of unused drive space, reserved for the future snapshot.
Installing Ubuntu
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
Install!
Using the snapshot
The following steps will show how to use the snapshot at any given time:
Mounting the snapshot:
Create the snapshot volume
Create the mount directory
Mount the snapshot as read-only mount
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.
How to use/install it? - Pretty much as with the link above:
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:
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)
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)
Re-mounting the snapshot: (See remount script, with the following instructions in it - link at the end of document) Pretty much as before:
If you saved the monitor_snapshot at your home dir you can restore it back:
That's all folks! Following instructions are for quitters only ;)
Discarding the snapshot volume (If no longer required)
(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:
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.