Descriptionlvsnap is a simple bash script that reads a configuration file (by default /usr/local/etc/lvsnaptab) with a format similar to fstab. The lvsnaptab file defines the logical volumes that should be snapped, the amount of space to be allocated to the snapshot, and the location the snaps should be mounted. For volumes that can't be snapped (such a filesystems on normal partitions) the lvsnaptab also supports bind mounts. This can be especially useful if you are attempting to create complete chrootable snapshots environments for backup purposes but you also need to include a filesystem that does not support snapshots.
UseHere's an example of a simple lvsnaptab file:
/dev/local/root /mnt/snapback ext3 snap 2G
/boot /mnt/snapback/boot ext3 bind
Using this file running the command "lvsnap start" would create a snapshot of the logical volume named
root on the volume group
local and allocate 2GB from the volume group for the snapshot COW area (obviously this space would need to be available). It would also automatically mount this snapshot at "/mnt/snapback". The script would then create a bind mount for /boot so that the /boot filesystem would appear at "/mnt/snapback/boot". Since a bind mount is simple a method for allowing the same filesystem to appear in two places it does not require a size parameter.
Our servers normally have multiple volumes for root, /var, and /usr, as well as additional volume for applications like Oracle or for file services so they are a little more complex, here's an example:
/dev/local/root /mnt/snapback ext3 snap 2G
/boot /mnt/snapback/boot bind bind
/dev/local/var /mnt/snapback/var ext3 snap 2G
/dev/local/usr /mnt/snapback/usr ext3 snap 6G
/dev/shared/home /mnt/snapback/home ext3 snap 40G
Now running "lvsnap start" would create snapshots for the logical volumes
root,
var and
usr on volume group
local and also a snapshot of logical volume
home on volume group
shared. It would then mount these snapshots, in a tree underneath /mnt/snapback, including a bind mount of the boot volume. You could then "chroot" to /mnt/snapback to have a consistent image of your system ideal for a backup (well, boot isn't exactly consistent, but doesn't usually change much). Here's an example of the output from running "lvsnap start":
Creating LVM snaphot of /dev/local/root...OK
Creating LVM snaphot of /dev/local/var...OK
Creating LVM snaphot of /dev/local/usr...OK
Creating LVM snaphot of /dev/shared/home...OK
Mounting snapshot volume /dev/local/root-snap at /mnt/snapback...OK
Creating bind mount for /boot at /mnt/snapback/boot...OK
Mounting snapshot volume /dev/local/var-snap at /mnt/snapback/var...OK
Mounting snapshot volume /dev/local/usr-snap at /mnt/snapback/usr...OK
Mounting snapshot volume /dev/shared/home-snap at /mnt/snapback/home...OK
To remove the mounts and snapshots simply run "lvsnap stop". It's important to note that snapshots are created and mounted in the order that they are listed and are umounted and removed in the reverse order.
Downloadlvsnap-0.7b.tar.gz (Beta) -- Apr 7, 2008
- Don't use color if not running on tty (useful if mailing output from cron, etc)
- Check if snapshot/bind mount is already mounted prior to calling mount (avoids multiple mounted filesystems on the same mounts points which can cause problems with recovering from errors or of "lvsnap start" is run twice without a "stop", especially for bind mounts)
lvsnap-0.6b.tar.gz (Beta) -- Apr 3, 2008 -- First public release
FutureThis script is pretty basic and there is plenty of room for enhancement, here are some future plans:
- More granular options to mount, unmount, create and destroy snapshots rather than just start and stop
- Improve error handling and reporting
- Options in lvsnaptab to create read-only snapshots and/or mount read-only
- Option to use dmsetup suspend to flush and pause I/O during snapshot creation
- Options for ordering and grouping LV's that should be snapped "together"
- Options for running fsck prior to mounting
- Automatic snapshot space allocation where the script determines the amount of free space available in the volume group that could be used for snapshots and allocates that space proportionally based on the size of the LV being snapped or a percentage in lvsnaptab
- Support for multiple snapshots of a single LV, perhaps with rotating mount points.
- Add support for "external" snapshots by running external scripts to take advantage of storage level snapshot capabilities rather than Linux LVM snapshots. Could be especially useful for systems with normal partitions that have SAN's with snapshot capabilities. Basically something similar to Microsoft VSS integration with third party snapshot helpers.
Friday, August 22. 2008 at 13:36 (Reply)