chiark / gitweb /
Makefile.am: Stupid workaround for new Automake pettiness.
[distorted-backup] / snap.ro
1 #! /bin/sh
2 ###
3 ### Make fake snapshots by remounting a filesystem readonly
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   dir=MOUNTPT           Mount point of filesystem.
45   op=OPERATION          \`snap' to create snapshot, or \`unsnap' to remove.
46   tag=TAG               Disambiguation tag to store in filesystem.
47 EOF
48     exit
49     ;;
50 esac
51
52 ## Scan the option keys.
53 dev=$1; shift
54 Oop=snap Otag=snap
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 *.ro) k=${k%.ro} ;; ?*.?*) continue ;; esac
63   case "$k" in
64     op | tag | dir) 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 ## Find a mount point if none was given.
71 sdn='s/^b[^ ]*  *[^ ]*  *[^ ]*  *[^ ]*  *\([0-9]*\),  *\([0-9]*\) .*$/\1:\2/'
72 case "${Odir+t}" in
73   t) ;;
74   *)
75     exec 3</etc/mtab
76     devno=$(ls -lL "/dev/$dev" | sed "$sdn")
77     case "$devno" in
78       *:*) ;;
79       *) echo >&2 "$quis: $dev is not a block device"; exit 1 ;;
80     esac
81     while read <&3 d m fs hunoz; do
82       if [ ! -b "$d" ]; then continue; fi
83       dn=$(ls -lL "$d" | sed "$sdn")
84       case "$dn" in
85         "$devno")
86           case "${Odir+t}" in
87             t) echo >&2 "$quis: /dev/$dev mounted multiple times"; exit 1 ;;
88           esac
89           Odir=$m
90           ;;
91       esac
92     done
93     exec 3>&-
94     ;;
95 esac
96 case "${Odir+t}" in
97   t) ;; *) echo >&2 "$quis: /dev/$dev apparently not mounted"; exit 1 ;;
98 esac
99
100 ###--------------------------------------------------------------------------
101 ### Take or remove the snapshot.
102
103 case "$Oop" in
104
105   snap)
106     echo "$Otag" >"$Odir/.snap"
107     mount -oremount,ro "/dev/$dev" "$Odir"
108     echo "$dev"
109     ;;
110
111   unsnap)
112     if [ ! -f "$Odir/.snap" ]; then
113       echo >&2 "$quis: no snapshot tag"
114       exit 1
115     fi
116     read tag <"$Odir/.snap"
117     case "$tag" in
118       "$Otag") ;;
119       *)
120         echo >&2 "$quis: tag mismatch (found \`$tag' but expected \`$Otag')"
121         exit 1
122         ;;
123     esac
124     mount -oremount,rw "/dev/$dev" "$Odir"
125     rm "$Odir/.snap"
126     ;;
127
128   *)
129     echo >&2 "$quis: unknown operation \`$Oop'"
130     exit 1
131     ;;
132
133 esac
134
135 ###----- That's all, folks --------------------------------------------------