Author: jo

# Snapshots Save

but you should still make Backups

## Snapshots

A raid 1 protects your data against disk failure. It does not protect them against accidental user changes such as deletion or overwriting. Such protection can however be provided by snapshots.

Snapshots are a feature that is implemented by the kernels device mapper. They can be managed with the logical volume manager LVM2. They provide a frozen image of a block device without the costs (in time and space) of copying the device. This is achieved using COW (Copy On Write). Every block on the device is saved to a separate block device before it is written. This is used to reconstruct the original state through an appropriate block mapping.

## `/home` Safety

The setup that is described is as follows. There is a raid1 device `/dev/md1` that is fed into a LVM2 volume group `volg1` as a physical volume. This provides a logical volume `/dev/volg1/home` that is mounted at `/home`.

A snapshot `/dev/volg1/home.old` of `/dev/volg1/home` is dropped and recreated at boot time. The snapshot is later mounted at `/home.old`. So everybody can easily restore the boot time state of his files.

Snapshot recreation is done from an init script. The critical line that creates the snapshot is

        lvcreate -l 19539 -c 512  -s -p r -n home.old /dev/volg1/home /dev/sdc1

Note that this uses `/dev/hdc1` as the COW device. This is unfortunately an undocumented `lvcreate` parameter. It is only mentioned on the linux-lvm mailing list. Using a separate physical COW device reduces the write performance penalty of the snapshot. To do this `/dev/sdc1` has to be a physical volume in the `volg1` group:

        pvcreate /dev/sdc1
        vgextend volg1 /dev/sdc1

The rather large chunk size (`-c 512`) serves the same purpose. The writes are however still slowed down by a factor of approximately 6. Snapshots can be writeable, `-p r` turns this off. `-l 19539` is the size of `/dev/sdc1` in physical extends:

        enter:~# pvdisplay /dev/sdc1 | grep Total
          Total PE              19539

To make sure the additional `lvcreate` parameter has not been silently ignored we take a look at the device mapper table:

        enter:~# dmsetup table
        volg1-home.old-cow: 0 160063488 linear 8:33 384
        volg1-home: 0 1468006400 snapshot-origin 253:2
        volg1-home-real: 0 1468006400 linear 9:1 384
        volg1-home.old: 0 1468006400 snapshot 253:2 253:3 P 1024

The `volg1-home.old-cow` lines major:minor of 8:33 is the one from `/dev/sdc1`:

        enter:~# ls -l /dev/sdc1
        brw-rw---- 1 root disk 8, 33 22. Mär 17:44 /dev/sdc1