chiark / gitweb /
remountrocp
[chiark-utils.git] / backup / remountrocp
1 #!/bin/bash
2 # invoked by backup scripts as
3 #    remountrocp snap $vardir $device $mountpoint
4 #               remounts $mountpoint readonly
5 #               copies data to $vardir/snap-mount
6 #               remounts $mountpoint readwrite
7 #    remountrocp drop $vardir
8 #               deletes $vardir/snap-mount
9
10 set -e
11 snapkind=remountrocp
12 : ${lvm_vg:=}
13 remountrocp_fs=ext2
14 . ${CHIARK_BACKUP_SHAREDIR:-/usr/share/chiark-backup}/snap-common
15
16 #---------- clean up anything
17
18 vgroup=$lvm_vg
19 lvmdropcore
20
21 lastsettings="$vardir/remountrocp-settings"
22 test ! -f $lastsettings || . $lastsettings
23
24 if test "$opmode" = drop; then
25         test -z "$last_mountpoint" || mount -o remount,rw $last_mountpoint
26         rm -f $lastsettings
27         echo 'remountrocp snap dropped'
28         exit 0
29 fi
30
31 #---------- create snapshot
32
33 if [ -z "$lvm_lvsize_opts" ]; then
34         lvmextentscore1
35
36         df_out="$(really df -P --block-size=$extsize $mountpoint)"
37         extents2="$(printf "%s" "$df_out" | awk '/^\// {print $3}')"
38         extents2=$(( ($extents2*150+99)/100 + 4 ))
39
40         lvmextentscore2
41 fi
42
43 lvmcreatecore1
44
45 cat >$lastsettings.new <<END
46 last_mountpoint=$mountpoint
47 END
48 mv -f $lastsettings.new $lastsettings
49
50 lvcreate \
51         $lvm_lvtools_opts \
52         $lvm_lvsize_opts \
53         -n $lvm_lv \
54         $lvm_lvcreate_opts \
55         $vgroup \
56         $lvm_lvcreate_args
57
58 mkfs -t $remountrocp_fs "$lvpath"
59 mkdir -- "$snmnt"
60 mount -t $remountrocp_fs $lvm_mount_opts "$lvpath" "$snmnt"
61
62 mount -o remount,ro "$mountpoint"
63 trap "mount -o remount,rw $mountpoint; exit 12" 0
64 cp -a -- "$mountpoint/." "$snmnt/."
65 mount -o remount,rw "$mountpoint"
66 trap '' 0
67 mount -o remount,ro "$lvpath"
68
69 echo 'remountrocp snap activated'