| 1 | #! /bin/sh |
| 2 | ### |
| 3 | ### Establish snapshots of LVM logical volumes |
| 4 | ### |
| 5 | ### (c) 2011 Mark Wooding |
| 6 | ### |
| 7 | |
| 8 | ###----- Licensing notice --------------------------------------------------- |
| 9 | ### |
| 10 | ### This file is part of the distorted.org.uk backup suite. |
| 11 | ### |
| 12 | ### distorted-backup is free software; you can redistribute it and/or modify |
| 13 | ### it under the terms of the GNU General Public License as published by |
| 14 | ### the Free Software Foundation; either version 2 of the License, or |
| 15 | ### (at your option) any later version. |
| 16 | ### |
| 17 | ### distorted-backup is distributed in the hope that it will be useful, |
| 18 | ### but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | ### GNU General Public License for more details. |
| 21 | ### |
| 22 | ### You should have received a copy of the GNU General Public License along |
| 23 | ### with distorted-backup; if not, write to the Free Software Foundation, |
| 24 | ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | |
| 26 | set -e |
| 27 | quis=${0##*/} |
| 28 | |
| 29 | ###-------------------------------------------------------------------------- |
| 30 | ### Parse the command line. |
| 31 | |
| 32 | ## Provide help or version information. |
| 33 | usage="usage: $quis DEVICE [KEY=VALUE ...]" |
| 34 | version="$quis, version 1.0.0" |
| 35 | case "$#,$1" in |
| 36 | 0,*) echo >&2 "$usage"; exit 1 ;; |
| 37 | *,-v | *,--version) echo "$version"; exit ;; |
| 38 | *,-h | *,--help) |
| 39 | cat <<EOF |
| 40 | $version |
| 41 | $usage |
| 42 | |
| 43 | Option keys: |
| 44 | op=OPERATION \`snap' to create snapshot, or \`unsnap' to remove. |
| 45 | snapsz=SIZE Size to reserve for snapshot storage. |
| 46 | tag=TAG Disambiguation tag to append to logical volume name. |
| 47 | EOF |
| 48 | exit |
| 49 | ;; |
| 50 | esac |
| 51 | |
| 52 | ## Scan the option keys. |
| 53 | dev=$1; shift |
| 54 | Oop=snap Otag=snap Osnapsz=100M |
| 55 | win=t |
| 56 | for i in "$@"; do |
| 57 | case "$i" in |
| 58 | ?*=*) ;; |
| 59 | *) echo >&2 "$quis: malformed option \`$i'"; exit 1 ;; |
| 60 | esac |
| 61 | k=${i%%=*} v=${i#*=} |
| 62 | case "$k" in *.lvm) k=${k%.lvm} ;; ?*.?*) continue ;; esac |
| 63 | case "$k" in |
| 64 | op | tag | snapsz) eval "O$k=\$v" ;; |
| 65 | *) echo >&2 "$quis: unknown option \`$k'"; win=nil ;; |
| 66 | esac |
| 67 | done |
| 68 | case $win in nil) exit 1 ;; esac |
| 69 | |
| 70 | ## Check the device name. |
| 71 | case "$dev" in |
| 72 | ?*/?*) ;; *) echo >&2 "$quis: device \`$dev' should be VG/LV"; exit 1 ;; |
| 73 | esac |
| 74 | vg=${dev%%/*} lv=${dev#*/} |
| 75 | |
| 76 | ###-------------------------------------------------------------------------- |
| 77 | ### Take or remove the snapshot. |
| 78 | |
| 79 | case "$Oop" in |
| 80 | |
| 81 | snap) |
| 82 | case "$Osnapsz" in |
| 83 | *%*) szarg="extents" ;; |
| 84 | *) szarg="size" ;; |
| 85 | esac |
| 86 | lvcreate >/dev/null --snapshot \ |
| 87 | --$szarg="$Osnapsz" \ |
| 88 | --name="$lv.$Otag" \ |
| 89 | "$vg/$lv" |
| 90 | echo "$vg/$lv.$Otag" |
| 91 | ;; |
| 92 | |
| 93 | unsnap) |
| 94 | ## LVM snapshot removal is full of awful bugs, mostly to do with races |
| 95 | ## with udev. We have a handy script which does the necessary. May it |
| 96 | ## not be needed for long. |
| 97 | lvm-rmsnap >/dev/null "$vg/$lv.$Otag" |
| 98 | ;; |
| 99 | |
| 100 | *) |
| 101 | echo >&2 "$quis: unknown operation \`$Oop'" |
| 102 | exit 1 |
| 103 | ;; |
| 104 | |
| 105 | esac |
| 106 | |
| 107 | ###----- That's all, folks -------------------------------------------------- |