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.
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 --list
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:
# lvdisplay
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.
# vgdisplay
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 --extents 100%FREE --name lvroot vgsnap
The root logical volume, starting with 100% of device.
# lvresize --size -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 --size 15G --name lvswap vgsnap
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!
Mounting the snapshot:
Create the snapshot volume
# lvcreate --size 15G --snapshot --name 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.
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:
# reboot
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 --size 15G --snapshot --name 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
Best Answer
If you're on live media, your data and installation should be just fine. You'll need a terminal for this, so open one from the Dash menu, or by pressing the trusty Ctrl+Alt+T shortcut.
/media/ubuntu/<something>
. Make note of the mount path.blkid
and look for something withext*
in the name:Ex:
/dev/sda4: UUID="..." TYPE="ext4" PARTUUID="..."
Make note of the partition ID (
/dev/sda4
).sudo mount <id> /mnt
, replacing<id>
with whatever you got in step 3, in my case,/dev/sda4
.cd /mnt/var
.sudo cp -R www/ <mountpath_from_step_1>
.Finally, use Nautilus or
ls
to check that all of your files did actually get copied over successfully.After it's all over and done, unmount both drives with this command:
Alternatively, you could have done with without live media. On the broken Ubuntu install, you could have gone to TTY4 (Ctrl+Alt+F4), inserted a USB drive, and done this:
lsblk
and find the USB drive's lettering (ex,/dev/sdb1
)./mnt
with the command:sudo mount <step_1> /mnt
sudo mkdir /mnt/web-backup
)sudo cp -R /var/www /mnt/web-backup
ls
to verify:ls /mnt/web-backup
.sudo sync; sudo umount /mnt
You could also do these steps from the Recovery Shell. You can access this by pressing SHIFT while Grub boots, selecting
(advanced options)
,(recovery mode)
for the latest kernel, and then going to theroot
option.