chiark / gitweb /
changelog: finalise 6.0.4
[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+102399)/102400 + 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 -q "$lvpath"
59
60 mkdir -- "$snmnt"
61 mount -t $remountrocp_fs $lvm_mount_opts "$lvpath" "$snmnt"
62 echo ' copy filesystem created and mounted'
63
64 attempts=10
65 while true; do
66         if mount -o remount,ro "$mountpoint"; then break; fi
67         attempts=$(( $attempts - 1 ))
68         if [ $attempts = 0 ]; then
69                 echo >&2 'cannot remount readonly'
70                 exit 1
71         fi
72         sleep 1
73 done
74 trap "set +e; mount -o remount,rw $mountpoint; exit 12" 0
75 echo ' source remounted readonly, copying...'
76 cp -ax -- "$mountpoint/." "$snmnt/."
77 echo ' finalising...'
78 mount -o remount,rw "$mountpoint"
79 trap '' 0
80 mount -o remount,ro "$lvpath"
81
82 echo 'remountrocp snap activated'